getContentPane.add để thêm Component vào Container Layout Manager là một đối tượng quyết định cách sắp... Mỗi loại Layout Manager có các nguyên tắc riêng cho việc bố trí các Compon
Trang 3 getContentPane().add để thêm Component vào Container
Layout Manager là một đối tượng quyết định cách sắp
Trang 4Layout Manager
định , nhưng hoàn toàn có thể gán cho Container một đối tượng Layout Manger theo ý muốn
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
Sử dụng phương thức setLayout (LayoutManager
Trang 5FlowLayout
Hình ảnh
Trang 6FlowLayout
chứa Component thêm vào
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
Trang 7FlowLayout – Phương thức thông dụng
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
Trang 8FlowLayout – Phương thức thông dụng
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
Trang 9FlowLayout – Phương thức thông dụng
public void setAlignment(int align)
public void setHgap(int hgap)
public void setVgap (int vgap)
public int getAlignment()
public int getHgap ()
public int getVgap ()
Trang 10FlowLayout - Demo
Trang 12//Phương thức khởi tạo các thành phần
private void initComponents() {
//MainFrame
this.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE); this.setTitle("FlowLayout");
FlowLayout layout=new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
this.setLayout(layout );
//jbts
this.jbts = new JButton[45];
for (int i = 0; i < 45; i++) {
this.jbts[i] = new JButton();
this.jbts[i].setText(String.valueOf(i+1)); this.getContentPane().add(this.jbts[i]);
}
pack();
}
}
Trang 14BorderLayout – Phương thức thông dụng
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
Trang 15BorderLayout – Phương thức thông dụng
public void setHgap(int hgap)
public void setVgap (int vgap)
public int getHgap()
public int getVgap ()
Trang 16BorderLayout - Demo
Trang 17private JButton jbtNorth;
private JButton jbtSouth;
private JButton jbtEast;
private JButton jbtWest;
private JButton jbtCenter;
//Phương thức khởi tạo
public BorderLayoutFrame() {
Trang 18//Phương thức khởi tạo các thành phần
private void initComponents() {
//MainFrame
this.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE); this.setTitle("BorderLayout");
BorderLayout layout = new BorderLayout();
this.setLayout(layout);
//jbtNorth
this.jbtNorth = new JButton("NORTH");
this.getContentPane().add(this.jbtNorth,
BorderLayout.NORTH); //jbtWest
this.jbtWest = new JButton("WEST");
this.getContentPane().add(this.jbtWest,
BorderLayout.WEST);
Trang 19
this.jbtEast = new JButton("EAST");
this.getContentPane().add(this.jbtEast,
BorderLayout.EAST); //jbtSouth
this.jbtSouth = new JButton("SOUTH");
this.getContentPane().add(this.jbtSouth,
BorderLayout.SOUTH); //jbtCenter
this.jbtCenter = new JButton("CENTER");
this.getContentPane().add(this.jbtCenter,
BorderLayout.CENTER);
Trang 20CardLayout
Hình ảnh
Trang 21CardLayout
hiển thị
(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ị
Card nào cũng có thể là Top Card
Trang 22CardLayout – Phương thức thông dụng
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
Trang 23CardLayout – Phương thức thông dụng
public void setHgap(int hgap)
public void setVgap(int vgap)
public int getHgap()
public int getVgap()
Trang 24CardLayout – Phương thức thông dụng
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)
Trang 25CardLayout - Demo
Trang 28//Phương thức khởi tạo các thành phần
private void initComponents() {
//MainFrame
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle("CardLayout");
this.setLayout(new BorderLayout());
//jpnChoice
this.jpnchoice = new JPanel();
this.jpnchoice.setLayout(new FlowLayout());
this.getContentPane().add(this.jpnchoice,
BorderLayout.NORTH);
//jbtCard1
this.jbtCard1 = new JButton("Card1");
this.jbtCard1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
jbtCard1ActionPerformed(e);
}
}); this.jpnchoice.add(this.jbtCard1);
Trang 29this.jbtCard2 = new JButton("Card2");
this.jbtCard2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { jbtCard2ActionPerformed(e);
}
});
this.jpnchoice.add(this.jbtCard2);
//jpnCards
this.jpnCards = new JPanel();
this.jpnCards.setLayout(new CardLayout());
this.getContentPane()
add(this.jpnCards, BorderLayout.CENTER);
Trang 30this.jpnCard1 = new JPanel();
this.jpnCard1.setName("Card1");
this.jpnCard1.setLayout(new FlowLayout()); this.jpnCards.add(this.jpnCard1,
Trang 31this.jpnCard2 = new JPanel();
this.jpnCard2.setName("Card2");
this.jpnCard2.setLayout(new FlowLayout()); this.jpnCards.add(this.jpnCard2,
Trang 32layout=(CardLayout)this.jpnCards.getLayout();
layout.show(this.jpnCards, jpnCard1.getName());
}
private void jbtCard2ActionPerformed(ActionEvent evt) { CardLayout
layout=(CardLayout)this.jpnCards.getLayout();
layout.show(this.jpnCards, jpnCard2.getName());
}
}
Trang 33GridLayout
bên trong một Grid với các Row và Column
Tất cả các Cell có cùng kích thước bên trong Grid
Kích thước của mỗi Cell được xác định bởi kích thước
Trang 34GridLayout
GridLayout()
GridLayout(int rows, int cols)
GridLayout(int rows, int cols, int hgap, int vgap)
Trang 35GridLayout - Demo
Trang 37GridLayout layout = new GridLayout(7,3);
this.setLayout(layout);
//jbts
this.jbts = new JButton[21];
for (int i = 0; i < 21; i++) {
this.jbts[i] = new JButton();
this.jbts[i].setText(String.valueOf(i + 1));
this.getContentPane().add(this.jbts[i]);
}
pack();
Trang 38GridBagLayout
với các Row và Column
nhau
cho việc bố trí các Component bên trong Container
theo dạng Grid
thường sử dụng nhất mà Java Platform cung cấp
38
Trang 39Các thuộc tính của GridBagContraints
Trang 40Các thuộc tính của GridBagContraints
anchor:
Trang 41Box 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
– container: chứa các Component
– axis:
Trang 42Box Layout
Trang 44HỎI VÀ ĐÁP
Trang 45DEMO
Trang 46Tham khảo