这是我的代码:
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);
现在这给了我以下输出
而我想要这个
增加了宽度,但我无法做到这一点。任何人都可以向我提供可以帮助我的更多详细信息
最佳答案
JPanel
默认情况下使用 FlowLayout
,顾名思义,它在流程中一个接一个地布局组件...
两个选择。使用复合布局,使用 BorderLayout
作为基础,创建使用 GridLayout
作为单选按钮的 JPanel
(使用 0
code> 行和 1
列),将其添加到基础面板的 CENTER
位置。
使用 FlowLayout
及其按钮创建第二个 JPanel
。将其添加到基础 Pane 的 SOUTH
位置。
第二个选择是使用GridBagLayout
关于java - 带有单选按钮的 JFrame 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19873175/