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

时间:2016-06-10 13:53:53

标签: java swing login jframe actionlistener

我目前正在研究一个java应用程序,我刚刚创建了一个登录框架。它工作正常,但因为每次我想要访问第二个JFrame时我必须登录它发现我有点恼火,你不能通过在密码文本字段按Enter键登录。有没有办法让textfield使用与按钮相同的actionlistner?

这是我目前正在使用的代码。随意将它用于您自己的登录系统!

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

        }
    });

}

}

2 个答案:

答案 0 :(得分:3)

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

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

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

附录:

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

答案 1 :(得分:1)

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

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

ActionListener actionListener = e -> {};

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