哪里错了真不知道了?
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class Denluo extends JFrame implements ActionListener
{
private JPanel pane,pan;
JLabel id,password;
private JTextField ID;
private JTextField Password;
private JButton dl,red;
private String zh="heilong";
private String mm="heilong";
Denluo()
{
super("登陆界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
id=new JLabel("帐号");
password=new JLabel("密码");
ID=new JTextField(10);
Password=new JTextField(10);
dl=new JButton("登陆");
red=new JButton("重填");
pan=new JPanel();
pan.setLayout(new GridLayout(3,2,10,10));
pan.add(id);
pan.add(ID);
pan.add(password);
pan.add(Password);
dl.addActionListener(this);
red.addActionListener(this);
pan.add(dl);
pan.add(red);
pan.setSize(300,300);
pane=new JPanel();
pane.add(pan,BorderLayout.CENTER);
setContentPane(pane);
}
public static void main(String args[])
{
JFrame frame=new Denluo();
frame.setSize(400,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==dl)
{
if(ID.getText().trim()==zh && Password.getText().trim()==mm)
System.out.println("欢迎进入!");
else
System.out.println("对不起,你帐号或密码错误!请重填!"); //只出现这句语,就是输入heilong heilong 都不行的
}
if(e.getSource()==red)
{
ID.setText("");
Password.setText("");
}
}
}
----------------解决方案--------------------------------------------------------
比较字符串不能用==
要用equals
----------------解决方案--------------------------------------------------------
将 if(ID.getText().trim()==zh && Password.getText().trim()==mm)
改为 if(ID.getText().trim().equals(zh) && Password.getText().trim().equals(mm))
----------------解决方案--------------------------------------------------------