1、 边界布局(borderlayout)
2、流式布局(flowlayout)
3、网格布局(gridlayout)
4、盒子布局(boxlayout)
5、空布局(null)
还有其他两种布局,分别是gridbaglayout(网格包布局)、cardlayout(卡片布局)
注意:jframe和jdialog默认布局为borderlayout,jpanel和applet默认布局为flowlayout
边界布局示例代码:
java" id="highlighter_646874">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jframe; public class borderlayoutexample extends jframe{ jbutton btn1= new jbutton( "东" ); jbutton btn2= new jbutton( "南" ); jbutton btn3= new jbutton( "西" ); jbutton btn4= new jbutton( "北" ); jbutton btn5= new jbutton( "中" ); borderlayoutexample(){ init(); this .settitle( "边界布局" ); this .setresizable( true ); this .setsize( 200 , 200 ); this .setlocationrelativeto( null ); this .setdefaultcloseoperation(exit_on_close); this .setvisible( true ); } void init(){ this .setlayout( new borderlayout( 10 , 5 )); //默认为0,0;水平间距10,垂直间距5 this .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(); } } |
运行结果:
流式布局示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.awt.flowlayout; import javax.swing.jbutton; import javax.swing.jframe; public class flowlayoutexample extends jframe{ jbutton btn1= new jbutton( "one" ); jbutton btn2= new jbutton( "two" ); jbutton btn3= new jbutton( "three" ); jbutton btn4= new jbutton( "four" ); jbutton btn5= new jbutton( "five" ); flowlayoutexample(){ init(); this .settitle( "流式布局" ); this .setresizable( true ); this .setsize( 200 , 200 ); this .setlocationrelativeto( null ); this .setdefaultcloseoperation(exit_on_close); this .setvisible( true ); } void init(){ this .setlayout( new flowlayout(flowlayout.left, 10 , 5 )); //默认为居中;水平间距10,垂直间距5 this .add(btn1); this .add(btn2); this .add(btn3); this .add(btn4); this .add(btn5); } public static void main(string args[]){ new flowlayoutexample(); } } |
运行结果:
网格布局示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.awt.gridlayout; import javax.swing.jbutton; import javax.swing.jframe; public class gridlayoutexample extends jframe{ jbutton btn1= new jbutton( "one" ); jbutton btn2= new jbutton( "two" ); jbutton btn3= new jbutton( "three" ); jbutton btn4= new jbutton( "four" ); jbutton btn5= new jbutton( "five" ); gridlayoutexample(){ init(); this .settitle( "表格布局" ); this .setresizable( true ); this .setsize( 300 , 200 ); 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,垂直间距5 this .add(btn1); this .add(btn2); this .add(btn3); this .add(btn4); this .add(btn5); } public static void main(string args[]){ new gridlayoutexample(); } } |
运行结果:
盒子布局示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import javax.swing.box; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jframe; public class boxlayoutexample extends jframe{ jbutton btn1= new jbutton( "one" ); jbutton btn2= new jbutton( "two" ); jbutton btn3= new jbutton( "three" ); jbutton btn4= new jbutton( "four" ); jbutton btn5= new jbutton( "five" ); boxlayoutexample(){ init(); this .settitle( "表格布局" ); this .setresizable( true ); this .setsize( 300 , 200 ); 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(); } } |
运行结果:
空布局示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import javax.swing.jbutton; import javax.swing.jframe; public class nulllayoutexample extends jframe{ jbutton btn1= new jbutton( "one" ); jbutton btn2= new jbutton( "two" ); jbutton btn3= new jbutton( "three" ); jbutton btn4= new jbutton( "four" ); jbutton btn5= new 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坐标10,y坐标0,组件宽100,高50 btn2.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(); } } |
运行结果:
原文链接:https://www.idaobin.com/archives/824.html