Java 使用JTextField创建/编辑对象
我正试图编写一个商店库存程序,程序读取当前库存的一个文件,该文件是一个ArrayList,其中product类只定义每个产品的名称,价格等。我正在努力寻找一种方法,让用户在JTextField中输入产品中新对象的信息,并在输入完所有信息后保存所有信息,创建对象并将其放入ArrayList。目前我的ActionListener类可以工作,但当我在文本框中输入信息并按enter键时,它会弹出一条消息,告诉我输入了什么。谢谢 你的ActionListener中应该有这样的功能Java 使用JTextField创建/编辑对象,java,swing,user-interface,arraylist,jtextfield,Java,Swing,User Interface,Arraylist,Jtextfield,我正试图编写一个商店库存程序,程序读取当前库存的一个文件,该文件是一个ArrayList,其中product类只定义每个产品的名称,价格等。我正在努力寻找一种方法,让用户在JTextField中输入产品中新对象的信息,并在输入完所有信息后保存所有信息,创建对象并将其放入ArrayList。目前我的ActionListener类可以工作,但当我在文本框中输入信息并按enter键时,它会弹出一条消息,告诉我输入了什么。谢谢 你的ActionListener中应该有这样的功能 String descr
String description = descriptionTextBox.getText();
String price = priceTextBox.getText();
Product p = new Product(description, price);
ArrayList<Product> products = new ArrayList<Product>();
products.add(p);
String description=descriptionContextBox.getText();
字符串price=priceTextBox.getText();
产品p=新产品(说明、价格);
ArrayList产品=新的ArrayList();
产品.加入(p);;
- 列一张清单
- 创建一个文本字段
- 制作按钮
- 将侦听器添加到按钮
- 从文本字段或文本区域获取文本
- 添加到数组列表对象
- 完成:)
ArrayList<String> arrayObject= new ArrayList<String>();
JButton button = new JButton();
JtextField textBox = new JtextField ();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//inside your action listener:
String add_item_to_array = textBox.getText().trim();
arrayObject.add(add_item_to_array);
}
});
ArrayList arrayObject=new ArrayList();
JButton button=新JButton();
JtextField textBox=新的JtextField();
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
//在动作监听器中:
字符串add_item_to_array=textBox.getText().trim();
arrayObject.add(将项目添加到数组);
}
});
请在您的回答中添加一些解释。我已经添加了所需的信息:)请随意回答,因为这个答案比上面的答案更直接。谢谢@PhilipGlad我能帮上忙的!