首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JRadioButton java

JRadioButton java
EN

Stack Overflow用户
提问于 2017-07-24 17:37:38
回答 4查看 255关注 0票数 0

我是java GUI编程的新手,当我在项目中遇到错误时,我的JRadioButtons在我的addActionListener上找不到符号,我不太确定我做错了什么,因为我在使用JButtons时没有收到同样的错误。

下面是我的代码:

代码语言:javascript
复制
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);
      }
    }
EN

回答 4

Stack Overflow用户

发布于 2017-07-24 18:06:29

据我所知,错误“找不到symbol”通常是指编译器无法解析的变量。

错误发生在哪一行?

乍一看似乎有点奇怪的是下面的陈述:

代码语言:javascript
复制
 SouthPanel = new JPanel();

add(SouthPanel);

由于SouthPanel是您的方法的名称,而您没有为您的SouthPanel (?)对象。

票数 1
EN

Stack Overflow用户

发布于 2017-07-24 18:07:04

要在ButtonRadioButton上使用getActionCommand(),应事先设置ActionCommand

代码语言:javascript
复制
  button.setActionCommand(String val);

当您调用以下命令时,您将能够将其取回:

代码语言:javascript
复制
  button.getActionCommand(); //This would return the string you previously set.

对于TextField,如果您不对其进行设置,ActionCommand将默认为您提供TextField中的文本。

这就是你可能漏掉的地方。

票数 0
EN

Stack Overflow用户

发布于 2017-07-24 18:34:02

您已经创建了JPanel的一个实例并插入到SouthPanel类中。这是怎么做到的。

代码语言:javascript
复制
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。在这里检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45277115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档