为什么我用户名、密码 正确输入了admin和123,逻辑上判断是false?
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=textField.getText();
char ch[]=passwordField.getPassword();
String pass=new String(ch);
/*
boolean bool1= str=="admin";
boolean bool2= pass=="123";
System.out.println(bool1);
System.out.println(bool2);
*/
if (str=="admin" || pass=="123" ) {
mainpage p=new mainpage();
dispose();
}
else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!","登陆失败!",JOptionPane.PLAIN_MESSAGE);
}
}
});
不要用==对比,完后str=="admin" || pass=="123" 应该是&&
字符串的判断不是用==而是用equals,应用str.equals("admin")&& pass.equals("123"),登陆的话用户名和密码同时正确才能登陆的。