Java GUI之五大布局

来源:互联网 发布:软件源代码查看器 编辑:程序博客网 时间:2024/06/06 00:51

1.默认布局FlowLayout

public class SetFlowLayout {   JFrame frame;   JButton[] button;   SetFlowLayout() {      frame = new JFrame();      frame.setLayout(new FlowLayout());      button = new JButton[3];      for (int i = 0; i < 3; i++) {         button[i] = new JButton("" + i);         frame.add(button[i]);      }      frame.setBounds(350, 100, 500, 500);      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      frame.setVisible(true);   }   public static void main(String[] args) {      new SetFlowLayout();   }}
2.BorderLayout

public class SetBorderLayout {   JFrame frame;   JButton[] button;   public SetBorderLayout() {      // TODO Auto-generated constructor stub      frame = new JFrame();      frame.setTitle("BorderLayout");      frame.setLayout(new BorderLayout());      button = new JButton[5]; // 先分配数组空间范围      button[0] = new JButton("");      frame.add(button[0], BorderLayout.NORTH);      button[1] = new JButton("");      frame.add(button[1], BorderLayout.SOUTH);      button[2] = new JButton("");      frame.add(button[2], BorderLayout.EAST);      button[3] = new JButton("西");      frame.add(button[3], BorderLayout.WEST);      button[4] = new JButton("");      frame.add(button[4], BorderLayout.CENTER);      frame.setBounds(350, 100, 500, 500);      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      frame.setVisible(true);   }   public static void main(String[] args) {      new SetBorderLayout();   }}
3.BoxLayout

public class SetBoxLayout extends JFrame {   private static final long serialVersionUID = 1L;   public SetBoxLayout() {      setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS));      getContentPane().setBackground(Color.green);      add(new Button("123"));      add(new Button("123"));      add(new Button("123"));      setVisible(true);      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      setBounds(100, 50, 700, 400);   }   public static void main(String[] args) {      new SetBoxLayout();   }}
4.卡片布局CardLayout

public class SetCardLayout {   public static void main(String[] args) {      JFrame f = new JFrame();      CardLayout card = new CardLayout();      f.setLayout(card);      Container c = f.getContentPane();      JButton[] b = new JButton[6];      for (int i = 0; i < b.length; i++) {         b[i] = new JButton("" + (i + 1) + "");         c.add(b[i], "page" + (i + 1));      }      b[0].setBackground(Color.blue);      b[1].setBackground(Color.yellow);      b[2].setBackground(Color.red);      b[3].setBackground(Color.green);      b[4].setBackground(Color.orange);      b[5].setBackground(Color.pink);      f.setSize(300, 300);      f.setVisible(true);      card.show(c, "1");      while (true) {         try {            Thread.sleep(1000);         } catch (InterruptedException e) {            // TODO 自动生成的 catch             e.printStackTrace();         }         card.next(c);      }      /*       * for (int i = 0; i < 6; i++) { try { while (true) {       * Thread.sleep(1000); card.next(c); } } catch (InterruptedException e)       * { e.printStackTrace(); } }       */   }}
5.网格布局GridLayout

public class SetGridLayout {   JFrame frame;   JButton[] button;   public SetGridLayout() {      // TODO Auto-generated constructor stub      frame = new JFrame();      frame.setLayout(new GridLayout(3, 4, 1, 1));      /*       * GridLayout( ); 第一个、二哥参数表示网格分成的行、列, 第三个、四个参数表示网格之间的横纵间距       */      button = new JButton[12];      for (int i = 0; i < 12; i++) {         button[i] = new JButton("" + i); // " "+1将数字转换成字符串“1”         frame.add(button[i]);      }      frame.setBounds(350, 100, 500, 500);      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      frame.setVisible(true);   }   public static void main(String[] args) {      new SetGridLayout();   }}

原创粉丝点击