简体   繁体   English

如何使JButton将用户输入添加到ArrayList并将其显示在JList中

[英]How to make a JButton add user input to an ArrayList and display it in a JList

I have an app that contains 3 classes - main gui (Book), add, contact. 我有一个包含3个类的应用程序-主gui(书籍),添加,联系。

I have a JList in my phonebook class. 我的电话簿课程中有一个JList。 I have a dialog window as my add class as it needs to add a contact, now I can add the data but storing it into an array and then placing that into a JList is proving tricky. 我有一个对话框窗口作为我的添加类,因为它需要添加联系人,现在我可以添加数据,但是将其存储到数组中,然后将其放置到JList中被证明是棘手的。

Would anyone be able to help? 有人可以帮忙吗? I am new to java and I understand that I will need to use defaultListModel at some point but I don't quite understand where 我是Java的新手,我知道我有时需要使用defaultListModel,但我不太了解

My button in the add class is called btnOK my arraylist is just ArrayListcontact my JList is just called list. 我在添加类中的按钮称为btnOK我的arraylist只是ArrayListcontact我的JList只是称为list。

Thanks to anyone who can help! 感谢任何能提供帮助的人!

You should NOT be using an ArrayList to store the data. 您不应该使用ArrayList来存储数据。 The data should be stored in the ListModel. 数据应存储在ListModel中。

Read the section from the Swing tutorial on How to Use Lists for a working example that shows you how to use the "Hire" button to add a persons name to the list. 阅读Swing教程中有关如何使用列表的部分,获取一个工作示例,该示例向您展示如何使用“雇用”按钮将人员姓名添加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM