我有Jradiobuttons.i的阵列想有实现ActionListener的java的匿名类来得到的JComboBox阵列的值,所以当单选按钮,用户按下我可以做的东西,但由于这是一个数组我不能给数组索引使用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++;
}
}
thnxxxx这是工作 – pavithra
也可以告诉我什么是JRadioButton的这个instanceof意味着 – pavithra
由于你的ActionListener实现为anonymius类,所以你不需要它。如果您点击了JRadioButton(即动作来源是单选按钮),那么您需要检查/确认和处理,以便在类型转换时不应该得到类转换异常。 – Garry