如何在不关闭整个程序的情况下清除 JTextFields 中的变量?

发布于 2024-11-07 04:42:18 字数 1364 浏览 0 评论 0原文

我创建了一个文本文件,在其中存储从文本字段获取的一些变量。但为了向该文本文件提交新变量,我需要关闭程序并重新打开它。 dispose(); 命令关闭 JFrame,将我带到主菜单,但再次打开菜单并提交不同的值时,上次的值已重新提交。有没有简单的方法可以修改这个?

这是我编写的 .txt 代码:

public class writeto {

static String data = AddProperty.inputdata;
BufferedWriter out;

public writeto(){
        try{
            out = new BufferedWriter(new FileWriter("writeto.txt", true));

            out.write(data);

            out.newLine();

            out.close();

        }catch(IOException e){

            System.out.println("you have an error" + e);
        }
    }
}

以及在我的 addproperty 类中调用该方法的位置

        submitproperty.addActionListener(new ActionListener()
        {

        public void actionPerformed(ActionEvent e)
            {
              housenumber1 = houseNumber.getText();
              streetname1 = streetName.getText();
              town1 = town.getText();
              postcode1 = postcode.getText();
              beds1 = beds.getText();
              price1 = price.getText();
              type1 = type.getText();

            inputdata = housenumber1 + " " + streetname1 + " " + town1 + " " + 

            postcode1 +" " + beds1 + " " + price1 + " " + type1;

             writeto write = new writeto();
             dispose();
            }
       });
    }

I have created a text file in which to store some variables which are taken from text fields. But in order to submit new variables to this text file, I need to close my program and reopen it. The dispose(); command closes the JFrame taking me to my main menu but upon opening the menu again and submitting different values, the values from the previous time have been resubmitted. Is there a simple way to amend this?

Here is my write to .txt code:

public class writeto {

static String data = AddProperty.inputdata;
BufferedWriter out;

public writeto(){
        try{
            out = new BufferedWriter(new FileWriter("writeto.txt", true));

            out.write(data);

            out.newLine();

            out.close();

        }catch(IOException e){

            System.out.println("you have an error" + e);
        }
    }
}

and where the method is called in my addproperty class

        submitproperty.addActionListener(new ActionListener()
        {

        public void actionPerformed(ActionEvent e)
            {
              housenumber1 = houseNumber.getText();
              streetname1 = streetName.getText();
              town1 = town.getText();
              postcode1 = postcode.getText();
              beds1 = beds.getText();
              price1 = price.getText();
              type1 = type.getText();

            inputdata = housenumber1 + " " + streetname1 + " " + town1 + " " + 

            postcode1 +" " + beds1 + " " + price1 + " " + type1;

             writeto write = new writeto();
             dispose();
            }
       });
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-11-14 04:42:18

您应该始终从菜单中创建一个带有小部件(如文本字段)的 JFrame。 文本字段没有内容,如果您再次显示文本字段,它仍然会显示以前的内容。


附加说明:

  • 请使用标准命名约定 - 不仅仅是当您向其他人展示代码时。在您的情况下:类名应以大写字母开头,首选驼峰式表示法(writeto -> WriteTo
  • writeto 类滥用构造函数。构造函数中的代码不会创建一个writeto对象,而是将一些字符串转储到文件中。将此类代码放入方法中,而不是构造函数中。
  • 如果发生异常,BufferedWriter不会关闭。环顾一下 stackoverflow,很多问题/答案显示了正确的 io 关闭模式,
  • 处置 jframe 是一种风险 - 代码在按下按钮后执行(正确?),在框架上显示的按钮上的方法内(正确的?)。在这种情况下,按钮可能会在按钮对象上的方法仍在执行时被释放。如果您只想隐藏 JFrame(例如“关闭对话框”),请尝试 setVisible(false)

From your menu you should always create a new JFrame with new widgets (like text fields). A new text field has no content, if you show a text field again, it will still display it's previous content.


Additional remarks:

  • Please use standard naming conventions - not only when you show code to others. In your case: class names shall start with a capital letter, camel-case notation is preferred (writeto -> WriteTo)
  • The writeto class abuses the constructor. The code in your constructor does not create an writeto object but dumps some strings to a file. Put this kind of code to a method, not to a constructor.
  • The BufferedWriter will not be closed if an exception occurs. Look around at stackoverflow, a lot of questions/answers show the correct io-closeing pattern
  • disposing the jframe is a risk - the code is executed after pressing a button (correct?), inside a method on a button that is displayed on the frame (correct?). In that case the button may be disposed while a method on the button object is still executed.. Try setVisible(false) if you just want to hide the JFrame (like "close the dialog")
故事与诗 2024-11-14 04:42:18

使用数据库而不是文本文件将使您受益匪浅。此外,您的问题表明我们不仅对 Swing 缺乏了解,而且对基本 CRUD(创建、读取、更新、删除)功能也缺乏了解。

要回答您的问题,您可以使用 textField1.setText(""); 清除文本字段。

我会阅读有关使用数据库存储数据的内容。这将使您的生活变得更加轻松。

You would benefit greatly from using a database as opposed to a text file. Further your question displays a fundamental lack of knowledge of not only Swing, but basic CRUD (Create, Read, Update, Delete) functionality.

To answer your question you can clear your text field with textField1.setText("");

I would read up on using a database for storing data. It will make life much easier for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文