简体   繁体   中英

How to assign dynamically names to a collection of JButton?

I have to create a collection of JButtons depending of a size of a certain collection. How to create the List of JButtons :button1, button2, button3...dynamically to have something like

for (int i=0;i<collection.size();i++){
   JButton button+i = new Button();
 }

Thanks

Use a list of buttons (or even an array of buttons) :

List<JButton> listOfButtons = new ArrayList<JButton>(collection.size());
for (int i=0; i < collection.size(); i++) {
    JButton button = new JButton();
    listOfButtons.add(button);
}

创建一个List<JButton> ,每次通过你的循环add新的JButton

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.

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