效果如下:
通过Box的应该虽然实现了居中,但是页面相当丑!且不能插入JTextField等文本框,总的来说相当失败!!!
import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JRadioButton; public class Frame_Box3 extends JFrame { private static final long serialVersionUID = 1L; Box b1; Box b2; Box b3; Box b4; Box b5; JButton jb1; JButton jb2; JButton jb3; JButton jb4; JRadioButton jrb1; JRadioButton jrb2; JRadioButton jrb3; Frame_Box3(){ init(); setVisible(true); } void init(){ jrb1 = new JRadioButton("选择"); jrb2 = new JRadioButton("当然"); jrb3 = new JRadioButton("或许"); jb1 = new JButton("按钮1"); jb2 = new JButton("按钮2"); jb3 = new JButton("按钮3"); jb4 = new JButton("按钮4"); b1=Box.createHorizontalBox(); b2=Box.createHorizontalBox(); b3=Box.createHorizontalBox(); b4=Box.createVerticalBox(); b5=Box.createHorizontalBox(); b1.add(Box.createVerticalStrut(20)); //添加高度为200的垂直框架 b1.add(jrb1);b1.add(jrb2);b1.add(jrb3); b1.add(Box.createHorizontalGlue()); //添加水平胶水 b2.add(jb1); b2.add(jb2); b3.add(jb3); b3.add(jb4); b4.add(b2);b4.add(b1);b4.add(b3); b5.add(b4); add(b5); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,400,200); } public static void main(String[] agrs) { new Frame_Box3(); } }