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
JFrame layout with radio buttons
提问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
现在这给了我以下输出
whereas I want this
而我想要这个
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
JPanel
uses a FlowLayout
by default, which, as the name suggests, layouts out components one after the after, in a flow...
JPanel
FlowLayout
默认情况下使用 a ,顾名思义,它在流程中一个接一个地布置组件......
Two choices. Use a compound layout, using BorderLayout
as the base, create JPanel
that uses a GridLayout
for the radio buttons (using 0
rows and 1
column), add this to the CENTER
position of the base panel.
两个选择。使用复合布局,BorderLayout
用作基础,创建JPanel
使用GridLayout
单选按钮(使用0
行和1
列)的 a,将其添加到CENTER
基础面板的位置。
Create a second JPanel
using a FlowLayout
and your buttons to it. Add this to the SOUTH
position 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
查看在容器内布置组件以了解更多详细信息