2017-05-24 174 views
0

我意識到這可能是noob問題的定義,但我相當卡住,非常感謝任何幫助。在單獨的類中獲取選定的單選按鈕的isSelected值

我有一個actionlistenerJRadioButton它在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()); 

我在這裏錯過了什麼?

回答

3

更改actionPerformed方法來從event

public void actionPerformed(ActionEvent event) { 

     display.setText(""); 

     for(Valuble item : valubles)  
     if(((JRadioButton)event.getSource()).isSelected()){ 

      //Bunch of code and stuff 

     } 

    } 
+0

這個問題解決了,非常感謝你的JRadioButton! – VICWICIV

相關問題