我有一个JButton
ArrayList
。下面的代码不起作用。如何从数组列表中访问特定按钮并设置大小,位置和其他属性,并将其添加到小程序中?
ArrayList<String> cdTitles = new ArrayList<String>();
ArrayList<JButton> btnCDS = new ArrayList<JButton>();
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Initialize")) {
cdTitles.add("Original Order");
cdTitles.add("Metric - Fantasies");
cdTitles.add("Beatles - Abby Road");
cdTitles.add("Pearl Jam - Ten");
cdTitles.add("Doors - Alive");
cdTitles.add("The Rolling Stones - Gimme Shelter");
for(int i = 0; i < cdTitles.size(); i++) {
String buttonName = "btn" + i;
System.out.println(buttonName);
btnCDS.add(new JButton(buttonName));
break;
}
btnCDS.get(0).setText(cdTitles.get(0));
btnCDS.get(0).setLocation(100, 145);
btnCDS.get(0).setSize(520, 25);
add(btnCDS.get(0));
}
}
答案 0 :(得分:0)
假设这是在Applet子类中执行的, 这对我有用:
@Override
public void init()
{
// all your code between:
// cdTitles.add("Original Order");
// AND
// add(btnCDS.get(0));
// Watch out for dead code in the for loop.
}