清除 JTextfields 以将多个数据写入 txt 文件
请原谅这个可能简单的问题(以及糟糕的布局方法)。我成功地将输入的数据写入 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);
}
}
}
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
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种实现方式存在一些不足,请考虑以下几点:
并这样调用它:
我也会更改方法的名称,但我试图使其尽可能接近原始代码。
不要以
SomeClass.someField
的方式访问类的字段,并在不需要时尽量避免使用静态成员。This way to achieve that lacks in a few manners, please consider the following:
And call it like that:
I would also change the name of the method, but I tried to keep it as close as possible to the original code.
Don't access fields of a class in this way
SomeClass.someField
, and try to avoid static members when they are not needed.该行
仅在类加载时运行一次。所有静态变量都是这种情况。 (您似乎沿着“数据流编程”或电子表格的思路思考,但 Java 不是这样工作的。或者您可能认为 String 对象是可更新的,但它们不是 - 它们是不可变的。
)是一种在类之间实现数据传递的糟糕方法,正如您所看到的,它不起作用。如果由于某种原因该类碰巧早于实际加载,它甚至无法工作一次。
The line
is only run once, when the class is loaded. That is the case with all static variables. (You appear to be thinking along the lines of "dataflow programming" or spreadsheets, but Java doesn't work like that. Or you might be thinking that String objects are updateable, but they're not - they're immutable.)
This is a terrible way to implement data passing between classes, and as you can see, it doesn't work. It wouldn't even work once if for some reason that class happened to be loaded earlier than it was.