【发布时间】:2015-03-11 16:12:10
【问题描述】:
对于下面的示例,我正在编写一个数据为 1、3、5、7、9 的 JComboBox,并期望在按下 OK 后它将变为 2、4、6、8、10。但是它只是不起作用.....任何建议将不胜感激,谢谢。
public class Test extends JFrame{
Test (){
final ArrayList<Integer> value = new ArrayList<>();
value.add(1);
value.add(3);
value.add(5);
value.add(7);
value.add(9);
final JComboBox pulldown = new JComboBox(value.toArray());
add(pulldown);
JButton ok = new JButton("OK");
add(ok);
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int data [] = {2,4,6,8,10};
value.clear();
for (int i=0; i < data.length; i++)
{
value.add(data[i]);
System.out.println(data[i]);
}
}
});
}
public static void main(String[] args) {
JFrame frame = new Test();
frame.setLayout(new FlowLayout());
frame.setSize(320, 240);
frame.setVisible(true);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
【问题讨论】:
-
怎么不行?请包括回答您的问题所需的详细信息。谢谢!
标签: java swing actionlistener jcombobox