我有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