LẬP TRÌNH JAVASWING LAYOUT MANAGER potx

46 261 0
LẬP TRÌNH JAVASWING LAYOUT MANAGER potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

LẬP TRÌNH JAVA Nguyễn Hoàng Anh – nhanh@fit.hcmus.edu.vn ĐHKHTN - 2011 SWING LAYOUT MANAGER Nội dung  Flow Layout  Border Layout  Card Layout  Grid Layout  Grid Bag Layout  Box Layout  Group Layout 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 là một đối tượng quyết định cách 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 hoặc LayoutManger2. 3 Layout Manager  Mỗi Container có một đối tượng Layout Manager mặc đị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.  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 nó. 4 FlowLayout  Hình ảnh 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 FlowLayout – 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 – FlowLayout.RIGHT; : Canh phải • hgap: 5px, vgap: 5px 7 FlowLayout – 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 8 FlowLayout – 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 () 9 FlowLayout - Demo 10 [...]... private void jbtCard1ActionPerformed(ActionEvent evt) { CardLayout layout= (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()); } } GridLayout  Grid Layout bố trí các Component của Container vào bên trong một... 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(); } } BorderLayout  Border Layout bố trí các Component bên trong... show(Container parent, String name) 24 CardLayout - Demo 25 CardLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 26 package swinglayoutmanager; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class CardLayoutFrame extends JFrame { CardLayout - Demo 1 2 3 4 5 6 7 8 9... BorderLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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);... 19 CardLayout  Hình ảnh 20 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 21 CardLayout –... mỗi Cell được xác định bởi kích thước của Container 33 GridLayout  GridLayout() – rows: 1 – 1 Component / 1 Column  GridLayout(int rows, int cols)  GridLayout(int rows, int cols, int hgap, int vgap) 34 GridLayout - Demo 35 GridLayout - Demo 1 2 3 4 5 6 7 36 public class GridLayoutFrame extends JFrame { private JButton[] jbts; public GridLayoutFrame() { this.initComponents(); } ...FlowLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 11 package swinglayoutmanager; import java.awt.FlowLayout; import javax.swing.*; public class FlowLayoutFrame extends JFrame { //Các control private JButton[] jbts; //Phương thức khởi tạo public FlowLayoutFrame () { this.initComponents(); } FlowLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16... CardLayoutFrame() { this.initComponents(); } CardLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 28 //Phương thức khởi tạo các thành phần private void initComponents() { //MainFrame this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOS E); this.setTitle("CardLayout"); this.setLayout(new BorderLayout()); //jpnChoice this.jpnchoice = new JPanel(); this.jpnchoice.setLayout(new FlowLayout());... BorderLayout - Demo 16 BorderLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 17 package swinglayoutmanager; import java.awt.BorderLayout; import javax.swing.*; public class BorderLayoutFrame extends JFrame { //Các control private JButton jbtNorth; private JButton jbtSouth; private JButton jbtEast; private JButton jbtWest; private JButton jbtCenter; //Phương thức khởi tạo public BorderLayoutFrame() { this.initComponents();... this.jpnchoice.add(this.jbtCard2); //jpnCards this.jpnCards = new JPanel(); this.jpnCards.setLayout(new CardLayout()); this.getContentPane() add(this.jpnCards, BorderLayout.CENTER); CardLayout - Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 30 //jpnCard1 this.jpnCard1 = new JPanel(); this.jpnCard1.setName("Card1"); this.jpnCard1.setLayout(new FlowLayout()); this.jpnCards.add(this.jpnCard1, this.jpnCard1.getName()); //jbt11 . LẬP TRÌNH JAVA Nguyễn Hoàng Anh – nhanh@fit.hcmus.edu.vn ĐHKHTN - 2011 SWING LAYOUT MANAGER Nội dung  Flow Layout  Border Layout  Card Layout  Grid Layout  Grid Bag Layout. tượng Layout Manager  Layout Manager là một đối tượng quyết định cách 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. WindowConstants.EXIT_ON_CLOSE); this.setTitle("FlowLayout"); FlowLayout layout= new FlowLayout(); layout. setAlignment(FlowLayout.LEFT); this.setLayout (layout ); //jbts this.jbts = new

Ngày đăng: 08/08/2014, 18:22

Tài liệu cùng người dùng

Tài liệu liên quan