我需要使用java.AWT
和/或中的文本文件創建一個基本的登錄系統。其他方法來傳遞文本文件中的值來創建一個基本的java登錄系統?
在控制檯應用程序中使用BufferedReader
可以工作,但是當我在GUI(Swing
)中使用相同的過程時,它完全是一團糟。我的JTextField
的值與文本文件中的值不匹配。
是否有任何可能的方式使用文本文件而不是使用io類創建登錄系統。這是我的代碼。
public class VotingSystemLogin extends JFrame implements ActionListener {
//initialize io constructors
static int checkAcc = 0;
static int checkAcc2 = 0;
static int checkTotal;
//initialize and construct components
JButton loginBtn = new JButton ("LOGIN");
JButton regBtn = new JButton ("REGISTER");
JLabel idLbl = new JLabel ("StudentID:");
JTextField studIdTxt = new JTextField (5);;
JLabel nameLbl = new JLabel ("StudentName:");
JTextField studNameTxt = new JTextField (5);
JLabel nameLayoutLbl = new JLabel ("(e.g. Juan dela Cruz)");
JLabel idLayoutLbl = new JLabel ("(e.g. 063-2013-0001)");
JLabel bgLbl = new JLabel ("bg");
JPanel panel = new JPanel();
Container con = getContentPane();
String sID = studIdTxt.getText();
String sName = studNameTxt.getText();
public VotingSystemLogin() {
super("Login");
//adjust size and set layout and location
setSize(380, 230);
setLocation(530,290);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout (null);
panel.setBackground(Color.yellow);
panel.setBorder(BorderFactory.createMatteBorder(10,10,10,10,Color.blue));
//add components
panel.add(loginBtn);
panel.add(regBtn);
panel.add(idLbl);
panel.add(studIdTxt);
panel.add(nameLbl);
panel.add(studNameTxt);
panel.add(nameLayoutLbl);
panel.add(idLayoutLbl);
//panel.add(bgLbl);
try{
BufferedImage loginLogo = ImageIO.read(new File("loginBg.jpg"));
bgLbl = new JLabel(new ImageIcon(loginLogo));
panel.add(bgLbl);
bgLbl.setBounds (15, 10, 340, 180);
}
catch(IOException a)
{
System.out.println(a);
}
con.add(panel);
//set component bounds (only needed by Absolute Positioning)
loginBtn.setBounds (70, 140, 115, 40);
regBtn.setBounds (210, 140, 115, 40);
idLbl.setBounds (70, 35, 60, 35);
studIdTxt.setBounds (155, 40, 170, 20);
nameLbl.setBounds (70, 85, 80, 25);
studNameTxt.setBounds (155, 85, 170, 20);
nameLayoutLbl.setBounds (155, 105, 170, 15);
idLayoutLbl.setBounds (155, 60, 120, 15);
//add actionlisterner to buttons
loginBtn.addActionListener(this);
}
public static void main (String[] args) {
VotingSystemLogin vsl = new VotingSystemLogin();
}
public void actionPerformed (ActionEvent e){
if (e.getSource() == loginBtn)
{
checkVoters();
checkVoted();
checkTotal = checkAcc + checkAcc2;
System.out.println(checkAcc);
System.out.println(checkAcc2);
System.out.println(checkTotal);
if (checkTotal == 2)
JOptionPane.showMessageDialog(null,"You have already voted!","Fail",JOptionPane.ERROR_MESSAGE);
else if (checkTotal == 1)
JOptionPane.showMessageDialog(null,"You may vote now!","Success",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,"StudentID or StudentName is incorrect or doesn't exist","Fail",JOptionPane.ERROR_MESSAGE);
}
}
public void checkVoters(){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("voters.txt"));
String id1 = br.readLine();
String name1 = br.readLine();
while (id1 != null && name1 != null){
System.out.println(id1);
System.out.println(name1);
id1 = br.readLine();
name1 = br.readLine();
if (sID.equals(id1) && sName.equals(name1)){
checkAcc = 1;
}
}
System.out.println();
}
catch (FileNotFoundException a){
a.printStackTrace();
}
catch (IOException a){
a.printStackTrace();
}
finally{
if (br != null){
try{
br.close();
}
catch(IOException a){
a.printStackTrace();
}
}
}
}
public void checkVoted(){
BufferedReader br2 = null;
try{
br2 = new BufferedReader(new FileReader("voted.txt"));
String id2 = br2.readLine();
String name2 = br2.readLine();
while (id2 != null && name2 != null){
System.out.println(id2);
System.out.println(name2);
if (sID.equals(id2) && sName.equals(name2)){
checkAcc2 = 1;
}
id2 = br2.readLine();
name2 = br2.readLine();
}
System.out.println();
}
catch (FileNotFoundException a){
a.printStackTrace();
}
catch (IOException a){
a.printStackTrace();
}
finally{
if (br2 != null){
try{
br2.close();
}
catch(IOException a){
a.printStackTrace();
}
}
}
}
}
我的意思是,如果在通過記事本進行掃描時還有其他方法可用?因爲我們還沒有被要求使用數據庫。記事本將成爲我們的臨時數據庫。 – user3347692
查看Java中的Property對象。他們對每一行都有一個key = value格式。評論以#開頭。您可以輕鬆地從磁盤加載()該文件,然後獲取(鍵)來檢索該值。如果找不到,則返回空值。不知道您的vot.txt文件的格式是/是 - 我沒有仔細看。 – mikemil