布局管理器
-
流式布局
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.LEFT)); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); frame.setSize(200,200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中
public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); 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(200,200); frame.setVisible(true); } }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tHxmM9JM-1667202346810)(C:\Users\86187\AppData\Roaming\Typora\typora-user-images\image-20221031153352924.png)]
-
表格布局Grid
public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new GridLayout(3,2)); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.pack();//java函数 frame.setVisible(true); } }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OzkXV6Tm-1667202346811)(C:\Users\86187\AppData\Roaming\Typora\typora-user-images\image-20221031154154708.png)]