Swing 在JFrame中使用JCheckBox和JRadioButton添加带有ItemsListener/ActionListener的drawRect

Swing 在JFrame中使用JCheckBox和JRadioButton添加带有ItemsListener/ActionListener的drawRect,swing,jframe,actionlistener,java,Swing,Jframe,Actionlistener,Java,我不知道在哪里添加ActionListener/ItemListener,需要帮助: 这是所需的输出: 如果在复选框中选择形状,它将绘制所选形状的类型 如果您选择radiobutton,它将填充颜色(可能是蓝色) 代码如下: import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swi

我不知道在哪里添加ActionListener/ItemListener,需要帮助:
这是所需的输出:

  • 如果在复选框中选择形状,它将绘制所选形状的类型
  • 如果您选择radiobutton,它将填充颜色(可能是蓝色)
代码如下:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;

public class ARadioCombo {

   public static void main(String args[]) {

      JFrame frame = new JFrame("Radio/Combo Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel = new JPanel(new GridLayout(0, 1));
      Border border = BorderFactory.createTitledBorder("Fill/Unfill");

      panel.setBorder(border);
      ButtonGroup group = new ButtonGroup();
      JRadioButton aRadioButton = new JRadioButton("Fill Color");
      panel.add(aRadioButton);
      group.add(aRadioButton);

      aRadioButton = new JRadioButton("Remove Fill");
      panel.add(aRadioButton);
      group.add(aRadioButton);

      Container contentPane = frame.getContentPane();
      contentPane.add(panel, BorderLayout.WEST);
      panel = new JPanel(new GridLayout(0, 1));

      border = BorderFactory.createTitledBorder("Select Shape");
      panel.setBorder(border);

      JCheckBox aCheckBox = new JCheckBox("Oval");
      panel.add(aCheckBox);

      aCheckBox = new JCheckBox("Square", true);
      panel.add(aCheckBox);

      aCheckBox = new JCheckBox("Rectangle");
      panel.add(aCheckBox);

      aCheckBox = new JCheckBox("Circle");
      panel.add(aCheckBox);

      contentPane.add(panel, BorderLayout.EAST);
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
}

您应该将ActionListeners添加到用户与之交互的任何按钮中,这里是您的JRadioButtons。那么你有这个:

  JRadioButton aRadioButton = new JRadioButton("Fill Color");
  panel.add(aRadioButton);
  group.add(aRadioButton);

  aRadioButton = new JRadioButton("Remove Fill");
  panel.add(aRadioButton);
  group.add(aRadioButton);
你可以有这样的东西:

  ActionListener myActionListener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        // TODO: put in code I want to have happen on button selection 
        // One ActionListener can likely be used for all buttons in this 
        // small program.
        // as noted below, it could be as simple as one line saying:
        // repaint();
     }
  };
  JRadioButton aRadioButton = new JRadioButton("Fill Color");
  panel.add(aRadioButton);
  group.add(aRadioButton);
  aRadioButton.addActionListener(myActionListener);

  aRadioButton = new JRadioButton("Remove Fill");
  panel.add(aRadioButton);
  group.add(aRadioButton);
  aRadioButton.addActionListener(myActionListener); // add to each radiobutton object
另外,您的jcheckbox是否也应该是JRadioButtons,在第二个ButtonGroup对象的帮助下,它一次只允许一个选择

此外:

  • 将大部分代码从main方法中提取出来,放入一个Java类中
  • 如果要在JPanel上绘图,则需要将JPanel子类化并重写其paintComponent方法。在该方法中,您将拥有if块,它将根据JRadioButtons的状态更改绘制的内容。如果这是我的项目,我会让我的主类扩展JPanel,然后绘制它的paintComponent方法。然后在我的main方法中,我将创建一个JFrame,并将这个类的一个实例添加到JFrame的contentPanel中
  • 一种解决方案是在ActionListener actionPerformed中对图形JPanel调用
    repaint()
    ,并让paintComponent方法轮询JRadioButtons的状态,并在if块中决定绘制什么
  • 你会想把所有关于截止日期和紧急情况的信息都放在你的帖子里,因为这是你的问题,不是我们的问题,也无助于我们帮助你找到解决方案。我已经冒昧地从你原来的帖子中删除了这个