2017-07-24 34 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); 
     } 
    } 
    } 
    }