Bài giảng Lập trình đồ họa: Quản lý trình bày

pdf 13 trang phuongnguyen 2310
Bạn đang xem tài liệu "Bài giảng Lập trình đồ họa: Quản lý trình bày", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_lap_trinh_do_hoa_quan_ly_trinh_bay.pdf

Nội dung text: Bài giảng Lập trình đồ họa: Quản lý trình bày

  1. QQuuảảnnllýý ttrrììnnhh bbààyy  Layout manager: quản lý cách trình bày của các GUI components trong một Container  Các layout manager  FlowLayout  BorderLayout  CardLayout  GridLayout  GridBagLayout  Sử dụng phương thức setLayout() của một Container để thay đổi layout 82
  2. QQuuảảnnllýý ttrrììnnhh bbààyy  FlowLayout  Là layout mặc định cho Applet và Panel  Các thành phần được sắp xếp từ trái sang phải, trên xuống dưới  Các hàm khởi tạo  FlowLayout()  FlowLayout(int alignment) Alignment có thể là FlowLayout.CENTER, LEFT, RIGHT  FlowLayout(int, int, int) 83
  3. QQuuảảnnllýý ttrrììnnhh bbààyy  FlowLayout import java.awt.*; import java.awt.event.*; public class FlowLayoutTest extends Frame implements WindowListener { public FlowLayoutTest(String title){ super(title); addWindowListener(this); setLayout(new FlowLayout(FlowLayout.CENTER, 20, 50)); for(int i = 1; i<15; i++) add(new Button("Button"+i)); setSize(575, 300); setVisible(true); } public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } 84
  4. QQuuảảnnllýý ttrrììnnhh bbààyy  FlowLayout public void windowActivated(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowOpened(WindowEvent e){} public static void main(String[] args) { FlowLayoutTest ft = new FlowLayoutTest("FlowLayout Test"); } } 85
  5. QQuuảảnnllýý ttrrììnnhh bbààyy  FlowLayout 86
  6. QQuuảảnnllýý ttrrììnnhh bbààyy  BorderLayout  Là layout mặc định cho Frame, Window, Dialog  Các thành phần được sắp xếp trên 5 khu vực khác nhau • NORTH – Đặt ở đỉnh của container. • EAST – Đặt phía bên phải của container.x` • SOUTH – Đặt ở phía dưới của container. • WEST – Đặt phía bên trái của container. • CENTER – Đặt ở giữa của container. Để thêm một thành phần vào vùng ‘North’ Button b1=new Button(“North Button”); // khai báo thành phần setLayout(new BorderLayout()); // thiết lập layout add(b1,BorderLayout.NORTH); // thêm thành phần vào layout  Khởi tạo: BorderLayout(), BorderLayout(int, int) 87
  7. QQuuảảnnllýý ttrrììnnhh bbààyy  BorderLayout 88
  8. QQuuảảnnllýý ttrrììnnhh bbààyy  CardLayout  Tự tìm hiểu trong API Documentation 89
  9. QQuuảảnnllýý ttrrììnnhh bbààyy  GridLayout  Chia Container thành các ô lưới bằng nhau  Các Component được đặt trong các ô  Mỗi ô lưới nên chứa ít nhất một thành phần  Ví dụ GridLayout l1 = new GridLayout(4, 3)  Nếu new GridLayout(0,3) nghĩa là ô có 3 cột, số hàng là bất kỳ  Không được sử dụng new GridLayout(0,0);  Các hàm khởi tạo:  GridLayout(int), GridLayout(int, int), GridLayout(int, int, int ,int) 90
  10. QQuuảảnnllýý ttrrììnnhh bbààyy  GridLayout import java.awt.*; import java.awt.event.*; public class GridLayoutTest extends Frame implements WindowListener { public GridLayoutTest(String title){ super(title); addWindowListener(this); setLayout(new GridLayout(0, 3, 5, 10)); for(int i = 1; i<15; i++) add(new Button("Button"+i)); pack(); setVisible(true); } 91
  11. QQuuảảnnllýý ttrrììnnhh bbààyy  GridLayout public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } public void windowActivated(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowOpened(WindowEvent e){} public static void main(String[] args) { GridLayoutTest ft = new GridLayoutTest(“GridLayout Test"); } } 92
  12. QQuuảảnnllýý ttrrììnnhh bbààyy  GridLayout 93
  13. QQuuảảnnllýý ttrrììnnhh bbààyy  GridBagLayout  Tự đọc 94