2011-05-16 44 views
1

原諒大概簡單的問題(和可怕的佈局方法)。我已經成功地將輸入的數據寫入txt文件,並點擊「submit」關閉輸入窗口,打開「菜單」,添加用戶選項(此代碼)或搜索屬性(不相關)。我可以毫無問題地向txt文件輸入一組詳細信息,但是當重新打開AddUser窗口時,無論輸入什麼內容,相同的數據都會像上次一樣輸入到文件中,除非程序關閉。我認爲這與在重新打開窗口之前清除一些變量有關(儘量向底部嘗試),但是我沒有任何運氣。我該如何去解決它?謝謝清除JTextfields寫入多個數據到txt文件

AddUser.java 

package assignment; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.util.*; 
import java.io.*; 
import java.lang.*; 

public class AddUser extends JFrame { 

    //Declare the array values 

    private String[] Name; 
    private String[] Username; 
    private String[] Password; 
    private String[] StaffID; 

    public String inputStaff; 
    public String inputUser; 
    public String inputPass; 
    public String inputID; 

    static public String inputData; 


    //Declare Text Fields 
    public JTextField Field1; 
    public JTextField Field2; 
    public JTextField Field3; 
    public JTextField Field4; 
    public JTextField Field5; 

    //Declare Labels 

    private JLabel Label; 
    private JLabel Label1; 
    private JLabel Label2; 
    private JLabel Label3; 
    private JLabel Label4; 
    private JLabel Label5; 
    private JLabel Space1; 
    private JLabel Space2; 

public AddUser() { 

    super("Add New Agent");  //Window Title 
    setLayout(new FlowLayout(FlowLayout.LEFT)); //Set Layout Type as FlowLayout 

    Label = new JLabel("Enter the Member of Staff's Details"); 
    Label1 = new JLabel("Staff Name"); //Label Values 
    Label2 = new JLabel("Username"); 
    Label3 = new JLabel("Password"); 
    Label4 = new JLabel("Confirm Password"); 
    Label5 = new JLabel("Staff ID"); 
    Space1 = new JLabel(" "); 
    Space2 = new JLabel("          "); 

    Field1 = new JTextField (10); //Create the Text Fields and Option Blocks & Arguments 
    Field2 = new JTextField (10); 
    Field3 = new JTextField (10); 
    Field4 = new JTextField (10); 
    Field5 = new JTextField (4); 






    //Add the labels, textfields and option blocks to the JFrame 

    add (Label); add(Space1); add (Label1); add (Field1); add (Label2); add (Field2); add (Label3); add (Field3); add (Label4); 
    add (Field4); add (Label5); add (Field5); add (Space2); 





    JButton button1 = new JButton("Submit"); //Add "Search" button to JFrame 
    add (button1); 

    onClick handler = new onClick(); 
    button1.addActionListener(handler); 

    } 

    private class onClick implements ActionListener{ 
     public void actionPerformed(ActionEvent event){ 

//Action to be performed 

//Attempt to clear the fields 

      inputStaff = (""); 
      inputUser = (""); 
      inputPass = (""); 
      inputID = (""); 




      inputStaff = Field1.getText(); 
      inputUser = Field2.getText(); 
      inputPass = Field3.getText(); 
      inputID = Field5.getText(); 

      inputData = inputStaff + " " + inputUser + " " + inputPass + " " + inputID; 


      WriteFile Write = new WriteFile(); //Create instance of write-to-file 

      setVisible(false); 
      //Close the window on clicking submit 



       } 

      } 


      } 

寫入文件代碼(WriteFile.java)如下;

package assignment; 

import java.io.*; 

public class WriteFile{ 
static String data = AddUser.inputData; 
BufferedWriter out; 

public WriteFile(){ 
    try { 
     out = new BufferedWriter(new FileWriter("AddUser.txt", true)); 

     out.write(data); 

     out.newLine(); 

     out.close(); 
    } 
    catch(IOException e) 
    { 
     System.out.println("There was a problem:" + e); 

    } 
} 


} 
+0

基本代碼看起來是正確的,但它不完整,所以我無法確定。發佈證明問題的SSCCE(http://sscce.org)。此外,請使用適當的Java命名約定。變量名不應以大寫字符開頭。 – camickr 2011-05-16 14:22:31

+0

你在哪裏寫文件?我沒有在你的代碼中看到它。 – MByD 2011-05-16 14:22:42

+0

隨着提交按鈕被點擊(從private類onCLick ...向下)WriteFile被實例化。在WriteFile.java中寫入文件代碼。 – 2011-05-16 14:25:09

回答

1

這樣的方式來實現在幾個方式缺乏,請考慮以下因素:

public static void WriteFile(String data){ 
    try { 
     out = new BufferedWriter(new FileWriter("AddUser.txt", true)); 
     out.write(data); 
     out.newLine(); 
     out.close(); 
    } 
    catch(IOException e) 
    { 
     System.out.println("There was a problem:" + e); 
    } 
} 

,並調用它像:

WriteFile.WriteFile(inputData); 

我也會改變的名稱該方法,但我儘量保持它儘可能接近原始代碼。

請勿以這種方式訪問​​某個類的字段SomeClass.someField,並在不需要時嘗試避免使用靜態成員。

+0

這是排序它,更有意義..謝謝! – 2011-05-16 14:44:53

+0

請閱讀羅賓的回答。他在那裏指出了重要的一點。 – MByD 2011-05-16 14:45:47

+0

是的,幫助我理解了它的缺陷。 – 2011-05-16 14:48:56

1

static String data = AddUser.inputData; 

被加載的類時,只運行一次。所有靜態變量都是這種情況。 (你似乎在思考「數據流編程」或電子表格,但是Java並不是這樣工作的,或者你可能認爲String對象是可更新的,但它們不是 - 它們是不可變的。)

這是一種在類之間實現數據傳遞的可怕方式,正如你所看到的,它不起作用。如果因爲某種原因,班級碰巧比以前更早加載,它甚至不會工作。

+0

+1 - 非常漂亮。 – MByD 2011-05-16 14:40:38