2014-02-24 19 views
0

我需要使用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(); 
        } 
       } 
      } 
    } 
} 

回答

0

我不太清楚你說「使用記事本」是什麼意思。你的意思是你需要一個更好的方式來處理選民而不是閱讀名爲'vot.txt'的文件嗎?如果是這樣的話,那麼,是的,你可以使用投票者ID查詢數據庫並檢查他們的狀態

+0

我的意思是,如果在通過記事本進行掃描時還有其他方法可用?因爲我們還沒有被要求使用數據庫。記事本將成爲我們的臨時數據庫。 – user3347692

+0

查看Java中的Property對象。他們對每一行都有一個key = value格式。評論以#開頭。您可以輕鬆地從磁盤加載()該文件,然後獲取(鍵)來檢索該值。如果找不到,則返回空值。不知道您的vot.txt文件的格式是/是 - 我沒有仔細看。 – mikemil

0

你從來沒有真正分配一個有效的值到sIDsName ......基本上你只是分配曾經被設置爲默認值時,他們創造了JTextField小號...

String sID = studIdTxt.getText(); 
String sName = studNameTxt.getText(); 

這意味着,當你來到檢查值對什麼是在文件中,你沒有真正比較有什麼目前在田野...記住,GUI是事件驅動的環境,有些東西偏偏,你迴應......

你應該是做什麼的,當actionPerformed被調用時,你應該從田間地頭的文字...

public void actionPerformed (ActionEvent e){ 

    if (e.getSource() == loginBtn) 
    { 
     sID = studIdTxt.getText(); 
     sName = studNameTxt.getText(); 

什麼是更好的是,如果你的檢查方法實際上要求你傳遞你想檢查的值,而不是依賴實例值。

是的,有比解決文件更好的解決方案,像H2(例如)的獨立數據庫將是一個更好的主意。

+0

我現在的問題是每當我登錄時,它總是落入我的if語句中,即使它應該落入我的else中。是否有其他方法來讀取和傳遞記事本的值?謝謝您的幫助。 – user3347692

+0

或者我應該這樣問嗎?是否有另一種閱讀和檢查每行文本文件的方式?因爲無論何時打印變量「id」的值,它都會在文件中打印整個文本。 – user3347692

+0

我將取決於您的文件是如何格式化的,您現在也可以使用XML – MadProgrammer

相關問題