我有一个注册按钮,当我按下注册时,它应该将值写入在文本字段中键入的details.txt。当我第一次注册时它可以工作,但如果文件包含一些值,如果我尝试第二次注册它将冻结。任何人都可以帮助调试这个?
主类
package assignment;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment
{
JFrame mainFrame;
JPanel framePanel,leftPnl,topPnl,btmPnl;
JLabel welcomeLabel,studId,passwd;
JTextField studField,passField;
JButton logBtn,signBtn;
Color btnClr,btnTxtClr,backgroundClr,lblClr;
Font mainFont,labelFont;
public void mainPage()
{
mainFrame = new JFrame("CGPA Calculator");
framePanel = new JPanel();
//-------------------Creating Components/Setting Fonts/Color----------------
-------------------------------------------
//Color
btnClr = new Color(0, 179, 60);
btnTxtClr = new Color(255,255,255);
backgroundClr = new Color(26, 26, 26);
lblClr = new Color(255,255,255);
//Fonts
mainFont = new Font("Verdana",Font.PLAIN,40);
labelFont = new Font("Verdana",Font.PLAIN,20);
//Button
logBtn = new JButton("<html><font face=\"Verdana\">Login</font></html>");//Source from:https://stackoverflow.com/questions/21046164/jbutton-text-with-different-font-family-in-different-words
logBtn.setFocusPainted(false);
logBtn.setBackground(btnClr);
logBtn.setForeground(btnTxtClr);
signBtn = new JButton("<html><font face=\"Verdana\">Register</font></html>");
signBtn.setFocusPainted(false);
signBtn.setBackground(btnClr);
signBtn.setForeground(btnTxtClr);
//Label
welcomeLabel = new JLabel("CGPA Calculator");
welcomeLabel.setForeground(lblClr);
studId = new JLabel("Student ID :");
studId.setForeground(lblClr);
passwd = new JLabel("Password :");
passwd.setForeground(lblClr);
//TextField
studField = new JTextField(8);
studField.setBorder(javax.swing.BorderFactory.createEmptyBorder());//Sourced From:https://stackoverflow.com/questions/2281937/swing-jtextfield-how-to-remove-the-border
passField = new JTextField(8);
passField.setBorder(BorderFactory.createEmptyBorder());
//Adding Fonts to Components
welcomeLabel.setFont(mainFont);
studId.setFont(labelFont);
passwd.setFont(labelFont);
//Panel
leftPnl = new JPanel();
topPnl = new JPanel();
btmPnl = new JPanel();
//----------------------Adding of panels/layout/components--------------------------------------------------------
//Seting up layouts to panel
leftPnl.setLayout(new FlowLayout());
btmPnl.setLayout(new BoxLayout(btmPnl,BoxLayout.X_AXIS));
//TOPPANEL(Adjustments)
topPnl.setBackground(backgroundClr);
topPnl.add(welcomeLabel);
topPnl.add(Box.createRigidArea(new Dimension(0,150)));
//LEFTPANEL(Adjustements)
leftPnl.setBackground(backgroundClr);
leftPnl.add(Box.createRigidArea(new Dimension(0,70)));
leftPnl.add(studId);
leftPnl.add(studField);
leftPnl.add(passwd);
leftPnl.add(passField);
//BOTTOMPANEL
btmPnl.setBackground(backgroundClr);
btmPnl.add(Box.createRigidArea(new Dimension(130,0)));
btmPnl.add(logBtn);
btmPnl.add(Box.createRigidArea(new Dimension(40,0)));
btmPnl.add(signBtn);
btmPnl.add(Box.createRigidArea(new Dimension(200,200)));
//Action Listeners(lambda expression)
signBtn.addActionListener((ActionEvent e) -> {
SignUpPage sign = new SignUpPage();
sign.SecondPage();
sign.getFrame().setVisible(true);
//mainFrame.setVisible(false);
});
mainFrame.getContentPane().setBackground(backgroundClr);
mainFrame.add(topPnl,BorderLayout.NORTH);
mainFrame.add(leftPnl,BorderLayout.WEST);
mainFrame.add(btmPnl,BorderLayout.SOUTH);
mainFrame.setVisible(true);
mainFrame.setSize(450,450);
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Assignment display = new Assignment();
display.mainPage();
new SignUpPage();
}
}
注册班级
package assignment;
import javax.swing.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.io.*;
public class SignUpPage
{
private JFrame signFrame;
JTextField idField,passwdField;
JButton registerBtn,cancelBtn,resetBtn;
JLabel studIdLbl,passwdLbl,schoolLbl,label;
JComboBox schoolBox;
String [] school = {"School Of Computing & Creative Media","School Of
Communication & Creative Arts","School Of Engineering","School Of Business"
,"School of Hospiality Tourism & Culinary Arts"};
Font labelFont;
Color btnClr,btnTxtClr,lblClr,backgroundClr;
String selectSchool;
BufferedReader validateFile;
String details;
String detailsArray[];
int detailsExists = 0;
public void SecondPage()
{
//ComboBox(Class)
label = new JLabel("");
//Color
btnClr = new Color(0, 179, 60);
btnTxtClr = new Color(255,255,255);
backgroundClr = new Color(26, 26, 26);
lblClr = new Color(255,255,255);
//Font
labelFont = new Font("Verdana",Font.PLAIN,13);
//JTextField
idField = new JTextField("Student ID",4);
idField.setMinimumSize(new Dimension(300,30));
idField.setMaximumSize(new Dimension(300,30));
idField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
passwdField = new JTextField("Password",4);
passwdField.setMinimumSize(new Dimension(300,30));
passwdField.setMaximumSize(new Dimension(300,30));
passwdField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
//JComboBox
schoolBox = new JComboBox(school);
schoolBox.setFont(labelFont);
schoolBox.setMinimumSize(new Dimension(300,30));
schoolBox.setMaximumSize(new Dimension(300,30));
schoolBox.addActionListener((ActionEvent e)->{
JComboBox <String> schoolCombo = (JComboBox<String>) e.getSource();
selectSchool = (String) schoolCombo.getSelectedItem();
});
//Panel
JPanel fieldPnl = new JPanel();
JPanel btnPnl = new JPanel();
//Button
registerBtn = new JButton("<html><font face=\"Verdana\">Register</font></html>");
registerBtn.setBackground(btnClr);
registerBtn.setForeground(lblClr);
cancelBtn = new JButton("<html><font face=\"Verdana\">Cancel</font></html>");
cancelBtn.setBackground(btnClr);
cancelBtn.setForeground(lblClr);
resetBtn = new JButton("<html><font face=\"Verdana\">Reset</font></html>");
resetBtn.setBackground(btnClr);
resetBtn.setForeground(lblClr);
//Label
studIdLbl = new JLabel("Student ID:");
passwdLbl = new JLabel("Password");
schoolLbl = new JLabel("School:");
//Adding Components to Panel
fieldPnl.add(Box.createRigidArea(new Dimension(0,100)));
fieldPnl.setBackground(backgroundClr);
fieldPnl.add(idField);
fieldPnl.add(Box.createRigidArea(new Dimension(0,10)));
fieldPnl.add(passwdField);
fieldPnl.add(Box.createRigidArea(new Dimension(0,10)));
fieldPnl.add(schoolBox);
fieldPnl.add(Box.createRigidArea(new Dimension(0,50)));
btnPnl.setBackground(backgroundClr);
btnPnl.add(registerBtn);
btnPnl.add(cancelBtn);
btnPnl.add(resetBtn);
btnPnl.add(label);
btnPnl.add(Box.createRigidArea(new Dimension(0,190)));
//Layout
fieldPnl.setLayout(new BoxLayout(fieldPnl,BoxLayout.Y_AXIS));
//Action Listener
registerBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedWriter toFile;
try {
signUpValidation();
if(detailsExists==0)
{
toFile = new BufferedWriter(new FileWriter("details.txt",true));
toFile.write(idField.getText() + "," + passwdField.getText() + ","+selectSchool);
toFile.newLine();
toFile.flush();
}
else if(detailsExists==1)
{
//same values exists in file
}
} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
signFrame = new JFrame("Sign Up");
signFrame.add(btnPnl,BorderLayout.SOUTH);
signFrame.add(fieldPnl,BorderLayout.CENTER);
signFrame.setVisible(true);
signFrame.setSize(450,450);
signFrame.setResizable(false);
signFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
public Frame getFrame()
{
return signFrame;
}
public int signUpValidation() throws IOException
{
validateFile = new BufferedReader(new FileReader("details.txt"));
details = validateFile.readLine();
while(details!=null)
{
detailsArray=details.split(",");
if(idField.equals(detailsArray[0]) || passwdField.equals(detailsArray[1]))
{
detailsExists = 1;
}
else
detailsExists = 0;
}
return detailsExists;
}
}
答案 0 :(得分:1)
while(details!=null)
阻止循环退出,因为details
未在循环的上下文中更新 - 它永远不会变为null
像...一样的东西。
String details = null;
while((details = validateFile.readLine()) !=null) {...
可能更安全。但是根据你的代码,你只需要一行,所以完全摆脱循环可能是一个更好的解决方案