网站观赏,哪里可以做产品购物网站,WordPress能做门户网吗,机刷推广软件在一个传统的java项目中#xff0c;遇到一个需要调整布局的需求#xff0c;下面将学习网上大佬的文章#xff0c;并将过程记录下来。 1、Java swing5种布局方式
1、 边界布局#xff08;BorderLayout#xff09;2、流式布局#xff08;FlowLayout#xff09;3、网格布局… 在一个传统的java项目中遇到一个需要调整布局的需求下面将学习网上大佬的文章并将过程记录下来。 1、Java swing5种布局方式
1、 边界布局BorderLayout2、流式布局FlowLayout3、网格布局GridLayout4、盒子布局BoxLaYout5、空布局null 还有其他两种布局分别是GridBagLayout网格包布局、CardLayout卡片布局。 注意JFrame和JDialog默认布局为BorderLayoutJPanel和Applet默认布局为FlowLayout。
2、边界布局BorderLayout
实例如下
public class BorderLayoutExample extends JFrame {JButton btn1new JButton(东);JButton btn2new JButton(南);JButton btn3new JButton(西);JButton btn4new JButton(北);JButton btn5new JButton(中);BorderLayoutExample(){init();this.setTitle(边界布局);this.setResizable(true);this.setSize(300, 300);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}void init(){this.setLayout(new BorderLayout(10,5)); //默认为00水平间距10垂直间距5this.add(btn1,BorderLayout.EAST);this.add(btn2,BorderLayout.SOUTH);this.add(btn3,BorderLayout.WEST);this.add(btn4,BorderLayout.NORTH);this.add(btn5,BorderLayout.CENTER);}public static void main(String args[]){new BorderLayoutExample();}
}运行结果如下
3、流式布局FlowLayout
实例如下
public class FlowLayoutExample extends JFrame {JButton btn1new JButton(one);JButton btn2new JButton(two);JButton btn3new JButton(three);JButton btn4new JButton(four);JButton btn5new JButton(five);FlowLayoutExample(){init();this.setTitle(流式布局);this.setResizable(true);this.setSize(300, 300);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}void init(){this.setLayout(new FlowLayout(FlowLayout.LEFT,10,5)); //默认为居中;水平间距10垂直间距5this.add(btn1);this.add(btn2);this.add(btn3);this.add(btn4);this.add(btn5);}public static void main(String args[]){new FlowLayoutExample();}
}运行结果如下
4、网格布局GridLayout
实例如下
public class GridLayoutExample extends JFrame {JButton btn1new JButton(one);JButton btn2new JButton(two);JButton btn3new JButton(three);JButton btn4new JButton(four);JButton btn5new JButton(five);GridLayoutExample(){init();this.setTitle(表格布局);this.setResizable(true);this.setSize(300, 300);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}void init(){this.setLayout(new GridLayout(2,3,10,5)); //默认为1行n列2行3列水平间距10垂直间距5this.add(btn1);this.add(btn2);this.add(btn3);this.add(btn4);this.add(btn5);}public static void main(String args[]){new GridLayoutExample();}
}运行结果如下
5、盒子布局BoxLaYout
实例如下
public class BoxLayoutExample extends JFrame {JButton btn1new JButton(one);JButton btn2new JButton(two);JButton btn3new JButton(three);JButton btn4new JButton(four);JButton btn5new JButton(five);BoxLayoutExample(){init();this.setTitle(表格布局);this.setResizable(true);this.setSize(300, 300);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}void init(){this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.X_AXIS));//可以使用Box容器代替//Box box new Box(BoxLayout.Y_AXIS);box.add(btn...);box.add(creat..);this.add(btn1);this.add(btn2);this.getContentPane().add(Box.createHorizontalStrut(10)); //采用x布局时添加固定宽度组件隔开//this.getContentPane().add(Box.createVerticalStrut(5)); //采用y布局时添加固定高度组件隔开this.add(btn3);this.add(btn4);this.add(btn5);}public static void main(String args[]){new BoxLayoutExample();}
}运行结果如下
6、空布局null
实例如下
public class NullLayoutExample extends JFrame {JButton btn1new JButton(one);JButton btn2new JButton(two);JButton btn3new JButton(three);JButton btn4new JButton(four);JButton btn5new JButton(five);NullLayoutExample(){init();this.setTitle(空布局);this.setResizable(true);this.setSize(300, 300);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}void init(){this.setLayout(null);btn1.setBounds(10, 0, 100, 50); //x坐标10y坐标0组件宽100高50btn2.setBounds(20, 50, 100, 50);btn3.setBounds(30, 100, 100, 50);btn4.setBounds(40, 150, 100, 50);btn5.setBounds(50, 200, 100, 50);this.add(btn1);this.add(btn2);this.add(btn3);this.add(btn4);this.add(btn5);}public static void main(String args[]){new NullLayoutExample();}
}运行结果如下 注感兴趣的小伙伴可以试试可以直接运行