返回列表 發帖

佈局元件 - FlowLayout

本帖最後由 tonyh 於 2019-9-7 14:51 編輯

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





  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import java.awt.FlowLayout;
  4. public class Ch113 {
  5.        
  6.         JFrame fm;
  7.         JButton btn1,btn2,btn3,btn4,btn5,btn6;
  8.         Ch113()
  9.         {
  10.                 JButton btn1=new JButton("xxx");
  11.                 //btn1.setBounds(0, 0, 100, 100);
  12.                 //btn1.setSize(100, 100);
  13.                 //若容器被設為FlowLayout,被丟入的元件則無法設定位置與大小,大小將依內容而改變
  14.                 JButton btn2=new JButton("xxxxxxx");
  15.                 JButton btn3=new JButton("xxxxxxxxxxx");
  16.                 JButton btn4=new JButton("xxxxxxxxx    xxxxxxx");
  17.                 JButton btn5=new JButton("xxx");
  18.                 JButton btn6=new JButton("x");
  19.                
  20.                 fm=new JFrame("FlowLayout");
  21.                 fm.setBounds(100, 100, 300, 200);
  22.                 fm.setVisible(true);
  23.                 fm.setResizable(true);
  24.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.                 fm.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));  //靠左對齊、水平間距5、垂直間距10
  26.                 //new FlowLayout() 對齊方式預設為置中對齊,而物件之間的水平及垂直間距皆預設為5像素
  27.                 fm.add(btn1);
  28.                 fm.add(btn2);
  29.                 fm.add(btn3);
  30.                 fm.add(btn4);
  31.                 fm.add(btn5);
  32.                 fm.add(btn6);
  33.         }
  34.        
  35.         public static void main(String[] args) {
  36.                 new Ch113();
  37.         }
  38. }
複製代碼

  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import java.awt.FlowLayout;
  4. public class Ch104 {
  5.         JFrame fm;
  6.         JButton b1,b2,b3,b4,b5,b6;
  7.         Ch104()
  8.         {
  9.                 JButton b1=new JButton("我換換");
  10.                 b1.setBounds(0,0,100,100);
  11.                 b1.setSize(100,100);
  12.                 JButton b2=new JButton("我換換換");
  13.                 JButton b3=new JButton("我換換換換");
  14.                 JButton b4=new JButton("我換換換換換");
  15.                 JButton b5=new JButton("我換換換換換換");
  16.                 JButton b6=new JButton("我換換換換換換換");
  17.                
  18.                 JFrame fm=new JFrame("FlowLayout");
  19.                 fm.setBounds(100,100,300,200);
  20.                 fm.setVisible(true);
  21.                 fm.setResizable(true);
  22.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.                 fm.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));
  24.                 fm.add(b1);
  25.                 fm.add(b2);
  26.                 fm.add(b3);
  27.                 fm.add(b4);
  28.                 fm.add(b5);
  29.                 fm.add(b6);
  30.         }
  31.         public static void main(String[] args) {
  32.                 new Ch104();
  33.         }

  34. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import java.awt.FlowLayout;
  4. public class Ch113 {
  5.         
  6.         JFrame fm;
  7.         JButton btn1,btn2,btn3,btn4,btn5,btn6;
  8.         Ch113()
  9.         {
  10.                 JButton btn1=new JButton("xxx");
  11.                 //btn1.setBounds(0, 0, 100, 100);
  12.                 //btn1.setSize(100, 100);
  13.                 //若容器被設為FlowLayout,被丟入的元件則無法設定位置與大小,大小將依內容而改變
  14.                 JButton btn2=new JButton("xxxxxxx");
  15.                 JButton btn3=new JButton("xxxxxxxxxxx");
  16.                 JButton btn4=new JButton("xxxxxxxxx    xxxxxxx");
  17.                 JButton btn5=new JButton("xxx");
  18.                 JButton btn6=new JButton("x");
  19.                
  20.                 fm=new JFrame("FlowLayout");
  21.                 fm.setBounds(100, 100, 300, 200);
  22.                 fm.setVisible(true);
  23.                 fm.setResizable(true);
  24.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.                 fm.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));  //靠左對齊、水平間距5、垂直間距10
  26.                 //new FlowLayout() 對齊方式預設為置中對齊,而物件之間的水平及垂直間距皆預設為5像素
  27.                 fm.add(btn1);
  28.                 fm.add(btn2);
  29.                 fm.add(btn3);
  30.                 fm.add(btn4);
  31.                 fm.add(btn5);
  32.                 fm.add(btn6);
  33.         }
  34.         
  35.         public static void main(String[] args) {
  36.                 new Ch113();
  37.         }
  38. }
複製代碼

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import java.awt.FlowLayout;
  4. public class Ch113 {
  5.         
  6.         JFrame fm;
  7.         JButton btn1,btn2,btn3,btn4,btn5,btn6;
  8.         Ch113()
  9.         {
  10.                 JButton btn1=new JButton("x      456            x");
  11.                 JButton btn3=new JButton("     ______/*  ____     ");
  12.                 JButton btn4=new JButton("_____  _______________");
  13.                 JButton btn5=new JButton("     1    1   1     ");
  14.                 JButton btn6=new JButton("     1    1  4 1  4    ");
  15.                 JButton btn2=new JButton("     1    8   7  12      ");
  16.                
  17.                 fm=new JFrame("FlowLayout");
  18.                 fm.setBounds(100, 100, 300, 200);
  19.                 fm.setVisible(true);
  20.                 fm.setResizable(true);
  21.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.                 fm.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));
  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.         
  31.         public static void main(String[] args) {
  32.                 new Ch113();
  33.         }
  34. }
複製代碼

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import java.awt.FlowLayout;
  4. public class Ch111 {
  5.         JFrame fm;
  6.         JButton btn1,btn2,btn3,btn4,btn5,btn6;
  7.         Ch111()
  8.         {
  9.                 JButton btn1=new JButton("xxx");
  10.                 btn1.setBounds(0,0,100,100);
  11.                 btn1.setSize(100,100);
  12.                
  13.                 JButton btn2=new JButton("xxxxxxx");
  14.                 JButton btn3=new JButton("xxxxxxxxxxx");
  15.                 JButton btn4=new JButton("xxxxxxxxx    xxxxxxx");
  16.                 JButton btn5=new JButton("xxx");
  17.                 JButton btn6=new JButton("x");
  18.                
  19.                 JFrame fm=new JFrame("FlowLayout");
  20.                 fm.setBounds(100,100,300,200);
  21.                 fm.setVisible(true);
  22.                 fm.setResizable(true);
  23.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.                 fm.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));
  25.                
  26.                 fm.add(btn1);
  27.                 fm.add(btn2);
  28.                 fm.add(btn3);
  29.                 fm.add(btn4);
  30.                 fm.add(btn5);
  31.                 fm.add(btn6);
  32.         }
  33.         public static void main(String[] args) {
  34.                 new Ch111();

  35.         }

  36. }
複製代碼

TOP

返回列表