【问题标题】:JRadioButton javaJRadioButton java
【发布时间】: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


【解决方案1】:

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

错误发生在哪一行?

乍一看有点奇怪的是以下语句:

 SouthPanel = new JPanel();

add(SouthPanel);

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

【讨论】:

    【解决方案2】:

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

      button.setActionCommand(String val);
    

    您可以在拨打电话时取回:

      button.getActionCommand(); //This would return the string you previously set.
    

    对于 TextField,如果你不设置,ActionCommand 会默认给你 TextField 中的文本。

    这就是你可能错过的地方。

    【讨论】:

      【解决方案3】:

      您已创建 JPanel 的一个实例并插入到 SouthPanel 类中。怎么可能。

      SouthPanel = new JPanel();
      add(greenButton);
      add(blueButton);
      add(cyanButton);
      
      add(SouthPanel);
      setVisible(true);
      

      您在哪里添加按钮并添加 SouthPanel。!请检查这个。似乎错误来自这里。添加了3个按钮,3次,错误显示3次。对。似乎错误来自这里。在这里检查。

      【讨论】:

        【解决方案4】:

        在此处查看完整代码。

         class SouthPanel extends JPanel {
        
         JLabel label = new JLabel("label");
        
          public 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((ActionListener) new 
            RadioButtonListener());
            blueButton.addActionListener(new RadioButtonListener());
            cyanButton.addActionListener(new RadioButtonListener());
        
            JPanel SouthPanel = new JPanel();
            add(label);
            add(greenButton);
            add(blueButton);
             add(cyanButton);
        
            add(SouthPanel);
            setVisible(true);
            JFrame frame = new JFrame();
            frame.setContentPane(this);
              frame.pack();
            frame.setVisible(true);
           }
        
              public static void main(String[] args) {
             new SouthPanel();
            }
        
            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);
              }
             }
            }
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-17
          • 2015-09-22
          • 2016-05-14
          • 2017-04-12
          • 1970-01-01
          • 2019-05-14
          • 1970-01-01
          相关资源
          最近更新 更多