【发布时间】:2017-07-24 20:37:30
【问题描述】:
我是 java GUI 编程的新手,在处理项目时,我收到错误无法在我的 JRadioButtons 的 addActionListener 上找到符号,我不太确定我做错了什么,因为我没有使用 JButton 时收到相同的错误。
这是我的代码:
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);
}
}
【问题讨论】:
-
抱歉,这个 sn-p 是在我尝试修改代码之后,我有新的关键字,但仍然遇到同样的问题。
-
ok 那么它应该可以工作了,也许可以看看官方
RadioButtonDemo.java或者提供一个完整的例子来测试它。 -
...这就是问题所在,它应该可以工作,但它不能工作
-
嗯,一个好的开始方法是调试您的
ActionListener,甚至调用actionPerformed方法吗?如果是这样,可能会将String actionRadio = e.getActionCommand();打印到控制台或仅使用调试器对其进行调试...如果可能未使用JRadioButton的构造函数进行设置.. -
编译器说找不到哪个
symbol?
标签: java user-interface jradiobutton