如何测试来自 JPanel 的用户输入?

How can one test user input from a JPanel?

我正在尝试使用 SQL (mySQL workbench) 和 Java (Eclipse) 构建一个简单的登录数据库。我不确定如何针对 SQL 中的数据库测试字符串输入(JTextField 和 JPasswordField),甚至针对任何其他字符串。

我希望 Java 在登录凭据等于指定字符串时打印出一条语句。这是我当前的代码:

import javax.swing.*;
import java.sql.*;


public class Hello1 extends JFrame {




private static final long serialVersionUID = 1487932324102279819L;


public static void main(String[] args) {

    JFrame frame = new JFrame("Frame Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(350,200);



    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);

    String username0 = "java";
    String password = "password";

    Statement stmt = null;
    try{
        String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
        Connection connection = DriverManager.getConnection(url, username0, password);
        stmt = connection.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery("SELECT * from userids ");
        while(rs.next()) {
            String user = rs.getString("username");
            String pass = rs.getString("paswrd");
            System.out.println(user);
            System.out.println(pass);
        }
        connection.close();

        }

        catch (Exception e) {
             System.err.println("Got an exception! ");
             System.err.println(e.getMessage());
        }
     }



private static void placeComponents(JPanel panel) {

    panel.setLayout(null);
    JLabel userLabel = new JLabel("Username");
    userLabel.setBounds(10,20,80,25);
    panel.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(100, 20, 165, 25);
    panel.add(userText);
    String user = userText.getText();       

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

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(100,50,165,25);
    panel.add(passwordText);
    String pass = passwordText.getSelectedText();

    if(user.equals('b') && pass.equals('t')){
        System.out.println("Correct Login");
    }
    System.out.println(pass);

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


}

}

我尝试使用 .getText() 和 .getSelectedText() 方法获取每个 userText/passwordText 中的文本。

您应该在准备好的语句中将用户名和密码作为参数传递并执行它,然后使用以下方法检查是否存在匹配项:

public void login(String user,String pass){
    String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
    Connection connection = DriverManager.getConnection(url, username0, password);
    PreparedStatement stmt = connection.prepareStatement("SELECT * from userids where username=? and password=? ");
    stmt.setString(1, user);
    stmt.setString(2, pass);
    ResultSet rs= stmt.executeQuery();

    if(rs.hasNext()){
    //continue with operation
    }else{
    //show user doesn't exist
    }
}

然后给按钮添加事件:

loginB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
String user = userTextBox.getText().toString();
char[] pass = passwordText.getPassword();
            login(user,pass);
        }
    });

如果我没理解错,那么你需要在点击Login按钮时获取密码。

初始化按钮后,请在placeComponents方法中添加以下代码。

loginB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            char[] pass = passwordText.getPassword();
            System.out.println(pass);
        }
    });

动作侦听器可能是最容易实现且最常见的事件处理程序。您实现一个动作监听器来定义当用户执行特定操作时应该做什么。

您可以阅读有关 ActionListener 的更多信息 here

您可以参考this example 了解如何使用密码字段。