Bài giảng Lập trình cơ sở dữ liệu - Java - Bài 2: Swing Layout Manager - Nguyễn Hữu thể
LẬP TRÌNH JAVA CSDL
BÀI 2
SWING LAYOUT
MANAGER
Nguyễn Hữu Thể
1
Nội dung
Flow Layout
Border Layout
Card Layout
Grid Layout
Grid Bag Layout
Box Layout
Group Layout
2
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
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
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
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
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
FlowLayout – Một số phương thức
Phương thức khởi tạo mặc định
. publicFlowLayout ()
• align: FlowLayout.CENTER
• vgap: 5px, hgap: 5px
Phương thức khởi tạo có tham số
. FlowLayout (intalign)
• align: canh lề
– FlowLayout.CENTER : Canh giữa
– FlowLayout.LEFT; : Canh trái
– FlowLayout.RIGHT; : Canh phải
• hgap: 5px, vgap: 5px
8
FlowLayout – Một số phương thức
Phương thức khởi tạo có tham số
. FlowLayout(intalign, intvgap, inthgap)
• align : canh lề
• vgap : kích thước chiều ngang
• hgap: chiều dọc
9
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
public class MyFlowLayout2 extends javax.swing.JFrame {
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
}
BorderLayout
Border Layout bố trí các Component bên trong Container
theo 5 vùng:
"North", "South", "East", "West" ,"Center".
12
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
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
BorderLayout – Ví dụ
Tạo JFrame dùng BorderLayout
15
package BorderLayout;
import java.awt.*;
import javax.swing.*;
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
//btNorth
btNorth = new JButton("NORTH");
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
}
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
CardLayout
Có thể sử dụng JTabbedPane để thay cho Card Layout
19
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
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình cơ sở dữ liệu - Java - Bài 2: Swing Layout Manager - Nguyễn Hữu thể", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_lap_trinh_csdl_java_bai_2_swing_layout_manager_ngu.pdf

