能不能帮忙看下这个代码啊?我输入正确用户名和密码的时候为什么还说我错误呢?哪里有错啊???万分感谢!!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class One implements ActionListener
{
JFrame f;
JPanel p1,p2,p3,p4,p5;
JLabel l1,l2;
JButton B1,B2;
JTextField tf1,tf2;
JPasswordField pf;
public static void main(String args[])
{
One one=new One();
one.go();
} public void go()
{
f=new JFrame();
f.setSize(300,300);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
l1=new JLabel("用户ID");
l2=new JLabel("用户口令");
B1=new JButton("登录");
B2=new JButton("清除");
tf1=new JTextField(30);
tf2=new JTextField(30);
pf=new JPasswordField(30); p5.setLayout(new GridLayout(2,2));
p5.add(l1);
p5.add(tf1);
p5.add(l2);
p5.add(pf);
p3.add(B1);
p3.add(B2);
p4.add(tf2);
f.getContentPane().add(p5,BorderLayout.NORTH);
f.getContentPane().add(p3,BorderLayout.CENTER);
f.getContentPane().add(p4,BorderLayout.SOUTH);
f.setVisible(true);
B1.addActionListener(this);
B2.addActionListener(this);
tf2.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
JButton xb=(JButton)e.getSource();
if(xb==B1)
{
String username=tf1.getText();
if(username!="javaclass")
tf2.setText("用户ID或用户口令不正确!");
else
{
char[]pw=pf.getPassword();
username=new String(pw);
if(username!="200905")
tf2.setText("用户ID或用户口令不正确!");
else
tf2.setText("登录成功!");
}
}
else
{
tf1.setText("");
pf.setText("");
}
}}

解决方案 »

  1.   

    程序我还没调,但先指出一个问题:字符串比较,用equals,而不是==!username.equals("200905")
      

  2.   

    果然是这个问题,重点看判等的地方:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.BorderLayout;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    public class One implements ActionListener
    {
        JFrame f;
        JPanel p1,p2,p3,p4,p5;
        JLabel l1,l2;
        JButton B1,B2;
        JTextField tf1,tf2;
        JPasswordField pf;
        public static void main(String args[])
        {
            One one=new One();
            one.go();
        }    public void go()
        {
            f=new JFrame();
            f.setSize(300,300);
            p1=new JPanel();
            p2=new JPanel();
            p3=new JPanel();
            p4=new JPanel();
            p5=new JPanel();
            l1=new JLabel("用户ID");
            l2=new JLabel("用户口令");
            B1=new JButton("登录");
            B2=new JButton("清除");
            tf1=new JTextField(30);
            tf2=new JTextField(30);
            pf=new JPasswordField(30);        p5.setLayout(new GridLayout(2,2));
            p5.add(l1);
            p5.add(tf1);
            p5.add(l2);
            p5.add(pf);
            p3.add(B1);
            p3.add(B2);
            p4.add(tf2);
            f.getContentPane().add(p5,BorderLayout.NORTH);
            f.getContentPane().add(p3,BorderLayout.CENTER);
            f.getContentPane().add(p4,BorderLayout.SOUTH);
            f.setVisible(true);
            B1.addActionListener(this);
            B2.addActionListener(this);
            tf2.setEditable(false);
        }
            public void actionPerformed(ActionEvent e)
            {
                    JButton xb=(JButton)e.getSource();
                    if(xb==B1)
                    {
                        String username=tf1.getText();
                        System.out.println(username);
                        if(!username.equals("javaclass"))
                            tf2.setText("用户ID或用户口令不正确!");
                        else
                        {
                            char[]pw=pf.getPassword();
                            username=new String(pw);
                            System.out.println(username);
                            if(!username.equals("200905"))
                                tf2.setText("用户ID或用户口令不正确!");
                            else
                                tf2.setText("登录成功!");
                        }
                    }
                    else
                    {
                        tf1.setText("");
                        pf.setText("");
                    }
            }}
      

  3.   

    将if(username!="200905") 改为 if("200905".equals(usename))