如何将按钮 actionlistner 绑定到文本字段

How to bind a button actionlistner to a textfield

我目前正在开发一个 java 应用程序,我刚刚制作了一个登录框架。它工作正常,但由于每次我想访问第二个 JFrame 时都必须登录,所以我突然想到,您无法通过在密码文本字段上按 enter 来登录,这有点令人恼火。有没有办法让文本字段使用与按钮相同的动作列表?

这是我目前使用的代码。欢迎将其用于您自己的登录系统!

package presentation;

/**
 *
 * @author Jessie den Ridder
 */


import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import presentation.ScreenInfoFrame;

public class MyLogin {

private JFrame frame = new JFrame("Login");
private final JLabel inputLabel1 = new JLabel("Gebruikersnaam");
private final JLabel inputLabel2 = new JLabel("Wachtwoord");
private JTextField input1 = new JTextField();
private JPasswordField input2 = new JPasswordField();
private final JButton button = new JButton("Login");
private final JLabel inputLabel3 = new JLabel("");

public MyLogin() {

    inputLabel1.setBounds(850, 405, 180, 20);
    input1.setBounds(1000, 400, 180, 30);

    inputLabel2.setBounds(850, 455, 180, 20);
    input2.setBounds(1000, 450, 180, 30);

    button.setBounds(1000, 520, 180, 30);
    frame.getContentPane().add(button);
    frame.getContentPane().add(inputLabel1);
    frame.getContentPane().add(input1);
    frame.getContentPane().add(input2);
    frame.getContentPane().add(inputLabel2);
    frame.getContentPane().add(inputLabel3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            String sql = "SELECT id, userName, password, firstName, lastName FROM employee ;";
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = (Connection) DriverManager.getConnection(
                        "Database", "user", "Password");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                String user = input1.getText();

                String pwd = (input2.getText());
                while (rs.next()) {
                    String uname = rs.getString("userName");
                    //Username is the coloumn name in the database table 
                    String password = rs.getString("password");
                    if ((user.equals(uname)) && (pwd.equals(password))) {
                        frame.dispose();
                        ScreenInfoFrame ui = new ScreenInfoFrame();
                        ui.setVisible(true);
                    }
                }
            } catch (ClassNotFoundException | SQLException k) {
                 JOptionPane.showMessageDialog(null, k.getMessage());
            }

        }
    });

}

}

创建一个动作侦听器,而不是向按钮添加新的动作侦听器

ActionListener myActionListener = new ActionListener() {
    // Action listener body here
}

然后将其添加到具有 button.addActionListener(myActionListener);input2.AddActionListener(myActionListener); 的元素中。

附录:

作为旁注,我建议不要给您的组件起通用名称,例如 buttoninput#,因为它们很难辨别它们的作用。选择更具体的名称,例如 passwordFieldsubmitButton.

您需要单独创建 ActionListener,然后将其添加到文本字段和按钮中:

JTextField textField = new JTextField();
JButton button = new JButton();

ActionListener actionListener = e -> {};

textField.addActionListener(actionListener);
button.addActionListener(actionListener);