[英]How Can I save data from GUI to array
I need to create a java project for bank account for 100 clients with Id, name, Surname, and Deposit. 我需要为100个具有ID,名称,姓氏和存款的客户的银行帐户创建一个Java项目。 However, I don't know how I can save data from GUI to arrays every time I press Save Button? 但是,我不知道每次按保存按钮时如何将数据从GUI保存到阵列?
"I want to add a new account everytime I press the button " “我想在每次按下按钮时添加一个新帐户”
You should have your Account
class already with the necessary fields and getters and setters 您应该已经将Account
类与必要的字段以及getter和setter一起使用
In your GUI
class you should have a List
Map
of Accounts
. 在你的GUI
类,你应该有一个List
Map
的Accounts
。 Forget an array, as they aren't dynamic. 忘记数组,因为它们不是动态的。 I'd preferably go with a Map
but if you don't understand Map
, it's a little bit harder to explain, so I'll start you off with a List
我最好选择Map
但如果您不了解Map
,则很难解释,因此我将从List
public class GUI extends JFrame { List<Account> accounts = new ArrayList<>(); }
Then in your actionPerformed
of your button listener, just gather the data from whatever source you're getting it from, ie text fields, and populate the Account
data, then add the new Account
to the list 然后在您的按钮侦听器的actionPerformed
中,仅从要从中获取数据的任何来源(即文本字段)收集数据,并填充Account
数据,然后将新Account
添加到列表中
public void actionPerformed(ActionEvent e) { String name = nameTextFeild.getText(); String surname = surTextField.getText(); String id = idTextField.getText(); Account account = new Account(name, surname, id); accounts.add(account); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.