gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:49 24 4
gpt4 key购买 nike

我不知道在哪里添加 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);
}
}

最佳答案

您应该将您的 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 不应该也是借助第二个 ButtonGroup 对象一次只允许一个选择的 JRadioButton 吗?

还有:

  • 将大部分代码从 main 方法中取出并放入适当的 Java 类中。
  • 如果要在 JPanel 上绘图,则需要子类化 JPanel 并覆盖其 paintComponent 方法。在该方法中,您将拥有 if block ,这些 block 将根据 JRadioButtons 的状态更改绘制的内容。如果这是我的项目,我会让我的主类扩展 JPanel,然后在它的 paintComponent 方法中绘制。然后在我的主要方法中,我将创建一个 JFrame,并将此类的一个实例添加到 JFrame 的 contentPanel
  • 一种解决方案是在 ActionListener actionPerformed 中的绘图 JPanel 上简单地调用 repaint(),并让 paintComponent 方法轮询 JRadioButton 的状态,并在 if block 中决定绘制什么。<
  • 您要在此处的帖子中完全提及截止日期和紧急情况,因为那是您的问题而不是我们的问题,并且无助于我们帮助您找到解决方案。我冒昧地从您的原始帖子中删除了此内容。

关于swing - 在 JFrame 中使用 JCheckBox 和 JRadioButton 添加带有 ItemsListener/ActionListener 的 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7132034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com