我的代码遇到了问题。我的代码的逻辑是,当它找不到我输入的用户名时,会出现一个确认框,真正发生的是当在一行中找到输入的用户名时 它没有启动确认框“没关系”但它继续搜索每一行并初始化其他{statement}顺便说一句,我的else语句是确认框。
我的代码写的示例data.dat
shawn | qwerty1234
cloe | password1234
jones | shittybrix
当我输入“shawn”时,它会启动我的if语句“那没关系” 但它继续搜索整个字符串,输入的用户名“shawn”变得不等于cloe和jones,它启动else语句,分别给我两个确认框。 我想避免这个错误,但我不知道如何。 REGISTER.JAVA中的错误发生
这是我的登录代码:
package login;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JOptionPane;
public class Login extends javax.swing.JFrame {
public Login() {
initComponents0();
}
@SuppressWarnings("unchecked")
private void initComponents0() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
uname = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
login = new javax.swing.JButton();
reset = new javax.swing.JButton();
pwd = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel1.setText("Login Pane");
jLabel2.setText("User Name:");
jLabel3.setText("Password:");
login.setText("Login");
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String un = uname.getText();
try {
try (BufferedReader br = new BufferedReader(new Filereader("data.dat"))) {
String strLine;
while ((strLine = br.readLine()) != null) {
if (strLine.startsWith(un)) {
JOptionPane.showMessageDialog(null, "Hello: " + un, "Registration", JOptionPane.INFORMATION_MESSAGE);
} else {
int sel = JOptionPane.showConfirmDialog(null, "It seems that you haven't registered yet? \n Lunch Registration Pane?", "Admin", JOptionPane.INFORMATION_MESSAGE);
if (sel == JOptionPane.YES_OPTION) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Register().setVisible(true);
}
});
}
}
}
}
} catch (IOException | HeadlessException ez) {
JOptionPane.showMessageDialog(null, "A null file was created in order to \n avoid File Catch errors", "Admin", JOptionPane.INFORMATION_MESSAGE);
File file = new File("data.dat");
try {
try (FileWriter writer = new FileWriter(file, true)) {
String data0 = "null";
String data1 = "null";
writer.write(data0 + " | " + data1 + "\n");
}
} catch (IOException | HeadlessException z) {
JOptionPane.showMessageDialog(null, e);
}
}
}
});
reset.setText("Reset Field");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(pwd))))
.addContainerGap(30, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(login)
.addComponent(reset))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
pack();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton login;
private javax.swing.JPasswordField pwd;
private javax.swing.JButton reset;
private javax.swing.JTextField uname;
// End of variables declaration
}
这是我在确认框中单击是时启动的注册码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package login;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
/**
*
* @author Jfetizanan
*/
class Register extends javax.swing.JFrame {
/**
* Creates new form GUIREG
*/
public Register() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
unamereg = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
pwdreg = new javax.swing.JTextField();
submit = new javax.swing.JButton();
resetreg = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel1.setText("Registration Pane");
jLabel2.setText("User Name:");
jLabel3.setText("Password:");
submit.setText("Submit");
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Runned");
File file = new File("data.dat");
try {
try (FileWriter writer = new FileWriter(file, true)) {
String data0 = unamereg.getText();
String data1 = pwdreg.getText();
writer.write(data0 + " | " + data1 + "\n");
}
JOptionPane.showMessageDialog(null, "Registered", "Registration", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException | HeadlessException z) {
JOptionPane.showMessageDialog(null, e);
}
}
});
resetreg.setText("Reset Field");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(unamereg))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pwdreg)
.addGroup(layout.createSequentialGroup()
.addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(resetreg, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))))
.addContainerGap(42, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(unamereg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(pwdreg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(submit)
.addComponent(resetreg))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
pack();
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField pwdreg;
private javax.swing.JButton resetreg;
private javax.swing.JButton submit;
private javax.swing.JTextField unamereg;
// End of variables declaration
}
答案 0 :(得分:3)
管理通过这个大混乱找到问题。在你的if语句弹出说'helh',你需要让它摆脱while循环,然后移动else语句。在while语句中复制什么,只是因为它与第一个人不匹配,并不意味着它不会与后一个匹配,所以不要对它们直到最后都大喊大叫。所以它是这样的:
BufferedReader br = new BufferedReader(
new InputStreamReader(fstream));
String strLine;
boolean registered = false;
while ((strLine = br.readLine()) != null) {
if (strLine.startsWith(un)) {
registered = true;
break;
}
}
if(registered) {
//blah
} else {
//blah
}
此外,您应该将午餐注册更改为启动注册;)
回应你的评论,这是由于这一行:
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
你在两个框架中都有这个,它会在你关闭JVM时立即停止它。您希望这是您的登录框架(如果他们关闭登录窗口,停止程序),但不是注册窗口。为此,你只需要javax.swing.WindowConstants.DISPOSE_ON_CLOSE
,它表示在关闭框架时会处理框架(良好的清理)。如果处置了所有帧,JVM将结束(假设没有其他线程正在运行),因此只要Login保持打开状态,它就不会退出。