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

发布于 2024-11-30 17:01:49 字数 1821 浏览 0 评论 0原文

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

  • 如果您在复选框中选择了一个形状,它将绘制您选择的形状类型
  • ,如果您选择了单选按钮,它将填充颜色(可能是蓝色)

这里是代码:

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);
   }
}

I do not know where to add ActionListeners/ItemListener and need help:
This is the desired output:

  • If you selected a shape in checkboxes, it will draw a type of shape you selected
  • and if you selected radiobutton, it will fill color(maybe a blue color)

here's the code:

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);
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-12-07 17:01:49

您应该将 ActionListener 添加到用户与之交互的任何按钮,这里是 JRadioButton。所以你有这个:

  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 块,这些块将根据 JRadioButton 的状态更改绘制的内容。如果这是我的项目,我会让我的主类扩展 JPanel,然后在其 PaintComponent 方法中进行绘制。然后在我的 main 方法中,我创建一个 JFrame,并将此类的一个实例添加到 JFrame 的 contentPanel 中。
  • 一种解决方案是简单地在 ActionListener actionPerformed 中的绘图 JPanel 上调用 repaint(),并且让paintComponent方法轮询JRadioButtons的状态,并在if块中决定绘制什么。
  • 您需要在此处的帖子中不要提及截止日期和紧急情况,因为这是您的问题而不是我们的问题,并且无助于我们帮助您找到解决方案。我已冒昧地将其从您的原始帖子中删除。

You should add your ActionListeners to any buttons that the user interacts with, here your JRadioButtons. So where you have this:

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

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

You could have something like this:

  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

Also, shouldn't your JCheckBoxes also be JRadioButtons that with the help of a second ButtonGroup object only allow one selection at a time?

Also:

  • Get most all of that code out of the main method and into a Java class proper.
  • If you are going to draw on a JPanel, you'll need to subclass JPanel and override its paintComponent method. Inside of that method you'll have if blocks that will change what is drawn depending on the state of your JRadioButtons. If this were my project, I'd have my main class extend JPanel and then draw in its paintComponent method. Then in my main method, I'd create a JFrame, and add an instance of this class into the JFrame's contentPanel
  • One solution is to simply call repaint() on the drawing JPanel in the ActionListener actionPerformed, and have the paintComponent method poll the JRadioButtons for their states and in if blocks decide what to draw.
  • You'll want to leave all mention of deadlines and urgencies out of your posts here as that is your problem not ours, and doesn't help us help you to a solution. I have taken the liberty to remove this from your original post.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文