-1
나는 자바로 로그인 프로그램을 만들고있다. 나는 거의 열심히 일한 후에 거의 끝났지 만 마지막 문제가 하나있다. 내 프로그램이 닫히기 전에 3 번의 로그인 시도를 원했고, 잘못된 패스를 입력 한 후 사용자가 사용자 이름과 암호를 다시 입력하지 않고 루핑을 계속 한 다음 3 회 반복 후 사용자가 종료합니다. 도와주세요. 그 루프가 첫 번째 실패 후 새로운 입력을 받아 들일 것이기 때문에Looping Not Working 자바
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();
}
}
이것은 올바른 대답을하고, 그래서 나는 왜 아래쪽 투표 확실하지 않다. 상향 투표에 반대했다. –
제발 할 수있는 방법에 대한 예를 들어 주시겠습니까? 나는 정말로 이해할 수 없다. –
@PatrickLouisFerrer 제 편집을 참조하십시오. – Eran