【发布时间】:2014-01-17 13:10:33
【问题描述】:
我正在为游戏创建登录机制。如果用户输入了无效的用户名-密码组合,则会出现一个对话框,告诉他们他们的错误。但是当我在对话框中单击“确定”时,之前表单中的所有组件都变为非活动状态。
这是我的事件处理程序方法的代码:
//Event Handler
public void actionPerformed(ActionEvent e){
Scanner fileScan = null;
Scanner passwordScan = null;
String lineVar;
int lineCount=0;
//Opens the "Create Account" form
if (e.getSource()==CreateNew){
new CreateAccount();
}
//user tries to login
else if(e.getSource()==submit){
Inputuser = user.getText();
InputPass = Pass.getText();
try{
fileScan = new Scanner(new File(Inputuser + ".txt"));
filefound = true;
}
catch(FileNotFoundException ex){
JFrame FailureFrame = new JFrame("Something went wrong...");
JOptionPane.showMessageDialog(FailureFrame, "The username you have entered does not exist in our records. Please try again");
filefound=false;
}
//If the file was found (username exists)
if (filefound==true){
//Loops while the username while has more lines of content
while(fileScan.hasNext()){
lineVar = fileScan.next();
//Each line is considered a token
passwordScan = new Scanner(lineVar);
passwordScan.useDelimiter("/n");
while (passwordScan.hasNext()){
lineCount +=1;
if (lineCount == 2){
if (InputPass.equals( passwordScan.next() ) ){
JFrame successframe = new JFrame("Success!");
JOptionPane.showMessageDialog(successframe, "Login Successful!");
frame.dispose();
new MainProfile();
}
//If the password they entered is wrong
else{
JFrame notLogin = new JFrame ("Something went wrong...");
JOptionPane.showMessageDialog(notLogin, "You have entered invalid info. Please try again");
CompEnable();
}
}
}
}
}
}
}
【问题讨论】:
-
您能否发布一个可运行的示例,以便我们进行测试
-
另外,考虑使用模态 JDialog 而不是两帧,例如 this answer
-
我应该在我当前的工作中发布一个 zip 文件吗?
-
如果您希望人们阅读您的代码,请使用标准 Java 变量名。变量名不应以大写字符开头。我从来没有从教科书中看到过这样的教程或示例,所以不要编造自己的约定。遵守标准。
-
我被教过。无论如何,还可以。注意点