流式布局

  1. import java.awt.*;
  2. public class TestFlowLayout {
  3. public static void main(String[] args) {
  4. Frame frame = new Frame();
  5. //按钮组件
  6. Button button1 = new Button("button1");
  7. Button button2 = new Button("button2");
  8. Button button3 = new Button("button3");
  9. //设置为流式布局
  10. frame.setLayout(new FlowLayout());//默认居中
  11. //frame.setLayout(new FlowLayout(FlowLayout.LEFT));//居左
  12. //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//居右
  13. frame.setSize(200, 200);
  14. //添加按钮
  15. frame.add(button1);
  16. frame.add(button2);
  17. frame.add(button3);
  18. frame.setVisible(true);
  19. }
  20. }

image.png

东西南北中

  1. import java.awt.*;
  2. public class TestBorderLayout {
  3. public static void main(String[] args) {
  4. Frame frame = new Frame("TestBorderLayout");
  5. Button east = new Button("East");
  6. Button west = new Button("West");
  7. Button south = new Button("South");
  8. Button north = new Button("North");
  9. Button center = new Button("Center");
  10. frame.add(east,BorderLayout.EAST);
  11. frame.add(west,BorderLayout.WEST);
  12. frame.add(south,BorderLayout.SOUTH);
  13. frame.add(north,BorderLayout.NORTH);
  14. frame.add(center,BorderLayout.CENTER);
  15. frame.setSize(200,200);
  16. frame.setVisible(true);
  17. }
  18. }

image.png

表格布局

  1. package gui;
  2. import java.awt.*;
  3. public class TestGridLayout {
  4. public static void main(String[] args) {
  5. Frame frame = new Frame("TestGridLayout");
  6. Button btn1 = new Button("btn1");
  7. Button btn2 = new Button("btn2");
  8. Button btn3 = new Button("btn3");
  9. Button btn4 = new Button("btn4");
  10. Button btn5 = new Button("btn5");
  11. Button btn6 = new Button("btn6");
  12. frame.setLayout(new GridLayout(3, 2));
  13. frame.add(btn1);
  14. frame.add(btn2);
  15. frame.add(btn3);
  16. frame.add(btn4);
  17. frame.add(btn5);
  18. frame.add(btn6);
  19. frame.pack();
  20. frame.setVisible(true);
  21. }
  22. }

image.png