来自JTextField的Java getText()返回空字符串,同时其中包含文本

时间:2016-01-19 20:07:01

标签: java

我正在尝试创建一个登录表单但是当我尝试从文本字段中获取用户名文本和密码文本时,它将返回为空字符串。

public class Login extends JPanel {

private JLabel lblUsername;
private JLabel lblPassword;
private JButton btnLogin;
private JTextField txtUsername;
private JPasswordField txtPassword;

public void paintComponent(Graphics g) {

    super.paintComponent(g);

    setLayout(null);

    lblUsername = new JLabel("Username:");
    lblUsername.setSize(80,20);
    lblUsername.setLocation(80,40);

    lblPassword = new JLabel("Password:");
    lblPassword.setSize(50,20);
    lblPassword.setLocation(80,80);

    txtUsername = new JTextField();
    txtUsername.setSize(150,20);
    txtUsername.setLocation(160,40);

    txtPassword = new JPasswordField();
    txtPassword.setSize(150,20);
    txtPassword.setLocation(160,80);

    btnLogin = new JButton("Login");
    btnLogin.setSize(150,30);
    btnLogin.setLocation(120,120);

    btnLogin.addActionListener(new ActionListener()
    {
      @SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent e)
      {   
        System.out.println("Username " + txtUsername.getText() + " - Password: " + txtPassword.getText());
        sqlFunctions database = new sqlFunctions();

        if (database.checkAdminLogin(txtUsername.getText(), txtPassword.getText()) == true) {
            System.out.println("Login success");
            Frame.hideLogin();
        } else {
            JOptionPane.showMessageDialog(null, "Login is niet gelukt, probeer opnieuw.");
        }

      }
    });

    add(lblUsername);
    add(lblPassword);
    add(txtUsername);
    add(txtPassword);
    add(btnLogin);



}

}

我以前在这个项目中遇到过这个问题。当我尝试使用buttonhandler处理多个按钮并使用if case和e.getSource()时,它永远不会识别单击了哪个按钮。

感谢您的帮助:)

0 个答案:

没有答案