[英]Getting the isSelected value of a selected radiobutton in separate class
我意識到這可能是一個菜鳥問題的定義,但我很固執,將不勝感激。
我有一個動作actionlistener
和JRadioButton
,它們在ValublesMain
類中這樣聲明。
JRadioButton name = new JRadioButton("Name", true);
name.addActionListener(new NameListener());
像這樣進一步聲明NameListener
。
class NameListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
display.setText("");
for(Valuble item : valubles)
if(name.isSelected()){
//Bunch of code and stuff
}
}
}
我的問題是名稱不可見,我想知道我在做什么錯。 我以為NameListener可以看到名稱,因為它是在這里聲明的。
name.addActionListener(new NameListener());
我在這里想念什么?
更改actionPerformed
方法以從event
獲取JRadioButton
public void actionPerformed(ActionEvent event) {
display.setText("");
for(Valuble item : valubles)
if(((JRadioButton)event.getSource()).isSelected()){
//Bunch of code and stuff
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.