【问题标题】:Clearing JTextfields to write multiple data to txt file清除 JTextfields 以将多个数据写入 txt 文件
【发布时间】: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


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

【讨论】:

  • 整理好了,也更有意义了..谢谢!
  • 请阅读罗宾的回答。他在那里陈述了一个重要的观点。
  • 是的,也帮助我理解了其中的缺陷。
  • @Chris - 如果其中一个答案令您满意并且对您有所帮助,请将其标记为已接受的答案,这样人们就不会无缘无故添加答案了。
【解决方案2】:

线

static String data = AddUser.inputData;

仅在加载类时运行一次。所有静态变量都是如此。 (您似乎在思考“数据流编程”或电子表格,但 Java 不是那样工作的。或者您可能认为 String 对象是可更新的,但它们不是 - 它们是不可变的。)

这是一种在类之间实现数据传递的糟糕方法,正如您所见,它不起作用。如果由于某种原因该类碰巧比它加载得早,它甚至不会工作一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多