返回列表 發帖

佈局元件 - FlowLayout

本帖最後由 tonyh 於 2020-8-31 20:36 編輯

運用 FlowLayout 佈局需留意以下事項:
1. 若容器被設為FlowLayout,被丟入的元件無法設定位置與大小,大小將依內容而改變。
2. FlowLayout 對齊方式預設為置中對齊,而物件之間的水平及垂直間距皆預設為5像素。





  1. import java.awt.FlowLayout;
  2. import javax.swing.JButton;
  3. import javax.swing.JFrame;

  4. public class Ch105 {

  5.         JFrame fm;
  6.         JButton btn1,btn2,btn3,btn4,btn5,btn6;

  7.         Ch105()
  8.         {
  9.                 btn1=new JButton("xxx");
  10.                 btn2=new JButton("xxxxxxx");
  11.                 btn3=new JButton("xxxxxxxxxxx");
  12.                 btn4=new JButton("xxxxxxxxx    xxxxxxx");
  13.                 btn5=new JButton("xxx");
  14.                 btn6=new JButton("x");

  15.                 //btn1.setBounds(0, 0, 100, 100);
  16.                 //btn1.setSize(100, 100);
  17.                 //若容器被設為FlowLayout,被丟入的元件則無法設定位置與大小,大小將依內容而改變

  18.                 fm=new JFrame("FlowLayout");
  19.                 fm.setBounds(100, 100, 580, 435);
  20.                 fm.setVisible(true);
  21.                 fm.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
  22.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.                 fm.add(btn1);
  24.                 fm.add(btn2);
  25.                 fm.add(btn3);
  26.                 fm.add(btn4);
  27.                 fm.add(btn5);
  28.                 fm.add(btn6);
  29.         }

  30.         public static void main(String[] args) {
  31.                 new Ch105();
  32.         }
  33. }
複製代碼

返回列表