在 JFrame 中使用 JCheckBox 和 JRadioButton 添加带有 ItemsListener/ActionListener 的 drawRect
我不知道在哪里添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将 ActionListener 添加到用户与之交互的任何按钮,这里是 JRadioButton。所以你有这个:
你可以有这样的东西:
另外,你的 JCheckBox 不应该也是 JRadioButtons 吗?在第二个 ButtonGroup 对象的帮助下一次只允许一个选择?
另外:
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:
You could have something like this:
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:
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.