我有一个恼人的消息框,当我不想要它时弹出。 在用户登录并且隐藏按钮可见后发生问题,但是当单击它时它再次显示“正确”消息?我也尝试将它放在第一个声明和底部之上。 编辑:恼人的消息是成功登录后出现的消息,不,我不需要拼写检查,
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("Closing window!");
System.exit(0);
}
}
class LoginForm extends JFrame implements ActionListener {
private final String username = "user";
private final String password = "pass";
JFrame frame;
JPanel jPanel;
JLabel userLabel;
final JTextField userText;
JLabel passLabel;
final JPasswordField passText;
JButton loginBtn;
JButton shopBtn;
JLabel welcome;
{
frame = new JFrame();
jPanel = new JPanel();
userLabel = new JLabel("Login : ");
userText = new JTextField(10);
passLabel = new JLabel("Password : ");
passText = new JPasswordField(10);
loginBtn = new JButton("Login");
shopBtn = new JButton("Go to Shop");
welcome = new JLabel("Welcome to ECSE501 Computers");
setTitle("Login Page");
loginBtn.addActionListener(this);
shopBtn.addActionListener(this);
Container c1 = new Container();
c1.setLayout(new GridLayout (3,2));
c1.add(userLabel);
c1.add(userText);
c1.add(passLabel);
c1.add(passText);
c1.add(loginBtn);
Container c2 = new Container();
c2.setLayout(new BoxLayout(c2, BoxLayout.Y_AXIS));
c2.add(welcome);
c2.add(shopBtn);
welcome.setVisible(false);
shopBtn.setVisible(false);
add(jPanel);
jPanel.add(c1);
jPanel.add(c2);
}
public void actionPerformed(ActionEvent e) {
String userInput = userText.getText();
char[] pass = passText.getPassword();
String p = new String(pass);
if (userInput.equals(username) && p.equals(password)) {
jPanel.setVisible(true);
welcome.setVisible(true);
jPanel.setBackground(Color.green);
JOptionPane.showMessageDialog(null, "Correct");
shopBtn.setVisible(true);
JButton hiddenBtn = (JButton) e.getSource();
if ( hiddenBtn == shopBtn)
{
SelectionForm selection = new SelectionForm();
selection.select();
}
}
else {
jPanel.setBackground(Color.red);
JOptionPane.showMessageDialog(null, "Wrong Login Details");
}
}
}
public class LoginTester {
public static void main(String[] args) {
// register an event handler for frame events
LoginForm frame = new LoginForm();
frame.addWindowListener(new MyWindowListener());
frame.setSize(300, 200);
frame.setVisible(true);
//frame.pack();
}
}
答案 0 :(得分:5)
您将此全部放在actionPerformed
方法中。只要执行任何操作,单击按钮,编辑文本字段等,就会调用它。
如果您不想在单击按钮时运行此选项,请使用getSource()
检查事件的来源,如果源是按钮,则不要运行代码。你的方法看起来像这样:
public void actionPerformed(ActionEvent e) {
String userInput = userText.getText();
char[] pass = passText.getPassword();
String p = new String(pass);
if(e.getSource().equals(loginBtn)) {
if (userInput.equals(username) && p.equals(password)) {
jPanel.setVisible(true);
welcome.setVisible(true);
jPanel.setBackground(Color.green);
JOptionPane.showMessageDialog(null, "Correct");
shopBtn.setVisible(true);
JButton hiddenBtn = (JButton) e.getSource();
}
else {
jPanel.setBackground(Color.red);
JOptionPane.showMessageDialog(null, "Wrong Login Details");
}
}
else if (e.getSource().equals(shopBtn)) {
SelectionForm selection = new SelectionForm();
selection.select();
}
}