为什么条件判断结果与预期不符?

本人新手,想制作一个简单的登录程序(用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);
        }
    }
}                                    //代码可以运行,但效果与预期不符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值