0

I am trying to make a small application in Java Swing using JFrame form. I added buttons from palette to panel in specific positions and now want to add these buttons to an array but I don't know the data type used for array that holds these designed buttons. I searched for it but didn't find anything related to my problem. I am new to coding and have very limited knowledge about Java - any help will be greatly appreciated.

4

3 回答 3

2

我想要一个灵活的按钮列表,只需声明一个List.JButton

List<JButton> listOfButton = new ArrayList<>();
于 2016-05-28T09:36:29.777 回答
1
JButton[] buttons = new JButton[10];

就像任何其他数组一样。

于 2016-05-28T07:54:03.867 回答
0

在这里,我正在编写代码,使用我将按钮添加到 arrayList 并将其取回。

// creating an ArrayList
ArrayList<JButton> btn = new ArrayList<JButton>();

// adding Buttons to ArrayList
btn.addAll(Arrays.asList(Button1, Button2, Button3,........)); 
//instead of writng btn.add(Button1);btn.add(Button2); and so on, use addAll();

// getting buttons from ArrayList
 for (int i = 0; i < btn.size(); i++){
 btn.get(i);
}
于 2016-05-29T07:54:51.003 回答