如何使用按钮(actionListener)连续将对象添加到数组列表? [英] How to continuously add objects to array list with button(actionListener) ?

查看:138
本文介绍了如何使用按钮(actionListener)连续将对象添加到数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于设法获得了我的GUI设置,并且听众注册了。我有一个Student类,一个StudentDemo类和一个StudentInfoWindow。



在StudentInfoWindow类中我有一个addButton,当我点击它时,它从文本字段中抓取信息,创建学生对象,将其添加到数组列表,最后清除文本字段。我想在每次点击addButton时继续将数组/元素添加到数组列表中。



我无法理解这个想法,我想我应该有一个布尔变量isAdding,以跟踪我何时应该停止添加元素,但我甚至无法添加多个开头。

I finally managed to get my GUI setup, and the listeners registered. I have a Student class, a StudentDemo class, and a StudentInfoWindow.

In the StudentInfoWindow class i have a addButton, when i click it, it grabs the info from the text fields, creates a student object, adds it to an array list, and finally clears the text fields. I would like to keep on adding Objects/elements to the array list every time i hit addButton.

I can't get around the idea, i think i should have a boolean variable isAdding, to keep track of when i should stop adding elements, but i cant even add multiple ones to begin with.

推荐答案

我正在创建数组列表在actionPerformed方法中,所以每次单击按钮时我都会重新创建一个新列表。相反,我在类的顶部创建了数组列表,因此它只是继续添加对象!
I was creating the array list inside the actionPerformed method, so i was recreating a new list every time i click the button. Instead i created the array list at the top of the class, so it just keeps on adding objects!


这篇关于如何使用按钮(actionListener)连续将对象添加到数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆