1. 程式人生 > >Java GUI之五大布局

Java GUI之五大布局

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();
}
}