循环不工作java

时间:2015-01-24 18:37:15

标签: java loops

我用Java创建一个LogIn程序。在努力工作之后,我差不多完成了,但我有最后一个问题。我希望我的程序在关闭之前只有3次登录尝试,事情是在你输入错误的传递之后,用户继续循环而没有用户再次输入用户名和密码,然后在3次循环后关闭。请帮助我。

package password;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Password extends JFrame {


public JPasswordField pass;
public JLabel usernameL,passwordL;
public JTextField usernameTF,passwordTF;
private JButton LogInB,CancelB;
private LogInButtonHandler logHandler;
private CancelButtonHandler canHandler;


public Password (){
    usernameL=new JLabel("Username");
    passwordL=new JLabel("Password");
    usernameTF=new JTextField(20);
    passwordTF=new JTextField(20);
   pass=new JPasswordField(10);


  pass.setEchoChar ('•');

    LogInB=new JButton("Log In");
    logHandler=new LogInButtonHandler();
    LogInB.addActionListener(logHandler);

    CancelB=new JButton("Cancel");
    canHandler=new CancelButtonHandler();
    CancelB.addActionListener(canHandler);

    setTitle("Log In");
    Container p=getContentPane();
    p.setLayout(new GridLayout(3,3));

    p.add(usernameL);
    p.add(usernameTF);
    p.add(passwordL);
    p.add(pass);
    p.add(LogInB);
    p.add(CancelB);



    setSize(300,200);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

    private class LogInButtonHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {


        String password=null;
        String user=null;
        boolean isValid;



       Scanner in= new Scanner(System.in);




        for(int i=0;i<3;i++)
        {

        try {
            if(i!=0)
            {
                JOptionPane.showMessageDialog(null,"Username/Password Inccorect Please Try Again: "+i,"Error",JOptionPane.ERROR_MESSAGE);
            }

            if(i==2)
            {
                JOptionPane.showMessageDialog(null,"This " +(++i) + "rd login will be your final attempt","Error",JOptionPane.ERROR_MESSAGE);
            }





            isValid=isValidCred(user,password);
            if(isValid)
            {
                JOptionPane.showMessageDialog(null,"Welcome "+user,"Succesful",JOptionPane.INFORMATION_MESSAGE );
                break;
            }

            if(i==3)
            {
                JOptionPane.showMessageDialog(null,"You had "+i+" failed attempts","Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (IOException ex) {

        }
        }

  System.exit(0);











    }



}



public boolean isValidCred(String user, String password) throws FileNotFoundException, IOException
{

    user = usernameTF.getText();
    password = pass.getText();
     File file=new File("C:\\login\\user.txt");
     File file2=new File("C:\\login\\pass.txt");

        BufferedReader in = new BufferedReader(new FileReader(file));
        String line=in.readLine();
        BufferedReader in2 = new BufferedReader(new FileReader(file2));
        String line2=in2.readLine();


    if (user.equals(line) && password.equals(line2))
    {
        return true;
    }
    else
    {
        return false;
    }
    }




private class CancelButtonHandler implements ActionListener{
    @Override
    public void actionPerformed (ActionEvent e){
        dispose();
    }
}

    public static void main(String[] args) {
        Password pass=new Password();
    }

}

1 个答案:

答案 0 :(得分:1)

您不希望actionPerformed LogInButtonHandler内的循环,因为该循环在第一次失败后不会接受新输入。

相反,每次按下登录按钮时,都应该增加一些成员变量。 actionPerformed的{​​{1}}将测试该变量的值,以确定向用户显示哪条消息。

Yuo可能需要这样的东西:

LogInButtonHandler