如何将多个文本输入保存在不同的.txt文件中
我制作了一个程序,该程序允许用户拥有尽可能多的文本框,可以输入文本字符串。我想将每个用户输入保存到不同的变量中。我还想打印所有这些变量,或者在我想要的时候访问存储的数据。
如果您能帮助我,那真是太好了,请提前。如果您有任何疑问,请在评论中告诉我,我会回答。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是
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 ofmyNewString
)ArrayLists are indexed like an array, but using a method
get
, so you can get the first item in your list by usinguserStrings.get(0)
.