In this code, everything is going great. The 2 statements that are called in the main method are good. However, in the code below this one, it isn't.
package Demo;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class GUIDemo implements ActionListener {
private static JLabel userLabel;
private static JTextField userText;
private static JLabel passwordLabel;
private static JPasswordField passwordText;
private static JButton button;
private static JLabel success;
private static boolean openWindow;
@Override
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String password = passwordText.getText();
try {
if (areValid(user, password)) {
success.setText("Login successfully. This needs a few seconds.");
openWindow = true;
} else {
System.exit(1);
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public boolean areValid(String user, String password) throws IOException {
FileReader fr = new FileReader("C:\\Users\\User\\IdeaProjects\\Tako\\src\\AirTraffic\\Username_password");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
try {
String[] elements = line.split("-");
String username = elements[0].trim();
String pass = elements[1].trim();
if (username.equals(user) && pass.equals(password)) {
br.close();
fr.close();
return true;
}
} catch (Exception e) {
}
line = br.readLine();
}
br.close();
fr.close();
return false;
}
public void execute() throws InterruptedException {
JPanel panel = new JPanel();
JFrame frame = new JFrame("Tako's airport - Login Process");
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("User");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
button = new JButton("Login");
button.setBounds(10, 80, 80, 25);
button.addActionListener(new GUIDemo());
panel.add(button);
success = new JLabel("");
success.setBounds(10, 110, 300, 25);
panel.add(success);
frame.setVisible(true);
while (!openWindow) {
int a = (int) (Math.random() * 10);
}
if (openWindow) {
Thread.sleep(2000);
frame.setVisible(false);
}
}
public static void main(String[] args) throws InterruptedException {
GUIDemo gui = new GUIDemo();
gui.execute();
}
}
In this code, the same 2 statements are written in the actionPerformed(e) method. Is there an explanation behind it? In the previous code, it's showing the whole window with the panels. However, in this code, it's showing an empty window after I put administrator --> Continue.
package Demo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FP {
public JFrame frame;
public static void main(String[] args) {
new FP();
}
public FP() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame = new JFrame("Login");
frame.setSize(350, 200);
frame.add(new MainPane());
frame.pack();
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel implements ActionListener {
private ButtonGroup checkBoxGroup;
private JCheckBox jcb1, jcb2;
private JButton button;
private JLabel success;
public MainPane() {
jcb1 = new JCheckBox("Administrator", false);
jcb2 = new JCheckBox("Customer", false);
JLabel selectLabel = new JLabel("Login as:");
add(selectLabel);
checkBoxGroup = new ButtonGroup();
checkBoxGroup.add(jcb1);
checkBoxGroup.add(jcb2);
add(jcb1);
add(jcb2);
button = new JButton("Continue");
button.addActionListener(this);
add(button);
success = new JLabel("");
add(success);
}
@Override
public void actionPerformed(ActionEvent e) {
if (jcb1.isSelected()) {
try {
frame.setVisible(false);
GUIDemo gui = new GUIDemo();
gui.execute();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} else {
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire