0
我想弄清楚如何在JButton上創建一個JButton的actionListner,這是從JButton的一個arrayList創建的。如何將一個ActionListener放在用Jbuttons的ArrayList創建的JButton上
這裏是按鈕的數組列表:
public static ArrayList<JButton> myTests;
public static ArrayList<JButton> selectedTests;
這裏的邏輯設置起來:
public Devices(String[] testList, String title2)
{
myTests = createTestList(testList);
selectedTests = new ArrayList<JButton>();
checkedSerialNo = new ArrayList();
int numCols = 2;
//Create a GridLayout manager with
//four rows and one column
setLayout(new GridLayout(((myTests.size()/numCols) + 1), numCols));
//Add a border around the panel
setBorder(BorderFactory.createTitledBorder(title2));
for(JButton jcb2 : myTests)
{
this.add(jcb2);
}
}
private ArrayList<JButton> createTestList(String[] testList)
{
String[] tests = testList;
ArrayList<JButton> myTestList = new ArrayList<JButton>();
for(String t : tests)
{
myTestList.add(new JButton(t));
}
for(JButton jcb2 : myTestList)
{
jcb2.addItemListener(this);
}
return myTestList;
}
@Override
public void itemStateChanged(ItemEvent ie)
{
if(((JButton)ie.getSource()).isSelected())
{
selectedTests.add((JButton) ie.getSource());
}
}
public ArrayList<JButton> getSelectedTests()
{
return selectedTests;
}
什麼我不知道的是如何使對所產生的actionListner或onClickListener數組列表中的按鈕。
任何洞察力和幫助表示讚賞。
謝謝!
ironmantis7x