2017-07-24 30 views
0

我是新來的Java GUI編程,並在項目上工作時,我得到錯誤找不到我的JRadioButtons addActionListener符號,我不太確定我做錯了什麼,因爲我沒有使用JButton時不會收到同樣的錯誤。JRadioButton java

這裏是我的代碼:

public void SouthPanel() { 

    JRadioButton greenButton = new JRadioButton("Green"); 
    JRadioButton blueButton = new JRadioButton("Blue"); 
    JRadioButton cyanButton = new JRadioButton("Cyan"); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(greenButton); 
    group.add(blueButton); 
    group.add(cyanButton); 

    greenButton.addActionListener(new RadioButtonListener()); 
    blueButton.addActionListener(new RadioButtonListener()); 
    cyanButton.addActionListener(new RadioButtonListener()); 

    SouthPanel = new JPanel(); 
    add(greenButton); 
    add(blueButton); 
    add(cyanButton); 

    add(SouthPanel); 
    setVisible(true); 
} 
private class RadioButtonListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 

     String actionRadio = e.getActionCommand(); 

     if (actionRadio.equals("Green")) { 
     label.setForeground(Color.GREEN); 
     } 
     else if (actionRadio.equals("Blue")) { 
     label.setForeground(Color.BLUE); 
     } 
     else if (actionRadio.equals("Cyan")) { 
     label.setForeground(Color.CYAN); 
     } 
    } 
+0

對不起,這段代碼是在我試着用代碼搞混了一下之後,我有了新的關鍵字,但仍然遇到了同樣的問題。 –

+0

好了,那麼它應該可以工作,也許看看官方['RadioButtonDemo.java'](https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle。 com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java)或者提供一個完整的示例來測試它。 – xander

+0

...多數民衆贊成在這個問題上,它應該工作,但它不工作 –

回答

1

據我所知,錯誤「無法找到符號」,通常是指不能由編譯器來解決一個變量。

錯誤發生在哪條線上?

什麼乍一看似乎有點奇怪的聲明如下:

SouthPanel = new JPanel(); 

add(SouthPanel);

因爲SouthPanel是你的方法的名稱,你沒有給一個名字你SouthPanel(? )對象。

0

要在ButtonRadioButton使用getActionCommand(),你應該預先設定的ActionCommand

​​

,您仍然可以把它找回來,當你做出一個電話:

button.getActionCommand(); //This would return the string you previously set. 

對於TextField,如果不設置它,ActionCommand會默認爲您提供TextField中的文本。

這就是你可能錯過的地方。

0

您已經創建了一個JPanel實例並插入到了SouthPanel類中。如何做呢。

SouthPanel = new JPanel(); 
add(greenButton); 
add(blueButton); 
add(cyanButton); 

add(SouthPanel); 
setVisible(true); 

到哪裏添加按鈕並添加SouthPanel。請檢查這一個。看錯誤是從這裏.3個按鈕被添加,3次,錯誤顯示3次。右圖。看到錯誤是從這裏。在這裏檢查。

0

請在這裏查看完整的代碼。

class SouthPanel extends JPanel { 

JLabel label = new JLabel("label"); 

    public SouthPanel() { 

    JRadioButton greenButton = new JRadioButton("Green"); 
    JRadioButton blueButton = new JRadioButton("Blue"); 
    JRadioButton cyanButton = new JRadioButton("Cyan"); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(greenButton); 
    group.add(blueButton); 
    group.add(cyanButton); 

    greenButton.addActionListener((ActionListener) new 
    RadioButtonListener()); 
    blueButton.addActionListener(new RadioButtonListener()); 
    cyanButton.addActionListener(new RadioButtonListener()); 

    JPanel SouthPanel = new JPanel(); 
    add(label); 
    add(greenButton); 
    add(blueButton); 
    add(cyanButton); 

    add(SouthPanel); 
    setVisible(true); 
    JFrame frame = new JFrame(); 
    frame.setContentPane(this); 
     frame.pack(); 
    frame.setVisible(true); 
    } 

     public static void main(String[] args) { 
    new SouthPanel(); 
    } 

    private class RadioButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 

    String actionRadio = e.getActionCommand(); 

     if (actionRadio.equals("Green")) { 
     label.setForeground(Color.GREEN); 
     } else if (actionRadio.equals("Blue")) { 
     label.setForeground(Color.BLUE); 
     } else if (actionRadio.equals("Cyan")) { 
     label.setForeground(Color.CYAN); 
     } 
    } 
    } 
    }