2013-12-15 56 views
1

我對編碼相當陌生,正在努力通過首先檢查索引文件是否存在(其中包含密碼)來獲取便宜且易於使用的登錄代碼,以及如果是這樣,請閱讀它說的內容並要求用戶輸入用戶名和密碼。如果文件不存在或爲空,它應該提示用戶從此開始使用兩個字段並將它們寫入索引文件。變量不會從JTextField獲取數據

然而,我遇到的問題是,我設置的運行代碼將這兩個字段寫入文件的按鈕似乎沒有在字符串中找到任何內容,因此無法寫入任何內容。我將下面的代碼包括在內,如果有什麼我忘記的話,可以添加更多的代碼。

以下是GUI窗口和動作偵聽器的代碼。

public class GUIComponents extends JFrame{ 
public static String UserN = " "; 
public static String PassW = " "; 

public static void LoginScreen(){ 
    JFrame LoginFrame = new JFrame("Log in Menu"); 
    JPanel LoginInfo = new JPanel(); 
    JPanel LoginAttempt = new JPanel(); 

    JButton LoginOkay = new JButton("Log in"); 
    LoginListenerClass loginListen = new LoginListenerClass(); 
    LoginOkay.addActionListener(loginListen); 
    JButton LoginCancel = new JButton("Cancel"); 
    CancelListenerClass cancelListen = new CancelListenerClass(); 
    LoginCancel.addActionListener(cancelListen); 
    JLabel usern = new JLabel("Username:"); 
    JLabel passw = new JLabel("Password:"); 
    JTextField user = new JTextField(15); 
    JTextField pass = new JTextField(15); 
    LayoutManager LoginLayout = new GridLayout(2,2,5,10); 
    LayoutManager OkayCancel = new FlowLayout(); 
    LoginInfo.setLayout(LoginLayout); 
    LoginAttempt.setLayout(OkayCancel); 

    LoginInfo.add(usern); 
    LoginInfo.add(user); 
    LoginInfo.add(passw); 
    LoginInfo.add(pass); 
    LoginAttempt.add(LoginOkay); 
    LoginAttempt.add(LoginCancel); 

    if(Login.firstTimeCheck()==true){ 
     JOptionPane.showMessageDialog(null, "This is your first time using this program. \nPlease enter the Username" 
       + " and Password\n    you wish to set for all users."); 
    } 
    LoginFrame.add(LoginInfo, BorderLayout.CENTER); 
    LoginFrame.add(LoginAttempt, BorderLayout.SOUTH); 

    LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    LoginFrame.setLocationRelativeTo(null);       




    LoginFrame.pack(); 
    LoginFrame.setVisible(true); 

    UserN = user.getText(); 
    PassW = pass.getText(); 
} 

}

class LoginListenerClass implements ActionListener{ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(Login.firstTimeCheck()==false){ 
      Login.passCheck(); 
     } 
     else{ 
      try { 
       Login.CreateLogin(GUIComponents.UserN,  GUIComponents.PassW); 
       System.out.println(GUIComponents.UserN+" "+ GUIComponents.PassW); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     } 
    } 
} 

class CancelListenerClass implements ActionListener{ 
     @Override 
    public void actionPerformed(ActionEvent e){ 
     System.exit(0); 
    } 
} 

下面這是對數函數的代碼:

public class Login { 
private static String UserName;  //true username 
private static String Password;  //true password 
private static boolean accepted = false; 
private static String user=""; 
private static String pass=""; 
private static boolean firstTime = true; 

Login(){ 

} 

public static void CreateLogin(String user, String pass) throws IOException{ 
    UserName = user; 
    Password = pass; 

    try{ 
     FileWriter FW = new FileWriter("Index.txt"); 
     PrintWriter output = new PrintWriter(FW); 
     output.println(UserName); 
     output.println(Password); 
     output.close(); 
     FW.close(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 

public static void LogIn(){ 
    try{ 
     FileReader FR = new FileReader("Index.txt"); 
     firstTime = false; 
     Scanner sc = new Scanner(FR); 
     user = sc.nextLine(); 
     pass = sc.nextLine(); 
     if(user.equalsIgnoreCase("")){ 
      firstTime = true; 
     } 
     if(pass.equals("")){ 
      firstTime = true; 
     } 
    } 

    catch(FileNotFoundException e){ 
     firstTime = true; 
    } 

    GUIComponents.LoginScreen(); 





} 

public static void passCheck(){ 
    if(UserName.equalsIgnoreCase(user)){ 
     if(Password.equals(pass)){ 
      accepted = true; 
     } 
    } 
    else{ 
     JOptionPane.showMessageDialog(null,"Your Username and Password do not /n match our files." 
       + "Please try again."); 
    } 
} 

public static boolean firstTimeCheck(){ 
    return firstTime; 
} 

}

如果任何人都可以找出我做了什麼愚蠢的事這種情況下,請讓我知道。我很抱歉,如果代碼是漫長的和煩人的閱讀。

+1

請縮短代碼,向我們顯示錯誤,並僅使用相關數據。 – iWumbo

回答

2

在你的代碼在這裏:

public static void LoginScreen(){ 
    // ............ code deleted to simplify things 

    JTextField user = new JTextField(15); 
    JTextField pass = new JTextField(15); 

    // ..... code removed to simplify things 

    LoginFrame.pack(); 
    LoginFrame.setVisible(true); 

    UserN = user.getText(); 
    PassW = pass.getText(); 
} 

你試圖在創建GUI和用戶之前從JTextField中提取文本有機會進入任何東西進去。爲了解決這個問題,不要在GUI創建時提取文本,而是由於事件,例如JButton的ActionListener中的事件,當按下JButton時將會激活該事件,或者在輸入時將激活的JTextFields的ActionListener當文本字段有焦點時按下。


順便說一句,你的代碼從總遭受過度使用靜態修飾符,並建議你將要學習如何創建真正的OOP類和Java基礎知識之前試圖創建的Swing GUI的。你不會後悔這樣做。

+0

對所有評論+1,我會添加學習和使用Java命名約定。變量名稱不應以大寫字符開頭。我見過的任何書或教程都遵循約定,所以不要自己編寫! – camickr

2

讀取listener類actionPerformed方法內的UserN和PassW值。否則,您正在讀取初始化時的值,該值爲空字符串。

2

的變量你定義

public static String UserN = " "; 
public static String PassW = " "; 

不會得到從文本字段的最新值。

提出的解決方案(不理想,但對於一些您的代碼)

  1. 讓Jtext領域靜態公網作爲GuiComponents

    public static JTextField user;  
    public static JTextField pass; 
    

在你LoginListener

Login.CreateLogin(GUIComponents.user.getText(),GUIComponents.pass.getText()); 
System.out.println(GUIComponents.user.getText() + GUIComponents.pass.getText());