摆动if else语句 [英] Swing if else statement

查看:96
本文介绍了摆动if else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an annoying message box that pops up when I dont want it. the problem occurs after a user logs on and the hidden button is visible, but when it is clicked it show the "correct" message again? Ive also tried putting it on top of the first statement and bottom. The annoying message is the message that comes up after a successful login, and no I dont need to spell check,

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 方法中。每当执行任何操作,单击一个按钮,编辑一个文本字段等时,即会调用该方法。

You put this all in your actionPerformed method. That is called whenever any action is performed, a button is clicked, a text field is edited, etc...

如果您不希望在执行此操作时运行单击按钮,使用 getSource()检查事件的来源,如果来源是按钮,则不要运行代码。您的方法如下所示:

If you don't want this to run when you click the button, check for the source of the event, using getSource(), and if the source is the button, then don't run the code. Your method would look like this:

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

        }
}

这篇关于摆动if else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆