题目是:实现用户根据自己的密码进入界面,如果用户信息正确,则输出“欢迎××用户”,否则用户还可以尝试两次,如果两次以后还是错误,则输出“请输入正确信息”,如果尝试失败,则退出我在编写如下代码以后运行得到的结果只显示了一个空白的框架,其他的什么东西都没有,各位帮我看看是怎么回事,谢了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Password extends JFrame implements ActionListener {
        
        JLabel userLabel,passwordLabel;
        JTextField userText;
        JPasswordField passwordText;
        
        int count=1;
 
    public Password() {
     super("Please input");
     Container cont=new Container();
     cont.setLayout(new FlowLayout());
    
     userLabel=new JLabel();
     passwordLabel=new JLabel();
     userText=new JTextField();
     passwordText=new JPasswordField();
     passwordText.addActionListener()
    
     cont.add(userLabel);
     cont.add(userText);
     cont.add(passwordLabel);
     cont.add(passwordText);
    
     setSize(240,100);
     setVisible(true);
    }
        
    public void actionPerformed(ActionEvent e){
     String userName=new String("chensan");
     String password=new String("12345678");
    
     if(e.getSource()==passwordText){
     count++;
     char []passwords=passwordText.getPassword();
     if(userText.getText().equals(userName)&&password.equals(new String(passwords))){
     JOptionPane.showMessageDialog(null,"welcome"+userName);
     System.exit(0);
     }
    
     else if(count>3){
     System.exit(0);
     }
    
     else
     JOptionPane.showMessageDialog(null,userText.getText()+"Please input again");
     }
    }
    
    public static void main(String[] args) {
        Password pass=new Password();
        pass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
    }
}

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Password extends JFrame implements ActionListener {
            
            JLabel userLabel,passwordLabel;
            JTextField userText;
            JPasswordField passwordText;
            JButton btn;
            
            int count=1;
     
        public Password() {
         super("Please input");
         Container cont=new Container();
         cont.setLayout(new FlowLayout());
        
         userLabel=new JLabel("用户名");
         passwordLabel=new JLabel("密码");
         userText=new JTextField(8);
         passwordText=new JPasswordField(8);
         btn = new JButton("OK");
         btn.addActionListener(this);
         //passwordText.addActionListener()
        
         cont.add(userLabel);
         cont.add(userText);
         cont.add(passwordLabel);
         cont.add(passwordText);
         cont.add(btn);
         this.setContentPane(cont);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setSize(240,100);
         setVisible(true);
        }
            
        public void actionPerformed(ActionEvent e){
         String userName=new String("chensan");
         String password=new String("12345678");
        
    count++;
    char []passwords=passwordText.getPassword();
    if(userText.getText().equals(userName)&&password.equals(new String(passwords))){
    JOptionPane.showMessageDialog(null,"welcome"+userName);
    System.exit(0);
    }
    else if(count>3){
    System.exit(0);
    }
    else
    JOptionPane.showMessageDialog(null,userText.getText()+"Please input again");
        }
        
        public static void main(String[] args) {
            Password pass=new Password();
            pass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
        }
    }
      

  2.   

    作  者:  karsinhe (发奋图强!)
    发表时间:  2007-05-07 01:18:19  
    ===
    楼主强
      

  3.   

    再添加一个按钮JButton button;
    在构造方法里面加上button = new Jbutton("验证");
    把构造方法里的
     userLabel=new JLabel();
        passwordLabel=new JLabel();
        userText=new JTextField();
        passwordText=new JPasswordField();改成
    userLabel=new JLabel("用户名");
        passwordLabel=new JLabel("密码");
        userText=new JTextField(8);
        passwordText=new JPasswordField(8);
    把passwordText.addActionListener()改成按钮的消息映射,button.addActionListener(this);
    再把事件处理中的if(e.getSource()==passwordText)改成if(e.getSource()==button)
    具体改完以后是这样的:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Password extends JFrame implements ActionListener {
            
            JLabel userLabel,passwordLabel;
            JTextField userText;
            JPasswordField passwordText;
            JButton btn;
            
            int count=1;
     
        public Password() {
        super("Please input");
        Container cont=new Container();
        cont.setLayout(new FlowLayout());
        
        userLabel=new JLabel("用户名");
        passwordLabel=new JLabel("密码");
        userText=new JTextField(8);
        passwordText=new JPasswordField(8);
        btn = new JButton("验证");
        btn.addActionListener(this);
        cont.add(userLabel);
        cont.add(userText);
        cont.add(passwordLabel);
        cont.add(passwordText);
        cont.add(btn);
        add(cont);
        setSize(240,100);
        setVisible(true);
        }
            
        public void actionPerformed(ActionEvent e){
        String userName=new String("chensan");
        String password=new String("12345678");
        
        if(e.getSource()==btn){
        count++;
        char []passwords=passwordText.getPassword();
        if(userText.getText().equals(userName)&&password.equals(new String(passwords))){
        JOptionPane.showMessageDialog(null,"welcome"+userName);
        System.exit(0);
        }
        
        else if(count>3){
        System.exit(0);
        }
        
        else
        JOptionPane.showMessageDialog(null,userText.getText()+"Please input again");
        }
        }
        
        public static void main(String[] args) {
            Password pass=new Password();
            pass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
        }
    }
      

  4.   

    在JOptionPane.showMessageDialog(null,userText.getText()+"Please input again");后面加上:userText.setText(null);
        passwordText.setText(null);