清除JTextfields以将多个数据写入txt文件 [英] Clearing JTextfields to write multiple data to txt file
问题描述
打扰可能简单的问题(以及可怕的布局方法).我已成功将输入的数据写入txt文件的代码,然后单击提交"将关闭输入窗口,使菜单"保持打开状态,并提供添加用户(此代码)或搜索属性(不相关)的选项.我可以毫无问题地向txt文件输入一组详细信息,但是当重新打开AddUser窗口时,无论在框中键入什么内容,除非关闭程序,否则与上次输入的数据相同.我认为这与在重新打开窗口之前清除一些变量有关(如向底部尝试),但是我没有任何运气.谢谢
excuse the probably simple question (and the terrible layout methods). The code I have successfully writes inputted data to a txt file and on clicking "submit" closes the input window, leaving the "menu" open, with options to add a user (this code) or search properties (unrelated). I can enter one set of details to the txt file with no problem, but when reopening the AddUser window, no matter what is typed in to the boxes the same data is inputted into the file as the previous time, unless the program is closed. I think it has something to do with clearing some variable before reopening the window (as tried towards the bottom) however i haven't had any luck.. How do I go about it? Thanks
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)如下;
The write to file code (WriteFile.java) is as follows;
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);
}
}
}
推荐答案
这种在某些方面缺少的方法,请考虑以下几点:
This way to achieve that lacks in a few manners, please consider the following:
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);
我也将更改方法的名称,但我尝试将其保持与原始代码尽可能近.
I would also change the name of the method, but I tried to keep it as close as possible to the original code.
请勿以这种方式访问类的字段SomeClass.someField
,并在不需要静态成员时尝试避免使用它们.
Don't access fields of a class in this way SomeClass.someField
, and try to avoid static members when they are not needed.
这篇关于清除JTextfields以将多个数据写入txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!