我是新來的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);
}
}
對不起,這段代碼是在我試着用代碼搞混了一下之後,我有了新的關鍵字,但仍然遇到了同樣的問題。 –
好了,那麼它應該可以工作,也許看看官方['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
...多數民衆贊成在這個問題上,它應該工作,但它不工作 –