未检测到文本字段和密码字段

TextField and Password field not being detected

需要帮助了解未检测到文本字段和密码字段的原因吗?所以这是我看到错误的地方(txtUsername 和 PW 上的红线)

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(txtUsername.getText().length()==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else if(PW.getPassword().length==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else {
        String user = txtUsername.getText();
        char[] pass = PW.getPassword();
        String pwd = String.copyValueOf(pass);
        if(validate_login(user,pwd))
            JOptionPane.showMessageDialog(null, "Correct Login Credentials!");
        else 
            JOptionPane.showMessageDialog(null,"Incorrect Login Credentials!");
    }
}

在我输入这段代码之前:

private boolean validate_login (String username, String password) {
    try {
            Class.forName(".com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/logins" + "user=root&password=");
            PreparedStatement pst = con.prepareStatement("Select * from admin where username=? and password=?");
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs = pst.executeQuery();
            if(rs.next()) {
                return true;
            }
            else
                return false;
    }
      catch(SQLException e) {
        e.printStackTrace();
        return false;
    }    

    }
}

没有任何错误。现在它给了我很多错误,因为我已经输入了该代码,但该方法本身并没有错误。你能帮我么?谢谢。

在您的validate_login方法中,有不必要的大括号。移除底部支架

public class Login extends javax.swing.JFrame {

public Login() {
    initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    txtUsername = new javax.swing.JTextField();
    PW = new javax.swing.JPasswordField();
    btnCancel = new javax.swing.JButton();
    btnLogin = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(0, 204, 153));
    jPanel1.setForeground(new java.awt.Color(255, 0, 0));

    jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Password: ");

    jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel2.setText("Username: ");

    txtUsername.setFont(new java.awt.Font("Trebuchet MS", 0, 12)); // NOI18N
    txtUsername.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtUsernameActionPerformed(evt);
        }
    });

    btnCancel.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    btnCancel.setText("Cancel");
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelActionPerformed(evt);
        }
    });

    btnLogin.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    btnLogin.setText("Login");
    btnLogin.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLoginActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(27, 27, 27)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(btnLogin)
                            .addGap(61, 61, 61)
                            .addComponent(btnCancel))
                        .addComponent(PW)))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(txtUsername)))
            .addContainerGap(32, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(91, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(PW, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnCancel)
                .addComponent(btnLogin))
            .addGap(91, 91, 91))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(txtUsername.getText().length()==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else if(PW.getPassword().length==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else {
        String user = txtUsername.getText();
        char[] pass = PW.getPassword();
        String pwd = String.copyValueOf(pass);
        if(validate_login(user,pwd))
            JOptionPane.showMessageDialog(null, "Correct Login Credentials!");
        else 
            JOptionPane.showMessageDialog(null,"Incorrect Login Credentials!");
    }
}