java - 清除 JTextfields 以将多个数据写入 txt 文件

标签 java save text-files jtextfield

请原谅这个可能很简单的问题(以及糟糕的布局方法)。我成功地将输入的数据写入 txt 文件,然后单击“提交”关闭输入窗口,使“菜单”保持打开状态,其中包含添加用户(此代码)或搜索属性(不相关)的选项。我可以毫无问题地向 txt 文件输入一组详细信息,但是当重新打开 AddUser 窗口时,无论在框中输入什么内容,都会将与上次相同的数据输入到文件中,除非程序关闭。我认为这与重新打开窗口之前清除某些变量有关(正如向底部尝试的那样),但是我没有任何运气..我该怎么做?谢谢

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);

    }
}


}

最佳答案

这种实现方式存在一些不足,请考虑以下几点:

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,并在不需要时尽量避免使用静态成员。

关于java - 清除 JTextfields 以将多个数据写入 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018678/

相关文章:

java - 如何在其他Gradle子项目中使用生成的EMF模型代码?

java - 多个可绘制背景图像以适应屏幕分辨率

c# - C#覆盖/保存当前文件

php - 将数组转换为 .txt 文件

c# - 将文件拖入富文本框以读取文件中的文本

c - 在 C 打印垃圾中添加回车实用程序?

java - 为基于 Java 的应用程序实现提前编译 (AOT)

swift - 当 UIImagePNGRepresentation 总是返回 nil 时保存 UIImage

iOS 保存图像并命名

java - 计算质数直到 N