java - JRadioButton java

标签 java user-interface jradiobutton

我是 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/

相关文章:

java - 如何将 java swing 组件大小设置为其封闭的父级大小。例如 : setting JLabel size to its enclosing JPanel size.

java - 是否可以阻止一次触发 Action 监听器?

java - 如何设置单选按钮的位置?

java - 如何找到二叉树中节点的父节点?

java - Guice @annotations 方法类似于 Guice MapBinder 方法

ios - 均匀分布 4 或 5 个按钮,具体取决于用户设备

c++ - Win32 : How to create a bordless popup window

java - 为什么我不能在 Java 中将对象转换为集合?

java.sql.SQLException : Access denied for user 'root' @'localhost' (using password: YES) 异常

java - 如何垂直设置 JRadioButtons