J'ai un LoginView de classe comme ceci:Comment définir un cadre visible dans un écouteur (Java)?
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class LoginView {
public static void main(String[] args) {
JFrame frame = new JFrame("Pagina di login");
frame.setSize(600, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
placeComponents(frame);
frame.setVisible(true);
}
private static void placeComponents(JFrame frame) {
frame.setLayout(null);
JLabel userLabel = new JLabel("Username");
userLabel.setBounds(110, 10, 80, 25);
frame.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(200, 10, 180, 25);
frame.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(110, 40, 80, 25);
frame.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(200, 40, 180, 25);
frame.add(passwordText);
JButton loginButton = new JButton("Login");
loginButton.setBounds(110, 80, 80, 25);
frame.add(loginButton);
JButton registerButton = new JButton("Registrati");
registerButton.setBounds(280, 80, 100, 25);
frame.add(registerButton);
ActionListener loginButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String uname = userText.getText();
String pass = passwordText.getText();
boolean login_error=true;
if (pass.contains("admin") && uname.contains("admin")) {
JButton source = (JButton) e.getSource();
JOptionPane.showMessageDialog(source, "Username e password valide per admin. \n" +
"Sei entrato nel sistema come admin.");
login_error=false;
userText.setText(null);
passwordText.setText(null);
frame.dispose();
AdminView catalogo = new AdminView();
}
try {
BufferedReader br = new BufferedReader(new FileReader("cliente.txt"));
try {
String line;
while ((line = br.readLine()) != null) {
String[] snippet = line.split(",");
//System.out.println(snippet[0]+" "+snippet[1]);
if (snippet[0].equals(uname) && snippet[1].equals(pass)) {
JButton source = (JButton) e.getSource();
JOptionPane.showMessageDialog(source, "Username e password validi. \n" +
"Sei entrato nel negozio.");
login_error=false;
userText.setText(null);
passwordText.setText(null);
frame.dispose();
AdminView catalogo = new AdminView();
break;
}
}
} catch (IOException ioe){
JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
}
if (login_error == true) {
JOptionPane.showMessageDialog(null, "Username e password non validi.", "login error", JOptionPane.ERROR_MESSAGE);
userText.setText(null);
passwordText.setText(null);
}
}
};
ActionListener registerButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SignView registrazione = new SignView();
}
};
loginButton.addActionListener(loginButtonListener);
registerButton.addActionListener(registerButtonListener);
}
}
Cette classe génère deux boutons, loginButton et registerButton. Dans le login ActionListener, je vérifie ces champs de texte, ouvre AdminView et exécute le code qu'il exécute. Dans le registre ActionListener j'ouvre SignupView, mais si j'exécute le code le cadre ouvert n'apparaît pas. Pourquoi?
C'est la classe SignupView qui n'est pas complète, mais il pourrait apparaître:
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class SignView {
public static void main(String[] args) {
JFrame frame = new JFrame("Pagina di Registrazione");
frame.setSize(500, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
placeComponents(frame);
frame.setVisible(true);
}
private static void placeComponents(JFrame frame) {
frame.setLayout(null);
JLabel userLabel = new JLabel("Username");
userLabel.setBounds(10, 10, 80, 25);
frame.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(150, 10, 240, 25);
frame.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 40, 80, 25);
frame.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(150, 40, 240, 25);
frame.add(passwordText);
JLabel pwdLabel = new JLabel("Conferma password");
pwdLabel.setBounds(10, 70, 130, 25);
frame.add(pwdLabel);
JTextField pwdText = new JTextField(20);
pwdText.setBounds(150, 70, 240, 25);
frame.add(pwdText);
JLabel nameLabel = new JLabel("Nome");
nameLabel.setBounds(10, 100, 80, 25);
frame.add(nameLabel);
JTextField nameText = new JTextField(20);
nameText.setBounds(150, 100, 240, 25);
frame.add(nameText);
JLabel surnameLabel = new JLabel("Cognome");
surnameLabel.setBounds(10, 130, 80, 25);
frame.add(surnameLabel);
JTextField surnameText = new JTextField(20);
surnameText.setBounds(150, 130, 240, 25);
frame.add(surnameText);
JLabel cityLabel = new JLabel("Citta' di residenza");
cityLabel.setBounds(10, 160, 130, 25);
frame.add(cityLabel);
JTextField cityText = new JTextField(20);
cityText.setBounds(150, 160, 240, 25);
frame.add(cityText);
JLabel codeLabel = new JLabel("Codice fiscale");
codeLabel.setBounds(10, 190, 80, 25);
frame.add(codeLabel);
JTextField codeText = new JTextField(20);
codeText.setBounds(150, 190, 240, 25);
frame.add(codeText);
JLabel telLabel = new JLabel("Numero di telefono");
telLabel.setBounds(10, 220, 130, 25);
frame.add(telLabel);
JTextField telText = new JTextField(20);
telText.setBounds(150, 220, 240, 25);
frame.add(telText);
JLabel smartLabel = new JLabel("Numero di cellulare");
smartLabel.setBounds(10, 250, 130, 25);
frame.add(smartLabel);
JTextField smartText = new JTextField(20);
smartText.setBounds(150, 250, 240, 25);
frame.add(smartText);
JButton creationAccButton = new JButton("Crea account");
creationAccButton.setBounds(150, 280, 110, 25);
frame.add(creationAccButton);
JButton backLoginButton = new JButton("Ritorna al login");
backLoginButton.setBounds(270, 280, 120, 25);
frame.add(backLoginButton);
ActionListener creationAccButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String passwd = passwordText.getText();
String passwdConfirm = pwdText.getText();
String uname = nameText.getText();
String cityRes = cityText.getText();
String taxCode = codeText.getText();
String telNumber = telText.getText();
String telSmartphone = smartText.getText();
boolean login_error=true;
try {
BufferedReader br = new BufferedReader(new FileReader("cliente.txt"));
try {
String line;
while ((line = br.readLine()) != null) {
String[] snippet = line.split(",");
//System.out.println(snippet[0]+" "+snippet[1]);
if (snippet[0].equals(user)) {
JButton source = (JButton) e.getSource();
JOptionPane.showMessageDialog(source, "Username gia' registrato.");
login_error=false;
userText.setText(null);
passwordText.setText(null);
break;
}
}
} catch (IOException ioe){
JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
}
if (login_error == true) {
JOptionPane.showMessageDialog(null, "Username e password non validi.", "login error", JOptionPane.ERROR_MESSAGE);
userText.setText(null);
passwordText.setText(null);
frame.dispose();
AdminView catalogo = new AdminView();
}
}
};
creationAccButton.addActionListener(creationAccButtonListener);
backLoginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog((Component) e.getSource(),
"button has been pressed");
}
});
}
}