的Java Swing GUI登录动作侦听器错误

汇率:

你好我试图使用原始编码创建Java GUI登录但是我遇到了Java的动作监听过程中一个错误,不检查登录系统是否有要求,发送文本。

这是它应该如何运行:

add username label and text 
add password label and text
add success label and text
checks username text input *
checks password text and input *
send success label to the JLabel *

*这个标记是完全不工作的三个检查。第3确实出现,但最后3根本不会出现。

这是我试图运行代码

button.addActionListener(this);
button.addActionListener(new Login());

我已经使用这个两种方法尝试,它似乎并不工作。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Login implements ActionListener {

    private JFrame frame;
    private JPanel panel;
    private JLabel username;
    private JTextField user;
    private JLabel password;
    private JPasswordField pass;
    private JButton button;
    private JLabel success;

    public Login(){
        frame = new JFrame();
        panel = new JPanel();

        frame.setSize(350,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        panel.setLayout(null);

        username = new JLabel("User");
        username.setBounds(10,20,200,25);
        panel.add(username);

        user =  new JTextField(20);
        user.setBounds(100,20,200,25);
        panel.add(user);

        password = new JLabel("Password");
        password.setBounds(10,50,80,25);
        panel.add(password);

        pass = new JPasswordField(20);
        pass.setBounds(100,50, 200 ,25);
        panel.add(pass);

        button = new JButton ("Login");
        button.setBounds(10,80,80,25);
        button.addActionListener(this);
        panel.add(button);

        success = new JLabel(""); 
        success.setBounds(10, 110, 300, 25);
        panel.add(success);


        frame.setVisible(true);
    }
    public static void main (String[] args ){
        new Login();
    }
    @Override
    public void actionPerformed(ActionEvent e){
        String user = username.getText();
        String password = pass.getText();

       if (user.equals("Someone") && password.equals("Someone")){
            success.setText("Success");
        }
    }
}

有没有什么办法解决它

安德鲁·汤普森:

一个小的调试将表明,在代码actionPerformed(..)方法获取用户标签,而不是文本字段的文本。

更改:

//String user = username.getText();
String userString = user.getText();

和:

//if (user.equals("Someone") && password.equals("Someone")) {
if (userString.equals("Someone") && password.equals("Someone")) {

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章