Bài giảng Lập trình Java cơ sở dữ liệu - Bài 2: Swing Layout Manager - Nguyễn Hữu Thể

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.

pdf 34 trang yennguyen 1060
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình Java cơ sở dữ liệu - 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

Tóm tắt nội dung tài liệu: Bài giảng Lập trình Java cơ sở dữ liệu - Bài 2: Swing Layout Manager - Nguyễn Hữu Thể

Bài giảng Lập trình Java cơ sở dữ liệu - Bài 2: Swing Layout Manager - Nguyễn Hữu Thể
1 
LẬP TRÌNH JAVA CSDL 
BÀI 2 
SWING LAYOUT 
MANAGER 
Nguyễn Hữu Thể 
2 
Nội dung 
 Flow Layout 
 Border Layout 
 Card Layout 
 Grid Layout 
 Grid Bag Layout 
 Box Layout 
 Group Layout 
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. 
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. 
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 
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); 
 } 
} 
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); 
 } 
 }); 
 } 
} 
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 
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 
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 () 
11 
FlowLayout – Một số phương thức 
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); 
 } 
} 
12 
BorderLayout 
 Border Layout bố trí các Component bên trong Container 
theo 5 vùng: 
 "North", "South", "East", "West" ,"Center". 
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 
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 () 
15 
BorderLayout – Ví dụ 
 Tạo JFrame dùng BorderLayout 
16 
BorderLayout – Một số phương thức 
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); 
17 
BorderLayout – Một số phương thức 
 //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); 
 } 
} 
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 
19 
CardLayout 
 Có thể sử dụng JTabbedPane để thay cho Card Layout 
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 
21 
CardLayout - Một số phương thức 
 public void setHgap(int hgap) 
 public void setVgap(int vgap) 
 public int getHgap() 
 public int getVgap() 
 public void next (Container parent) 
 public void previous(Container parent) 
 public void first(Container parent) 
 public void last(Container parent) 
 public void show(Container parent, String 
name) 
22 
CardLayout – Ví dụ 
23 
CardLayout – Ví dụ 
//CardLayout With tool Netbean 
public class CardLayoutFrame extends javax.swing.JFrame { 
 public CardLayoutFrame() { 
 initComponents(); 
 } 
 private void initComponents() { 
 setDefaultCloseOperation( 
 javax.swing.WindowConstants.EXIT_ON_CLOSE); 
 getContentPane().setLayout(new java.awt.CardLayout()); 
 pack(); 
 } 
 public static void main(String args[]) { 
 /* Set the Nimbus look and feel ... */ 
 java.awt.EventQueue.invokeLater(new Runnable() { 
 public void run() { 
 new CardLayoutFrame().setVisible(true); 
 } 
 }); 
 } 
} 
24 
CardLayout – Ví dụ 
//CardLayout With tool Eclipse 
public class CardLayout extends JFrame { 
private JPanel contentPane; 
public static void main(String[] args) { 
EventQueue.invokeLater(new Runnable() { 
public void run() { 
try { 
CardLayout frame = new CardLayout(); 
frame.setVisible(true); 
} catch (Exception e) { e.printStackTrace(); } 
} 
}); 
} 
public CardLayout() { 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setBounds(100, 100, 450, 300); 
contentPane = new JPanel(); 
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
contentPane.setLayout(new java.awt.CardLayout(0, 0)); 
setContentPane(contentPane); 
} 
} 
25 
CardLayout – Ví dụ 
//CardLayout With tool Eclipse – 4 button 
public class CardLayout extends JFrame { 
private JPanel contentPane; 
public CardLayout() { 
setTitle("CardLayout"); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setBounds(100, 100, 210, 113); 
contentPane = new JPanel(); 
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
setContentPane(contentPane); 
contentPane.setLayout(new java.awt.CardLayout(0, 0)); 
JPanel panel = new JPanel(); 
contentPane.add(panel, "Card Layout"); 
JButton btnNewButton1 = new JButton("Button 1"); 
panel.add(btnNewButton1); 
JButton btnNewButton2 = new JButton("Button 2"); 
panel.add(btnNewButton2); 
JButton btnNewButton3 = new JButton("Button 3"); 
panel.add(btnNewButton3); 
JButton btnNewButton4 = new JButton("Button 4"); 
panel.add(btnNewButton4); 
} 
26 
GridLayout 
 Grid Layout bố trí các Component của Container vào bên 
trong một Grid với các Row và Column. 
 Mỗi Component được bố trí trong 1 Cell của lưới. 
 Tất cả các Cell có cùng kích thước bên trong Grid. 
 Các Component được thêm vào Container theo thứ tự từ trái 
sang phải, từ trên xuống dưới (mặc định). 
 Kích thước của mỗi Cell được xác định bởi kích thước của 
Container. 
27 
GridLayout 
 GridLayout() 
 rows: 1 
 1 Component / 1 Column 
 GridLayout(int rows, int cols) 
 GridLayout(int rows, int cols, int hgap, int 
vgap) 
28 
GridLayout – Ví dụ 1 
29 
CardLayout – Ví dụ 
// GridLayout With tool Eclipse 
public class GridLayoutFrame extends JFrame { 
private JPanel contentPane; 
private JButton[] bt; 
public GridLayoutFrame() { 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setBounds(100, 100, 450, 300); 
contentPane = new JPanel(); 
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
setContentPane(contentPane); 
contentPane.setLayout(new GridLayout(1, 0, 0, 0)); 
this.bt = new JButton[21]; 
 for (int i = 0; i < 15; i++) { 
 this.bt[i] = new JButton(); 
 this.bt[i].setText(String.valueOf(i + 1)); 
 this.getContentPane().add(this.bt[i]); 
 } 
} 
30 
GridLayout – Ví dụ 2 
31 
CardLayout – Ví dụ 
// GridLayout With tool Eclipse 
public class GridLayoutFrame extends JFrame { 
private JPanel contentPane; 
private JButton[] bt; 
public GridLayoutFrame() { 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setBounds(100, 100, 450, 300); 
contentPane = new JPanel(); 
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
setContentPane(contentPane); 
contentPane.setLayout(new GridLayout(5, 3));//5 dòng 3 cột 
this.bt = new JButton[21]; 
 for (int i = 0; i < 15; i++) { 
 this.bt[i] = new JButton(); 
 this.bt[i].setText(String.valueOf(i + 1)); 
 this.getContentPane().add(this.bt[i]); 
 } 
} 
32 
GridBag Layout 
 GridBag Layout bố trí các Component trong một Grid với các 
Row và Column. 
 Mỗi Component bên trong Grid được RowSpan và 
ColumnSpan (giống table HTML) 
 Width và Height của các Row/Column có thể khác nhau. 
 GridBag Layout là một Layout Manager rất linh động cho 
việc bố trí các Component bên trong Container theo dạng 
Grid. 
 GridBag Layout là một trong các Layout Manager thường 
sử dụng nhất mà Java Platform cung cấp. 
33 
Box Layout 
 Box Layout bố trí các Component bên trong Container theo 1 
dòng theo trục X, hoặc là trục Y. 
 BoxLayout(Container container, int align) 
 container: chứa các Component 
 align : 
• BoxLayout.X_AXIS : Trục X 
• BoxLayout.Y_AXIS : Trục Y 
34 
Group Layout 
 Group Layout bố trí các Component bên trong Container 
theo chiều ngang và chiều dọc. 
 Sự bố trí được thực hiện theo mỗi chiều riêng lẽ. 

File đính kèm:

  • pdfbai_giang_lap_trinh_java_co_so_du_lieu_bai_2_swing_layout_ma.pdf