我是java GUI编程的新手,当我在项目中遇到错误时,我的JRadioButtons在我的addActionListener上找不到符号,我不太确定我做错了什么,因为我在使用JButtons时没有收到同样的错误。
下面是我的代码:
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);
}
}发布于 2017-07-24 18:06:29
据我所知,错误“找不到symbol”通常是指编译器无法解析的变量。
错误发生在哪一行?
乍一看似乎有点奇怪的是下面的陈述:
SouthPanel = new JPanel();和add(SouthPanel);
由于SouthPanel是您的方法的名称,而您没有为您的SouthPanel (?)对象。
发布于 2017-07-24 18:07:04
要在Button或RadioButton上使用getActionCommand(),应事先设置ActionCommand
button.setActionCommand(String val);当您调用以下命令时,您将能够将其取回:
button.getActionCommand(); //This would return the string you previously set.对于TextField,如果您不对其进行设置,ActionCommand将默认为您提供TextField中的文本。
这就是你可能漏掉的地方。
发布于 2017-07-24 18:34:02
您已经创建了JPanel的一个实例并插入到SouthPanel类中。这是怎么做到的。
SouthPanel = new JPanel();
add(greenButton);
add(blueButton);
add(cyanButton);
add(SouthPanel);
setVisible(true);添加按钮和添加SouthPanel的位置。!请检查此one.Seems错误是否来自此处。添加了3个按钮,连续3次,错误显示为3 times.right.Seems error is here。在这里检查。
https://stackoverflow.com/questions/45277115
复制相似问题