【发布时间】:2015-07-22 05:45:20
【问题描述】:
我有一个 Jradiobuttons 数组。我试图让 java 匿名类实现 ActionListener,所以当用户按下单选按钮时,我可以做一些事情,但由于这是一个数组,我无法使用 while 循环给出数组索引那么如何识别我正在使用的 Jradiobutton。我想获取该单选按钮的文本并将其保存在另一个变量中...我该怎么做?
这是我到目前为止所做的:
if(count!=0) {
rs=pst.executeQuery();
JRadioButton a []=new JRadioButton[count];
jPanel3.setLayout(new GridLayout());
int x=0;
ButtonGroup bg=new ButtonGroup();
while(rs.next()) {
a[x]=new JRadioButton(rs.getString("name"));
bg.add(a[x]);
jPanel3.add(a[x]);
a[x].setVisible(true);
a[x].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,a[x].getText()); //here i cant use this x...even though i make x global value of x always will be 6 becouse of while loop.
}
});
x++;
}
}
【问题讨论】:
标签: java arrays swing actionlistener anonymous-class