Java 带有单选按钮的 JFrame 布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19873175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:57:23  来源:igfitidea点击:

JFrame layout with radio buttons

javaswingjframejradiobutton

提问by Santino 'Sonny' Corleone

This is my code:

这是我的代码:

frame2 = new JFrame("Confirmation");
        frame2.setLayout(new BorderLayout());
        JRadioButton y,n,c;
         panel = new JPanel();
          ButtonGroup buttonGroup = new ButtonGroup();
          y = new JRadioButton("Add");
          buttonGroup.add(y);
          panel.add(y);
          n = new JRadioButton("Update");
          buttonGroup.add(n);
          panel.add(n);
          c = new JRadioButton("Delete");
          buttonGroup.add(c);
          panel.add(c);
          y.setSelected(true);
          b1=new JButton();
          b1.setBounds(300,100,2,2);
          b1.setIcon(new ImageIcon(searchresult.class.getResource("/images/yes.png")));
          b2=new JButton();
          b2.setBounds(100,10,2,2);
          b2.setIcon(new ImageIcon(searchresult.class.getResource("/images/no.png")));
          panel.add(b1);
          panel.add(b2);
          frame2.add(panel);
          frame2.setSize(182,150);
          frame2.setVisible(true);

Right now this gives me the following output enter image description here

现在这给了我以下输出 在此处输入图片说明

whereas I want this enter image description here

而我想要这个 在此处输入图片说明

with an increased width but I am not able to do it..Could anyone provide me with further details that could help me

宽度增加了,但我做不到..谁能向我提供可以帮助我的更多细节

采纳答案by MadProgrammer

JPaneluses a FlowLayoutby default, which, as the name suggests, layouts out components one after the after, in a flow...

JPanelFlowLayout默认情况下使用 a ,顾名思义,它在流程中一个接一个地布置组件......

Two choices. Use a compound layout, using BorderLayoutas the base, create JPanelthat uses a GridLayoutfor the radio buttons (using 0rows and 1column), add this to the CENTERposition of the base panel.

两个选择。使用复合布局,BorderLayout用作基础,创建JPanel使用GridLayout单选按钮(使用0行和1列)的 a,将其添加到CENTER基础面板的位置。

Create a second JPanelusing a FlowLayoutand your buttons to it. Add this to the SOUTHposition of the base pane.

JPanel使用 aFlowLayout和您的按钮创建第二个。将此添加到SOUTH基本窗格的位置。

Second choice is to use a GridBagLayout

第二种选择是使用 GridBagLayout

Take a look at Laying out Components within a Containerfor more details

查看在容器内布置组件以了解更多详细信息