原谅可能简单的问题(以及可怕的布局方法)。我已成功将输入数据写入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);
}
}
}
答案 0 :(得分: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
访问类的字段,并在不需要静态成员时尽量避免使用静态成员。
答案 1 :(得分:1)
该行
static String data = AddUser.inputData;
加载类时,仅运行一次。所有静态变量都是这种情况。 (您似乎在思考“数据流编程”或电子表格,但Java不能像那样工作。或者您可能认为String对象是可更新的,但它们不是 - 它们是不可变的。)< / p>
这是在类之间实现数据传递的一种可怕方式,正如您所看到的,它不起作用。如果由于某种原因,该类恰好比它更早加载,它甚至不会工作一次。