Java 框架上未显示其他射线组
ButtonGroup在直接添加到框架时不起作用,这就是我直接在框架上添加单选按钮的原因。然而,我只看到单选按钮。我在这里用的方法不对吗?有人能帮我吗Java 框架上未显示其他射线组,java,swing,Java,Swing,ButtonGroup在直接添加到框架时不起作用,这就是我直接在框架上添加单选按钮的原因。然而,我只看到单选按钮。我在这里用的方法不对吗?有人能帮我吗 protected ButtonGroup radioGroup = new ButtonGroup(); protected JRadioButton small = new JRadioButton("small"); protected JRadioButton medium = new JRadioButton("medium")
protected ButtonGroup radioGroup = new ButtonGroup();
protected JRadioButton small = new JRadioButton("small");
protected JRadioButton medium = new JRadioButton("medium");
protected JRadioButton large = new JRadioButton("large");
public PlayingWithShapes()
{
shapes.add(circle);
shapes.add(square);
shapes.add(rectangle);
shapes.add(triangle);
colors.add(red);
colors.add(blue);
colors.add(magenta);
radioGroup.add(small);
radioGroup.add(medium);
radioGroup.add(large);
menuBar.add(shapes);
menuBar.add(colors);
JFrame frame = new JFrame("Playing With Shapes");
frame.setLayout(new GridLayout(1,2));
frame.setJMenuBar(menuBar);
frame.add(this);
frame.add(small);
frame.add(medium);
frame.add(large);
frame.setSize(600,400);
框架的默认布局是边框布局。只能将单个构件添加到BorderLayout的中心
您需要:
本教程还有一个关于布局管理器的章节,您应该仔细阅读。
JFrame
默认情况下使用了边框布局
,因此您有点自作自受there@MadProgrammer有什么例子吗?谢谢you@JayGorio有什么例子吗?
-已经给了你一个例子。你读过答案了吗?还是我浪费时间键入答案了?@JayGorio使用更适用的布局管理器,看看JFrame
默认情况下使用BorderLayout
,所以你把自己搞砸了there@MadProgrammer有什么例子吗?谢谢you@JayGorio有什么例子吗?
-已经给了你一个例子。你读过答案了吗?还是我浪费时间键入答案了?@JayGorio使用了一个更适用的布局管理器,看看谢谢,但它只显示单选按钮,而不显示我的其他形状displaying@JayGorio, . 我们不是来为您编写代码的。您已获得一个指向Swing教程的链接,该教程提供了使用布局管理器的示例。您不可能在5分钟内阅读教程并使用示例。从工作代码开始,修改它,而不是使用您的代码。我们也不知道你的“形状”和“颜色”变量是什么。还有一个关于“如何使用菜单”的教程,你可以看看。您获得教程的链接是有原因的。把它当作一种资源,好的,我知道了,它开始工作了。谢谢您的努力。@JayGorio,很高兴评论和教程对您有所帮助。别忘了“接受”答案,这样人们就知道问题已经解决了。谢谢,但它只显示单选按钮,而不显示我的其他形状displaying@JayGorio, . 我们不是来为您编写代码的。您已获得一个指向Swing教程的链接,该教程提供了使用布局管理器的示例。您不可能在5分钟内阅读教程并使用示例。从工作代码开始,修改它,而不是使用您的代码。我们也不知道你的“形状”和“颜色”变量是什么。还有一个关于“如何使用菜单”的教程,你可以看看。您获得教程的链接是有原因的。把它当作一种资源,好的,我知道了,它开始工作了。谢谢您的努力。@JayGorio,很高兴评论和教程对您有所帮助。别忘了“接受”答案,这样人们就知道问题已经解决了。
frame.add(small);
frame.add(medium);
frame.add(large);