Tải bản đầy đủ (.pptx) (34 trang)

SỬ DỤNG LAYOUT MANAGER TRONG THIẾT KẾ GIAO DIỆN

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (281.02 KB, 34 trang )

SỬ DỤNG LAYOUT MANAGER
TRONG THIẾT KẾ GIAO DIỆN

Nhóm 3


NỘI DUNG

Giới thiệu
Chức năng của các Layout
Demo


I. GIỚI THIỆU
1.

Layout manager là gì?
a.

Layout: Cách bố trí các thành phần trên container, hay nói cách
khác là cách sắp xếp các thành phần vào các vị trí đặc biệt trong
container. Việc này được quản lý bởi LayoutManager.

b.

LayoutManager: Sắp xếp vị trí của các components một cách tự
động, thay vì xác định chính xác về vị trí và kích thước của các
components trong container. Nếu chúng ta không sử dụng
LayoutManger thì các component cũng được sắp xếp theo
LayoutManager mặc định.



I. GIỚI THIỆU
1.

Layout manager là gì?
c.

Những hạn chế khi không sử dụng layout: Chúng ta cũng có thể
sắp xếp các component bằng tay, tuy nhiên nó gặp nhiều rắc rối:
.Khó

khăn trong việc xử lý nhiều component.

.Thường

thì thông tin chiều rộng và chiều cao của component
không được đưa ra khi sắp xếp chúng.


I. GIỚI THIỆU
2.

Phân loại các Layout manager
Các loại LayoutManager hổ trợ trong java bao gồm:
.BorderLayout
.FlowLayout
.BoxLayout
.CardLayout
.GridLayout
.GridBagLayout

.GroupLayout
.SpringLayout


II. CÁC LOẠI LAYOUT MANAGER
1.

BorderLayout
a.

Giới thiệu


II. CÁC LOẠI LAYOUT MANAGER
1.

BorderLayout

a.

Đặc điểm:

a.

.

Sắp xếp các thành phần(component) vào 5 khu vực: East, West, North, South và
Center. Mỗi khu vực chỉ có thể chứa một thành phần và mỗi thành phần trong từng khu
vực được xác định bởi các hằng số tương ứng: NORTH, SOUTH, EAST, WEST và
CENTER. Nếu chỉ có container chỉ chứa một component thì nó sẽ phủ kín container đó.


.

Là layout manager mặc định của Jframe.

Cú pháp khai báo:

public class BorderLayout
extends Object
implements LayoutManager2, Serializable


II. CÁC LOẠI LAYOUT MANAGER
1.

BorderLayout
c.

Hàm khởi tạo
public BorderLayout
public BorderLayout(int hgap, int vgap)

e.

Một số phương thức hay sử dụng
public void addLayoutComponent(Component comp, Object
constraints)
public void addLayoutComponent(String name, Component comp) 



II. CÁC LOẠI LAYOUT MANAGER
1.

BorderLayout
d.

Một số phương thức hay sử dụng
public void setHgap(int hgap)
public void setVgap(int vgap)
public Object getConstraints(Component comp)
public float getLayoutAlignmentX(Container parent)
public float getLayoutAlignmentY(Container parent)
public Component getLayoutComponent(Container target, Object constraints)
public Component getLayoutComponent(Object constraints)

Tham khảo thêm tại nguồn: http

://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html


II. CÁC LOẠI LAYOUT MANAGER
2.
a.

a.

FlowLayout
Đặc điểm:
.


Sắp xếp các components trên cùng một dòng từ góc trên bên trái đến góc dưới
bên phải của màn hình, khi nào hết chỗ chứa nó sẽ tự động xuống dòng, ta cũng có
thể điều chỉnh hướng xuất hiện của components.

.

Mặc định khi một JPanel được khởi tạo thì bản thân lớp chứa này sẽ có kiểu Layout
là FlowLayout

Cú pháp khai báo
public class FlowLayout
extends Object
implements LayoutManager, Serializable


II. CÁC LOẠI LAYOUT MANAGER
FlowLayout

2.
c.

Hàm khởi tạo
public FlowLayout()
public FlowLayout(int align)
public FlowLayout(int align, int hgap, int vgap)

e.

Một số phương thức thường dùng
public void addLayoutComponent(Component comp, Object

constraints)


II. CÁC LOẠI LAYOUT MANAGER
FlowLayout

2.
d.

Một số phương thức thường dùng
public int getAlignment
public void setAlignment()
public void setHgap(int hgap) 
public void setVgap(int vgap)
public void getHgap() 


II. CÁC LOẠI LAYOUT MANAGER
FlowLayout

2.
d.

Một số phương thức thường dùng
public void getVgap()
public void setAlignOnBaseline(Boolean alignOnBaseline) 
public bolean getAlignOnBaseline() 
public void removeLayoutComponent(Component comp) 
Tham khảo thêm tại nguồn: http://
docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html



II. CÁC LOẠI LAYOUT MANAGER
BoxLayout

3.
a.

Đặc điểm: Cho phép đặt nhiều components theo các dòng và các
cột liên tiếp nhau. Trong BoxLayout thường dùng Jpanel để hiển thị
và nó có thể chứa nhiều Jpanel khác nhau.

b.

Cú pháp khai báo
public class BoxLayout
extends Object
implements LayoutManager LayoutManager2, Serializable


II. CÁC LOẠI LAYOUT MANAGER
BoxLayout

3.
c.

Hàm khởi tạo
public BoxLayout(Container target, int axis)

e.


Một số phương thức thường dùng
public final Container getTarget()
public final int getAxis()
public void invalidateLayout(Container target)


II. CÁC LOẠI LAYOUT MANAGER
BoxLayout

3.
d.

Một số phương thức thường dùng
public void addLayoutComponent(String name, Component comp)
public void removeLayoutComponent(Component comp)
public void addLayoutComponent(Component comp, Object
constraints)
public Demention preferredLayoutSize(Container target)
public Demention minimumLayoutSize(Container target)


II. CÁC LOẠI LAYOUT MANAGER
CardLayout

4.
a.

Đặc điểm: Cho phép bố trí các components thành từng lớp như các
quân bài trong một bộ bài. Để chứa các components ta thường sử

dụng các Jpanel. Tại một thời điểm chỉ có một Jpanel được hiển thị.

b.

Cú pháp khai báo
public class CardLayout
extends Object
implements LayoutManager2, Serializable


II. CÁC LOẠI LAYOUT MANAGER
CardLayout

4.
c.

Hàm khởi tạo
public CardLayout()
public CardLayout(int hgap, int vgap)

e.

Các phương thức thường dùng
public void addLayoutComponent(Component comp, Object
constraints)
public void addLayoutComponent(String name, Component comp) 


II. CÁC LOẠI LAYOUT MANAGER
4.

d.

CardLayout
Các phương thức thường dùng
public void setHgap(int hgap)
public void setVgap(int vgap)
public void first(Component parent)
public float getLayoutAlignmentX(Container parent)
public float getLayoutAlignmentY(Container parent)
public void layoutContainer(Container parent)
Tham khảo thêm tại nguồn: http://
docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html 


II. CÁC LOẠI LAYOUT MANAGER
GridLayout

5.
a.

Đặc điểm: Bố trí các components dưới dạng lưới, với các dòng và
các cột đều nhau, mỗi một khung lưới chỉ chứa một components
duy nhất.

b.

Cú pháp khai báo
public class GridLayout
extends Object
implements LayoutManager, Serializable



II. CÁC LOẠI LAYOUT MANAGER
GridLayout

5.
c.

Hàm khởi tạo
public GridLayout()
public GridLayout(int rows, int cols)
public GridLayout(int rows, int cols, int hgap, int vgap)

e.

Các phương thức thường dùng
public void addLayoutComponent(String name, Component comp) 


II. CÁC LOẠI LAYOUT MANAGER
5.
d.

GridLayout
Các phương thức thường dùng
public void setHgap(int hgap)
public void setVgap(int vgap)
public int getColumns ()
public int getRows()
public void setColumns(int cols)

Public void setRows(int rows)
public void layoutContainer(Container parent)
Tham khảo thêm tại: http://
docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html 


II. CÁC LOẠI LAYOUT MANAGER
6.
a.

GridBagLayout
Đặc điểm:
Cho phép đặt một component trên một số ô kề nhau theo cả hay chiều. Nó hiệu
quả nhưng phức tạp hơn các layout khác. Các component trong GridBagLayout
không cần có cùng kích thước.

.

Tương tự như GridLayout nhưng thứ tự đặt các component không theo nguyên
tắc sắp xếp thì trái qua phải và từ trên xuống dưới, bạn cần cung cấp thông tin
về kích thước và vị trí của mỗi thành phần.

.

a.

Cú pháp khai báo
public class GridBagLayout
extends Object
implements LayoutManager2, Serializable



II. CÁC LOẠI LAYOUT MANAGER
GridBagLayout

6.
c.

Hàm khởi tạo
public GridBagLayout()

e.

Các phương thức thường dùng
public void addLayoutComponent(Component comp, Object
constraints)
public void addLayoutComponent(String name, Component comp) 


II. CÁC LOẠI LAYOUT MANAGER
GridBagLayout

6.
d.

Các phương thức thường dùng
protected void adjustForGravity(GridBagConstraints constraints,
Rectangle r)
protected void AdjustForGravity(GridBagConstraints constraints,
Rectangle r)

protected void arrangeGrid(Container parent)
protected void ArrangeGrid(Container parent)


×