[英]Store Jtextfield input in Arraylist then print contents of Arraylist
This is my first time creating a GUI.这是我第一次创建 GUI。 I can create text fields just fine, but am having trouble collecting user input and storing their input into an Arraylist.
我可以很好地创建文本字段,但在收集用户输入并将其输入存储到 Arraylist 时遇到问题。
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
textField1 = new JTextField("Enter resident name",20);
add(textField1);
textField2 = new JTextField("How many accounts to enter data for?",20);
add(textField2);
textField3 = new JTextField("Enter account #",20);
add(textField4);
textField3 = new JTextField("Enter data for account",20);
add(textField5);
If the user enters "4" into textField2, I would like "Enter account #" and "Enter data for account" to then appear four times (resulting in 8 new textfields)如果用户在 textField2 中输入“4”,我希望“输入帐户编号”和“输入帐户数据”出现四次(产生 8 个新文本字段)
I would like the user to be able to click a "Store" button that places textfields 1-5 (and more if they want to enter a lot of accounts) into an arraylist that can later be retrieved and print all stored array info (show all resident and all accounts info), similar to something like below:我希望用户能够单击“存储”按钮,将文本字段 1-5(如果他们想输入大量帐户,则更多)放入一个数组列表中,该列表可以在以后检索并打印所有存储的数组信息(显示所有居民和所有帐户信息),类似于以下内容:
ArrayList accounts = new ArrayList<>();
\n\nAccount exampleAccount = new Account();
\n\n账户示例Account = new Account(); exampleAccount.setFirstName("John");
exampleAccount.setFirstName("John"); exampleAccount.setLastName("Doe");
exampleAccount.setLastName("Doe"); exampleAccount.setBalance(101.13d);
exampleAccount.setBalance(101.13d);
accounts.add(exampleAccount);
\n\n帐户。添加(示例帐户);
for(Account account : accounts) { System.out.println("Name: " + account.getFirstName() + " " + account.getLastName() + ". Balance: " + account.getBalance());
for(Account account : accounts) { System.out.println("Name: " + account.getFirstName() + " " + account.getLastName() + ". Balance: " + account.getBalance()); // Or override Account's toString() method and do System.out.println(account.toString()) }
// 或者覆盖 Account 的 toString() 方法并执行 System.out.println(account.toString()) }
Would like the form "reset" so that after each time the user clicks "Store" they can enter a new resident's information.想要“重置”表单,以便用户每次单击“商店”后,他们都可以输入新居民的信息。
How do I go about doing this?我该怎么做?
You need something like this..你需要这样的东西..
textField1 = new JTextField("Enter resident name", 20);
add(textField1);
textField2 = new JTextField("How many accounts to enter data for?", 20);
add(textField2);
textField2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = Integer.valueOf(textField2.getText());
for (int i = 1; i <= count; i++) {
textField3 = new JTextField("Enter account #", 20);
add(textField4);
textField3 = new JTextField("Enter data for account", 20);
add(textField5);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.