Ciao a tutti,
Io sto facendo una specie di progetto in cui ad un utente viene mostrata una finestra login, se questo però non è già registrato deve schiacciare il bottone Registra e dovrebbe visualizzarsi la finestra per la registrazione. Io ho ovviamente creato due frame diversi. Quello che mi chiedevo io è : come faccio dal codice di Login a far sì che, premuto il bottone "New? Register", mi si apra la finestra di registrazione?
questi sono i due codici
LOGIN:
package progettoSweng;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.*;
import java.lang.Override;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.Font;
public class Login extends JFrame {
private JPanel contentPaneUN;
private JTextField textFieldUN;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 545, 353);
contentPaneUN = new JPanel();
contentPaneUN.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPaneUN);
contentPaneUN.setLayout(null);
JLabel lblNewLabel = new JLabel("Username");
lblNewLabel.setBounds(104, 117, 55, 14);
contentPaneUN.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Password");
lblNewLabel_1.setBounds(104, 175, 46, 14);
contentPaneUN.add(lblNewLabel_1);
textFieldUN = new JTextField();
textFieldUN.setBounds(268, 114, 86, 20);
contentPaneUN.add(textFieldUN);
textFieldUN.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!textFieldUN.getText().isEmpty() && !passwordField.getText().isEmpty()) {
if(UsersRegist.getInstance().credentialsLogCheck(textFieldUN.getText(), passwordField.getText())) {
JOptionPane.showMessageDialog(null, "YOU ARE SUCCESFULLY LOGINED");
}
else {
JOptionPane.showMessageDialog(null, "INVALID USERNAME OR PASSWORD");
}
}
else {
JOptionPane.showMessageDialog(null, "INCOMPLETE FORM");
}
}
});
btnLogin.setBounds(268, 239, 89, 23);
contentPaneUN.add(btnLogin);
passwordField = new JPasswordField();
passwordField.setBounds(268, 172, 86, 20);
contentPaneUN.add(passwordField);
JButton btnNewButton = new JButton("New? Register!");
btnNewButton.addMouseListener(new MouseAdapter() {
//?
});
btnNewButton.setBounds(404, 239, 115, 23);
contentPaneUN.add(btnNewButton);
JLabel lblNewLabel_2 = new JLabel("USERAPP");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabel_2.setBounds(216, 29, 148, 37);
contentPaneUN.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("LOGIN");
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel_3.setBounds(229, 70, 46, 14);
contentPaneUN.add(lblNewLabel_3);
}
}
REGISTER:
package progettoSweng;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 402);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabelREG = new JLabel("USERAPP");
lblNewLabelREG.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabelREG.setBounds(166, 22, 113, 34);
contentPane.add(lblNewLabelREG);
JLabel lblNewLabelRE = new JLabel("REGISTRATION");
lblNewLabelRE.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabelRE.setBounds(157, 67, 152, 34);
contentPane.add(lblNewLabelRE);
textField = new JTextField();
textField.setBounds(257, 112, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(257, 156, 86, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(257, 202, 86, 20);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(257, 241, 86, 20);
contentPane.add(textField_3);
textField_3.setColumns(10);
JLabel lblNewLabel = new JLabel("USERNAME");
lblNewLabel.setBounds(79, 115, 86, 14);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("PASSWORD");
lblNewLabel_1.setBounds(79, 159, 86, 14);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("MOBILE PHONE");
lblNewLabel_2.setBounds(79, 205, 86, 14);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("E-MAIL ADDRESS");
lblNewLabel_3.setBounds(79, 244, 86, 14);
contentPane.add(lblNewLabel_3);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(257, 297, 89, 23);
contentPane.add(btnNewButton);
}
}
Io ho pensato di creare un nuovo oggetto di tipo Register in Login, ma non ho più idea di come andare avanti.