Xem mẫu

  1. LẬP TRÌNH JAVA CSDL BÀI 2 SWING LAYOUT MANAGER Nguyễn Hữu Thể 1
  2. Nội dung  Flow Layout  Border Layout  Card Layout  Grid Layout  Grid Bag Layout  Box Layout  Group Layout 2
  3. Layout Manager  Một Container là một Component có thể chứa các Component khác:  JFrame, JDialog, JScollPane, Jpanel, JDesktopPane, JInternalFrame  getContentPane().add để thêm Component vào Container  Mỗi Container có một đối tượng Layout Manager  Layout Manager: sắp xếp vị trí của các Component bên trong một Container.  Các Layout Manager “implements” từ interface LayoutManager. 3
  4. Layout Manager  Mỗi Container có một đối tượng Layout Manager mặc định, người dùng có thể gán cho Container một đối tượng Layout Manger khác.  Mỗi loại Layout Manager có các nguyên tắc riêng cho việc bố trí các Component bên trong một Container.  Một Layout Manager chịu trách nhiệm bố trí các Component được thêm vào Container và khi Container thay đổi kích thước.  Sử dụng phương thức setLayout (LayoutManager mng) của Container để thay đổi cách bố trí các Component bên trong. 4
  5. FlowLayout  Flow Layout bố trí các Component trong Container theo dòng, từ trái sang phải theo thứ tự thêm vào.  Tạo dòng mới khi kích thước dòng còn lại không đủ chứa Component thêm vào.  Flow Layout bố trí vị trí các Component phụ thuộc vào kích thước của Container.  Mỗi dòng của các Component được window mặc định canh giữa theo chiều ngang . Có thể điều chỉnh canh trái hoặc phải 5
  6. JFrame FlowLayout – Cấu trúc class package FlowLayout; public class MyFlowLayout extends javax.swing.JFrame { public MyFlowLayout() { initComponents(); } private void initComponents() { setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout( new java.awt.FlowLayout()); pack(); } public static void main(String args[]) { MyFlowLayout layout = new MyFlowLayout(); layout.setVisible(true); } } 6
  7. JFrame FlowLayout – Tool Netbean package FlowLayout; public class MyFlowLayout extends javax.swing.JFrame { public MyFlowLayout() { initComponents(); } private void initComponents() { setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.FlowLayout()); pack(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MyFlowLayout().setVisible(true); } }); } } 7
  8. FlowLayout – Một số phương thức  Phương thức khởi tạo mặc định  public FlowLayout () • align: FlowLayout.CENTER • vgap: 5px, hgap: 5px  Phương thức khởi tạo có tham số  FlowLayout (int align) • align: canh lề – FlowLayout.CENTER : Canh giữa – FlowLayout.LEFT; : Canh trái – FlowLayout.RIGHT; : Canh phải • hgap: 5px, vgap: 5px 8
  9. FlowLayout – Một số phương thức  Phương thức khởi tạo có tham số  FlowLayout(int align, int vgap, int hgap) • align : canh lề • vgap : kích thước chiều ngang • hgap: chiều dọc 9
  10. FlowLayout – Một số phương thức  public void setAlignment(int align)  public void setHgap(int hgap)  public void setVgap (int vgap)  public int getAlignment()  public int getHgap ()  public int getVgap () 10
  11. public class MyFlowLayout2 extends javax.swing.JFrame { FlowLayout – Một số phương thức private JButton[] bt; public MyFlowLayout2() { initComponents(); } private void initComponents() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle("FlowLayout"); FlowLayout layout=new FlowLayout(); layout.setAlignment(FlowLayout.LEFT); setLayout(layout); bt = new JButton[15]; for (int i = 0; i < 15; i++) { bt[i] = new JButton(); bt[i].setText(String.valueOf(i+1)); getContentPane().add(bt[i]); } pack(); } public static void main(String args[]) { MyFlowLayout2 frm = new MyFlowLayout2(); frm.setVisible(true); } 11 }
  12. BorderLayout  Border Layout bố trí các Component bên trong Container theo 5 vùng: "North", "South", "East", "West" ,"Center". 12
  13. BorderLayout – Một số phương thức  Phương thức khởi tạo mặc định public BorderLayout ()  hgap = 0  vgap = 0  Phương thức khởi tạo có tham số public BorderLayout (int hgap, int vgap)  hgap: chiều ngang  vgap : chiều dọc 13
  14. BorderLayout – Một số phương thức  public void setHgap(int hgap)  public void setVgap (int vgap)  public int getHgap()  public int getVgap () 14
  15. BorderLayout – Ví dụ  Tạo JFrame dùng BorderLayout 15
  16. package BorderLayout; import java.awt.*; BorderLayout import javax.swing.*; – Một số phương thức public class MyBorderLayout extends javax.swing.JFrame { private JButton btNorth; private JButton btSouth; private JButton btEast; private JButton btWest; private JButton btCenter; public MyBorderLayout() { initComponents(); } private void initComponents() { //MainFrame setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle("BorderLayout"); BorderLayout layout = new BorderLayout(); setLayout(layout); 16
  17. //btNorth – Một số phương btNorth = new JButton("NORTH"); BorderLayout thức getContentPane().add(this.btNorth,BorderLayout.NORTH); //btWest btWest = new JButton("WEST"); getContentPane().add(btWest,BorderLayout.WEST); //btEast btEast = new JButton("EAST"); getContentPane().add(btEast,BorderLayout.EAST); //btSouth btSouth = new JButton("SOUTH"); getContentPane().add(btSouth,BorderLayout.SOUTH); //btCenter btCenter = new JButton("CENTER"); getContentPane().add(this.btCenter,BorderLayout.CENTER); pack(); } public static void main(String args[]) { MyBorderLayout frm = new MyBorderLayout(); frm.setVisible(true); } 17 }
  18. CardLayout  Card Layout quản lý nhiều Card cùng một không gian hiển thị  Card Layout giúp quản lý hai hay nhiều Component (thường là JPanel) để chia sẽ cùng một không gian hiển thị.  Chỉ duy nhất Top Card được hiển thị.  Mỗi “Card” có thể sử dụng Layout Manager riêng.  Card nào cũng có thể là Top Card  Có thể sử dụng JTabbedPane để thay cho Card Layout 18
  19. CardLayout  Có thể sử dụng JTabbedPane để thay cho Card Layout 19
  20. CardLayout - Một số phương thức  Phương thức khởi tạo mặc định  public CardLayout () • hgap = 0 • vgap = 0  Phương thức khởi tạo có tham số  public CardLayout (int hgap, int vgap) • hgap: chiều ngang • vgap : chiều dọc 20
nguon tai.lieu . vn