本人新手,想制作一个简单的登录程序(用JFrame实现),但判断输入语句与正确密码是否相符时出现问题,输入正确密码但判断为false,求解。
代码如下:
/**
*
*/
package jinjie;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
/**
* @author ZANE
*
*/
public class 密码登录 extends JFrame {
private static final long serialVersionUID = -8501458415481947988L;
private JPanel contentPane;
private JTextField textField1;
private JTextField textField2;
private JLabel label1;
private JLabel label2;
private String name;
private String password;
private boolean istrue1;
private boolean istrue2;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
密码登录 frame = new 密码登录();
frame.setVisible(true);
}catch(Exception e) {
e.printStackTrace();
}
}
});
}
public 密码登录() {
name = "Zane";
password = "Zane091101";
setTitle("密码登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,325,165);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(3,1,5,5));
JPanel panel1 = new JPanel();
contentPane.add(panel1);
label1 = new JLabel("用户名:");
panel1.add(label1);
textField1 = new JTextField();
textField1.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
do_textField1_keyReleased(e);
}
});
panel1.add(textField1);
textField1.setColumns(18);
JPanel panel2 = new JPanel();
contentPane.add(panel2);
label2 = new JLabel("密码:");
panel2.add(label2);
textField2 = new JTextField();
textField2.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e1) {
do_textField2_keyReleased(e1);
}
});
panel2.add(textField2);
textField2.setColumns(18);
JPanel panel3 = new JPanel();
contentPane.add(panel3);
JButton button = new JButton("登录");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e2) {
do_button_action_actionPerformed(e2);
}
});
panel3.add(button);
}
protected void do_textField1_keyReleased(KeyEvent e) {
String text = textField1.getText();
if (text == name) {
istrue1 = true;
System.out.println("istrue1 = " + istrue1 + ":" + text + " =? " + name);
}else {
istrue1 = false;
System.out.println("istrue1 = " + istrue1 + ":" + text + " =? " + name);
}
}
protected void do_textField2_keyReleased(KeyEvent e) {
String text = textField2.getText();
if(text == password) {
istrue2 = true;
System.out.println("istrue2 = " + istrue2 + ":" + text + " =? " + password);
}else {
istrue2 = false;
System.out.println("istrue2 = " + istrue2 + " : " + text + " =? " + password);
}
}
protected void do_button_action_actionPerformed(ActionEvent e) {
if (istrue1 == true&&istrue2 == true) {
JOptionPane.showMessageDialog(this,"登录成功",null,JOptionPane.CLOSED_OPTION);
}else if (istrue2 == false&&istrue1 == false){
JOptionPane.showMessageDialog(this,"请输入正确用户与密码",null,JOptionPane.WARNING_MESSAGE);
}else if (istrue1 == false){
JOptionPane.showMessageDialog(this,"请输入正确用户",null,JOptionPane.WARNING_MESSAGE);
}else if (istrue2 == false){
JOptionPane.showMessageDialog(this,"请输入正确密码",null,JOptionPane.WARNING_MESSAGE);
}
}
}
PS:重点问题部分:
protected void do_textField1_keyReleased(KeyEvent e) {
String text = textField1.getText();
if (text == name) {
istrue1 = true;
System.out.println("istrue1 = " + istrue1 + ":" + text + " =? " + name);
}else {
istrue1 = false;
System.out.println("istrue1 = " + istrue1 + ":" + text + " =? " + name);
}
}
protected void do_textField2_keyReleased(KeyEvent e) {
String text = textField2.getText();
if(text == password) {
istrue2 = true;
System.out.println("istrue2 = " + istrue2 + ":" + text + " =? " + password);
}else {
istrue2 = false;
System.out.println("istrue2 = " + istrue2 + " : " + text + " =? " + password);
}
}
protected void do_button_action_actionPerformed(ActionEvent e) {
if (istrue1 == true&&istrue2 == true) {
JOptionPane.showMessageDialog(this,"登录成功",null,JOptionPane.CLOSED_OPTION);
}else if (istrue2 == false&&istrue1 == false){
JOptionPane.showMessageDialog(this,"请输入正确用户与密码",null,JOptionPane.WARNING_MESSAGE);
}else if (istrue1 == false){
JOptionPane.showMessageDialog(this,"请输入正确用户",null,JOptionPane.WARNING_MESSAGE);
}else if (istrue2 == false){
JOptionPane.showMessageDialog(this,"请输入正确密码",null,JOptionPane.WARNING_MESSAGE);
}
}
} //代码可以运行,但效果与预期不符