【问题标题】:Swing if else statement摆动 if else 语句
【发布时间】:2012-01-19 21:30:55
【问题描述】:

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

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

【问题讨论】:

  • 您能否提供完整的程序,您似乎在循环调用此功能! (作为主要部分)
  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) "Corerect" 不正确。使用拼写检查器。 3) “恼人的消息”的确切文本是什么?
  • "Corerect"?如果这突然出现在我身上,我会很生气。抱歉,无法抗拒讽刺。

标签: java swing actionlistener


【解决方案1】:

你把这一切都放在你的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();

        }
}

【讨论】:

  • 每次点击“Go to Shop”/shopBtn 按钮都会再次显示消息。
  • 可能是因为我使用了“!=”而不是 !equals()。我会改变它。如果更改后仍然不起作用,请替换 JOptionPane.showMessageDialog(null, "Corerect");与 JOptionPane.showMessageDialog(null, e.getSource());为了看看它叫什么。
  • 我没有真正解释清楚,因为我很累。我希望在用户成功登录后打开消息但只打开一次。然后会显示一个隐藏的按钮,当它被点击时,它会打开一个新框架并再次显示我不想要的“正确”消息。
  • 哦,我明白了。因此,您希望在单击 shopBtn 时显示表单,并且希望在输入正确的用户名和密码时显示对话框?好的。我将编辑我的答案。
  • 如果对的话,我应该放一个 else 吗?很好用,谢谢。
猜你喜欢
  • 2019-04-09
  • 2017-02-05
  • 2015-07-06
  • 2012-08-05
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多