我是 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);
}
}
最佳答案
据我所知,错误“找不到符号”通常是指编译器无法解析的变量。
错误发生在哪一行?
乍一看似乎有点奇怪的是以下声明:
SouthPanel = new JPanel();
和add(SouthPanel);
因为 SouthPanel 是您的方法的名称,并且您没有为 SouthPanel (?) 对象指定名称。
关于java - JRadioButton java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45277115/