i am using netbeans to create my GUI. I got the following class
Inside my MainUI.java are the codes auto generated by netbeans for my UI.
I want to create an ArrayList which will add a Person object into the arraylist when a JButton is clicked.
Where should i place the code ArrayList<Person> list = new ArrayList<Person>();
?
public class MainUI extends javax.swing.JFrame {
OR
public static void main(String args[]) {
.
If i put it in static void main, i could not access the arraylist from my eventhandler code in MainUI etends javax.swing.JFrame
Create a class where you store variables that should be accessed from anywhere in your program.
class Global {
public static ArrayList<Object> list = new ArrayList<>();
private Global(){}
}
You would access it like this:
GLobal.list.add(new Object());
我看不到每个实现的内容,建议将其放在MainUI
类中,因为每次按下按钮并在MainUI
实现该按钮时都会使用MainUI
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.