其他分享
首页 > 其他分享> > 三种布局管理器

三种布局管理器

作者:互联网

三种布局管理器

流式布局


```java
package com.zishi.lesson01;

import java.awt.*;

public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();

//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");

//设置为流式布局
//frame.setLayout(new FlowLayout());
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));


frame.setSize(400,400);

//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);

frame.setVisible(true);

}
}
```

东西南北中布局BorderLayout


```java
package com.zishi.lesson01;

import java.awt.*;

public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");

Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");

frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);

frame.setSize(400,400);
frame.setVisible(true);


}
}
```

表格布局


```java
package com.zishi.lesson01;

import java.awt.*;

public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("GridLayout");

Button button1 = new Button("btn1");
Button button2 = new Button("btn2");
Button button3 = new Button("btn3");
Button button4 = new Button("btn4");
Button button5 = new Button("btn5");
Button button6 = new Button("btn6");

frame.setLayout(new GridLayout(2,3));

frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

frame.pack();//Java函数 自动填充
frame.setVisible(true);

}
}
```

 

标签:管理器,Button,布局,BorderLayout,public,add,三种,new,frame
来源: https://www.cnblogs.com/yizhifei/p/15113300.html