如何将多个文本输入保存在不同的.txt文件中

发布于 2025-01-30 02:00:43 字数 1489 浏览 2 评论 0原文

我制作了一个程序,该程序允许用户拥有尽可能多的文本框,可以输入文本字符串。我想将每个用户输入保存到不同的变量中。我还想打印所有这些变量,或者在我想要的时候访问存储的数据。

如果您能帮助我,那真是太好了,请提前。如果您有任何疑问,请在评论中告诉我,我会回答。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI
{
    private JFrame frame;
    private JButton button;
    private JTextField tfield;
    private String nameTField;
    private int count;
    public GUI()
    {
        nameTField = "tField";
        count = 0;
    }
    private void displayGUI()
    {
        frame = new JFrame("GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 1, 2, 2));
        button = new JButton("Add Another");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                tfield = new JTextField();
                tfield.setName(nameTField + count);
                count++;
                frame.add(tfield);
                frame.revalidate();
                frame.repaint();
            }
        });
        frame.add(button);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new GUI().displayGUI();
            }
        });
    }
}

I have made a program, which allows users to have as many text boxes as they want in which they can input a text string. I want to save each user input into different variables. I also want to print all of those variable or access the data stored in them when ever I want.

If you can help me that it would be really nice, thanks in advance. If you have any question please let me know in comment and I will answer them.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI
{
    private JFrame frame;
    private JButton button;
    private JTextField tfield;
    private String nameTField;
    private int count;
    public GUI()
    {
        nameTField = "tField";
        count = 0;
    }
    private void displayGUI()
    {
        frame = new JFrame("GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 1, 2, 2));
        button = new JButton("Add Another");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                tfield = new JTextField();
                tfield.setName(nameTField + count);
                count++;
                frame.add(tfield);
                frame.revalidate();
                frame.repaint();
            }
        });
        frame.add(button);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new GUI().displayGUI();
            }
        });
    }
}

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

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

发布评论

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

评论(1

蹲在坟头点根烟 2025-02-06 02:00:43

您正在寻找的是arrayList< string>

声明私有ArrayList< string> UserStrings;您拥有私有字段。

在您的构造函数中,添加usersrings = new ArrayList< string>();

,然后在接收字符串时,只需使用userstrings.add(myNewString)将其添加到您的列表中。 (使用您的字符串代替myNewString

ArrayLists像数组一样被索引,但是使用方法get,因此您可以使用> usersrings.get(0)

What you're looking for is an ArrayList<String>.

Declare a private ArrayList<String> userStrings; where you have your private fields.

In your constructor, add userStrings = new ArrayList<String>();

And then when you receive a string, just add it into your list with userStrings.add(myNewString). (Use your String instead of myNewString)

ArrayLists are indexed like an array, but using a method get, so you can get the first item in your list by using userStrings.get(0).

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