java - Swing if else 语句

标签 java swing actionlistener

我有一个烦人的消息框,当我不想要它时就会弹出。 问题发生在用户登录并且隐藏按钮可见后,但是当单击它时,它再次显示“正确”消息?我还尝试将其放在第一个语句的顶部和底部。 编辑:烦人的消息是成功登录后出现的消息,不,我不需要拼写检查,

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();
        }
    }

最佳答案

您将这一切放入您的 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();

        }
}

关于java - Swing if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8498872/

相关文章:

Java getText JTextField

java - eclipse中GUI界面异常

java - java中的列名无效,但适用于数据库

java递归: best collection of numbers

java - 读写对象链表

java - MouseListener 事件在 Java 中不起作用

java - 如何在数组中正确添加JTextField组件?

java - 锁定Excel工作表中的单元格值

java - optionType 在 JOptionPane OptionsDialog 中扮演什么角色?

java - 您的类应该实现 ActionListener 还是使用匿名 ActionListener 类的对象