【发布时间】:2011-08-26 11:54:26
【问题描述】:
请原谅这个可能很简单的问题(以及糟糕的布局方法)。我已成功将输入数据写入 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);
}
}
}
【问题讨论】:
-
基本代码看起来正确但不完整,所以我无法确定。发布演示问题的 SSCCE (sscce.org)。此外,使用正确的 Java 命名约定。变量名不应以大写字符开头。
-
您实际上在哪里写入文件?我在您的代码中没有看到它。
-
单击提交按钮时(从私有类 onCLick... 向下)WriteFile 被实例化。在 WriteFile.java 中是写入文件的代码。
-
但是你怎么知道要写什么?
-
它将输入的内容写入上面包含的文本字段(无论存储在“inputData”中)
标签: java save text-files jtextfield