IF 语句在简单的登录程序中不起作用

IF statement not working in a simple login programme

您好,我正在 Java 中编写一个简单的登录程序,我遇到了问题,即使输入了正确的帐号和密码,我仍然得到 'Please enter the correct Account number/ Password',但我似乎看不到错误是

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

    public class Main2 extends JFrame{

        public static int x=0;

        public JPanel panel;
        String accountnumber = "123456";
        JTextField inputUser;
        String password = "admin";  
        JPasswordField inputPass;  

        public JLabel lblctr;

        public Main2(){ 
            setTitle("Login");
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(500, 450, 500, 500);
            panel = new JPanel();
            setContentPane(panel);
            panel.setLayout(null);

            inputUser = new JTextField();
            inputUser.setBounds(130, 30,125, 20);
            panel.add(inputUser);
            inputUser.setColumns(10);

            inputPass = new JPasswordField();
            inputPass.setBounds(130, 60, 125, 20);
            panel.add(inputPass);
            inputPass.setColumns(10);

            lblctr = new JLabel("Attempts:");
            lblctr.setBounds(80, 135, 72, 17);
            panel.add(lblctr);

            JButton cancelbtn = new JButton("Cancel");
            cancelbtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    System.exit(0);
                }
});

            JButton loginbtn = new JButton("Login");

            loginbtn.addActionListener(new ActionListener(){
                @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0){

                    if((inputUser.getText().equals(accountnumber)) && (inputPass.getPassword().equals(password))){
                        JOptionPane.showMessageDialog(null,"Press OK to Continue!","Login success!",JOptionPane.INFORMATION_MESSAGE);
                    }
                    else{
                        x++;
                        if(x>3){
                            JOptionPane.showMessageDialog(null,"Too many attempts","Access Denied!",JOptionPane.ERROR_MESSAGE);
                            System.exit(0);
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"Please input correct Account Number/Password","Login Failed!",JOptionPane.ERROR_MESSAGE);
                        }
                    }
                    lblctr.setText("Attempts : "+x);
                }});

            cancelbtn.setBounds(170,105, 89, 21);
            panel.add(cancelbtn);

            loginbtn.setBounds(50,105, 89, 21);
            panel.add(loginbtn);

            JLabel lbluser = new JLabel("Account Number:");
            lbluser.setBounds(12, 34, 100, 10);
            panel.add(lbluser);

            JLabel lblpass = new JLabel("Password:");
            lblpass.setBounds(50, 62, 72, 17);
            panel.add(lblpass);
        }

        public static void main (String[]args){
            Main2 proj = new Main2();
            proj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
            proj.setVisible(true);
            proj.setSize(320,190);
            proj.setLocationRelativeTo(null);
        }
}

JPasswordField#getPassword() returns 一个字符数组,而不是一个字符串,因此直接比较 "password" 和 inputPass.getPassword() 注定会失败。使用 Arrays 方法比较 char 数组,而不是 equals(...) 方法。

此外,user7 是正确的,您正尝试将密码变量名称用作字符串,这也会失败。去掉引号。

例如,

if (Arrays.equals(password.toCharArray(), inputPass.getPassword()))

其他问题:

  • 永远不要在你的代码中有这个:@SuppressWarnings("deprecation")。该代码已被弃用,原因非常充分,不应使用。而是转到 API 查看代码为何被弃用,并使用 API 建议的替代方法。
  • 您应该避免使用空布局和 setBounds(...),因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新并保持。而是使用布局管理器来帮助您创建美观、实用且易于维护的 GUI。