Lesson 05 (Layout Manager)
Transcript of Lesson 05 (Layout Manager)
-
7/27/2019 Lesson 05 (Layout Manager)
1/25
Layout ManagersBi 5
-
7/27/2019 Lesson 05 (Layout Manager)
2/25
Bi 05 2/ 24
Ni dung chnh nh ngha v chc nng ca layout managers Cc kiu Layouts ng dng ca layout managers Cc loi layouts:
FlowLayout
BorderLayout
GridLayout
CardLayout
GridBagLayout
-
7/27/2019 Lesson 05 (Layout Manager)
3/25
Bi 05 3/ 24
Layout Manager Cc component trn giao din ngi dng nn
c sp xp theo mt trnh t hp l. Mi nhm component nn sp theo mt b cc
ring cho ph hp nht. qun l b cc ta s dng layout managers.
-
7/27/2019 Lesson 05 (Layout Manager)
4/25
Bi 05 4/ 24
Cc kiu Layouts AWT cung cp mt s lp qun l layoutc gi l layout managers.
Cc kiu layout : FlowLayout BoxLayout
BorderLayout
CardLayout
GridLayout
GridBagLayout
-
7/27/2019 Lesson 05 (Layout Manager)
5/25
Bi 05 5/ 24
Thit lp layouts nh th no? Component u tin c thit lp, n s s
dng layout mc nh. Layout mc nh ca applet l FlowLayout Layout mc nh ca Frame l BorderLayout
Tt c components c t trong mtcontainer v c sp xp theo layout thitlp cho n.
Mt layout manager c th c thit lp nhmethod setLayout()
-
7/27/2019 Lesson 05 (Layout Manager)
6/25
Bi 05 6/ 24
FlowLayout Manager Layout mc nh ca applet v panel Cc Components c sp xp theo th t gc
tri trn xung gc phi di. Constructors ca FlowLayout l :
FlowLayout mylayout = new FlowLayout();
FlowLayout exLayout = new
FlowLayout(FlowLayout.LEFT);
// alignment specified
-
7/27/2019 Lesson 05 (Layout Manager)
7/25Bi 05 7/ 24
FlowLayout Manager Contd
Flow Layout Left and Right Aligned
-
7/27/2019 Lesson 05 (Layout Manager)
8/25Bi 05 8/ 24
Example
Output
import java.awt.*;import java.awt.event.*;class FlowlayoutDemo extends Frame{
Label lblName;TextField txtName;Button btnOk;
public FlowlayoutDemo(String title){
super(title);
setLayout(new FlowLayout());
lblName = new Label("Name: ");txtName = new TextField(20);btnOk = new Button("Ok");
add(lblName);add(txtName);add(btnOk);
}public static void main(String[]arg){
FlowlayoutDemo frmLayout = newFlowlayoutDemo("Flowlayout Demo...");
frmLayout.setSize(300,200);frmLayout.setVisible(true);
}
}
-
7/27/2019 Lesson 05 (Layout Manager)
9/25Bi 05 9/ 24
BorderLayout Manager L layout mc nh cho Window, Frame vDialog
Cc component c th c t vo cc vngNorth, South, East, West, or Center ca containers dng BorderLayout
-
7/27/2019 Lesson 05 (Layout Manager)
10/25Bi 05 10/ 24
BorderLayout Manager Cc hng s xc nh vng t
component vo container:
PAGE_START: nh trn container theo chiu dc
LINE_END: bn phi container theo chiu ngang
PAGE_END: nh di container theo chiu dc
LINE_START: bn tri container theo chiu ngang
LINE_CENTER: center ca container
-
7/27/2019 Lesson 05 (Layout Manager)
11/25Bi 05 11/ 24
Example
Output
import java.awt.*;
public class BorderLayoutDemo extends Frame{
public BorderLayoutDemo(){
setLayout(new BorderLayout());Button best = new Button("EAST");Button bwest = new Button("WEST");Button bnorth = new Button("NORTH");Button bsouth = new Button("SOUTH");Button bcentre = new Button("CENTER");add(best, BorderLayout.LINE_END);add(bwest, BorderLayout.LINE_START);add(bnorth, BorderLayout.PAGE_START);add(bsouth, BorderLayout.PAGE_END);add(bcentre, BorderLayout.CENTER);
}public static void main(String [] args){
BorderLayoutDemo frmBorder = new BorderLayoutDemo();frmBorder.setSize(300,300);frmBorder.setVisible(true);
}}
-
7/27/2019 Lesson 05 (Layout Manager)
12/25Bi 05 12/ 24
GridLayout Manager GridLayout chia container thnh li hnh ch
nht
Cc component s c sp xp trong cccell Thng dng khi cc component c cng kch
thc Constructor ca GridLayout:
GridLayout g1= newGridLayout(3,3);
(khi to mt grid 3 hng, 3 ct)
-
7/27/2019 Lesson 05 (Layout Manager)
13/25Bi 05 13/ 24
Hnh nh ca GridLayout
-
7/27/2019 Lesson 05 (Layout Manager)
14/25Bi 05 14/ 24
GridBagLayout Manager Cc component thng c kch thc khc
nhau
Components c sp xp theo rows, cols Th t cc comp c th ko theo chiu top-to-
bottom or left-to-right
Thit lp GridBaglayout cho 1 container theoc php nh sau:
GridBagLayoutgb = new GridBagLayout();
ContainerName.setLayout(gb);
-
7/27/2019 Lesson 05 (Layout Manager)
15/25Bi 05 15/ 24
GridBagLayout Manager s dng Gridbaglayout ta phi thit lp
thng tin v size v layout ca micomponent
Lp GridBagConstraintscha ng ttc cc thng tin c GridBagLayout yucu cung cp v tr, kch thc ca
component
-
7/27/2019 Lesson 05 (Layout Manager)
16/25Bi 05 16/ 24
GridBagLayout Manager Cc thuc tnh ca GridBagConstraints :
weightx, weighty: xc nh khong trngtrong GridbagLayout
gridwidth, gridheight: Xc nh s theochiu ngang, dc hin th component
ipadx, ipady: lng lm thay i chiu cao,chiu rng ti thiu, n s thm 2*ipadx vo chiurng ti thiu v 2*ipady vo chiu cao
-
7/27/2019 Lesson 05 (Layout Manager)
17/25Bi 05 17/ 24
GridBagLayout Manager Cc thuc tnh ca GridBagConstraints:
Anchor: v tr ca comp trong cell (NORTH,WEST,EAST, SOUTH)
gridx, gridy: v tr cell s t comp
Fill: cch m mt thnh phn c b tr vo cell
ntn nu cell ln hn comp
Insets: xc nh khong cch top, bottom, left vright gia cc comp
-
7/27/2019 Lesson 05 (Layout Manager)
18/25Bi 05 18/ 24
Exampleimport java.awt.*;
public class GridbagLayoutDemo extends Frame{
TextArea ObjTa;
TextField ObjTf;Button butta, buttf;CheckboxGroup cbg;Checkbox cbbold,cbitalic,cbplain,cbboth;GridBagLayout gb;GridBagConstraints gbc;public GridbagLayoutDemo(){
gb = new GridBagLayout(); //tao mot doi tuong gridbaglayoutsetLayout(gb);gbc = new GridBagConstraints();
//doi tuong gridbagconstraints de quan ly cac rang buoc ->gan vao gb
ObjTa = new TextArea("Textarea ",5,10);ObjTf = new TextField("enter your name");butta = new Button("TextArea");buttf = new Button("TextField");
-
7/27/2019 Lesson 05 (Layout Manager)
19/25
Bi 05 19/ 24
Example public void addComponent(Component comp, int row, int col, int nrow, int ncol){gbc.gridx = col;gbc.gridy = row;
gbc.gridwidth = ncol;gbc.gridheight = nrow;
gb.setConstraints(comp,gbc);add(comp);}public static void main(String []args){
GridbagLayoutDemo frmGrid= new GridbagLayoutDemo();frmGrid.setSize(250,200);frmGrid.setVisible(true);
}}
cbg = new CheckboxGroup();cbbold = new Checkbox("Bold",cbg,false);cbitalic = new Checkbox("Italic",cbg,false);cbplain = new Checkbox("Plain",cbg,false);cbboth = new Checkbox("Bold/Italic",cbg,true);gbc.fill = GridBagConstraints.BOTH;addComponent(ObjTa,0,0,4,1);
gbc.fill = GridBagConstraints.HORIZONTAL;addComponent(butta,0,1,1,1);gbc.fill = GridBagConstraints.HORIZONTAL;addComponent(buttf,0,2,1,1);gbc.fill = GridBagConstraints.HORIZONTAL;addComponent(cbbold,1,1,1,1);gbc.fill = GridBagConstraints.HORIZONTAL;addComponent(cbitalic,1,2,1,1);gbc.fill = GridBagConstraints.HORIZONTAL;addComponent(cbplain,3,1,1,1);gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(cbboth,3,2,1,1);gbc.fill = GridBagConstraints.HORIZONTAL;addComponent(ObjTf,4,0,1,3);}
-
7/27/2019 Lesson 05 (Layout Manager)
20/25
Bi 05 20/ 24
ExampleOutput
-
7/27/2019 Lesson 05 (Layout Manager)
21/25
Bi 05 21/ 24
CardLayout Manager C th lu tr nh mt ngn xp cc layouts Mi layout ging nh mt card trong thn
Card thng l i tng Panel S dng khi chng ta mun dng nhiu panel
m mi panel ch hin th mt thi im Main panel s cha ng cc panel ny
-
7/27/2019 Lesson 05 (Layout Manager)
22/25
Bi 05 22/ 24
CardLayout Manager Thitlp layout:
CardLayout card=new CardLayout();
panelMain.setLayout(card); Thm cc panel vo mainPanel:
panelMain.add(Red Panel, panelOne);
panelMain.add(Blue Panel, panelTwo);
Thitlp layout cho cc panel con: panelTwo.setLayout(new GridLayout(2,1));
-
7/27/2019 Lesson 05 (Layout Manager)
23/25
Bi 05 23/ 24
Example/**/
import java.awt.*;import java.awt.event.*;import java.applet.*;public class MyCardDemo extends Applet implements
ActionListener, MouseListener{
Checkbox nov, fic, autobio, story, swim, runn;Panel hobcards;
CardLayout cardlo;Button reading, playing;CheckboxGroup cbg;public void init(){
reading = new Button("Reading");playing = new Button ("Games");
add(reading);add(playing);cardlo = new CardLayout();hobcards = new Panel(); // main panel
// sets the layout of the main panel to card layouthobcards.setLayout(cardlo);
cbg = new CheckboxGroup();nov = new Checkbox("NOVELS", cbg, true);fic = new Checkbox("FICTIONS", cbg, false);autobio = new Checkbox("AUTOBIOGRAPHY", cbg, false);story = new Checkbox("STORIES", cbg, false);swim = new Checkbox("SWIMMING", false);
runn = new Checkbox("RUNNING", false);
-
7/27/2019 Lesson 05 (Layout Manager)
24/25
Bi 05 24/ 24
Example// adding radio buttons to the reading card panel first deckPanel readpan = new Panel();readpan.setLayout(new GridLayout(2,2));readpan.add(nov);readpan.add(fic);readpan.add(autobio);
readpan.add(story);// adding checkbox to the playing card panel Second deck
Panel playpan = new Panel();playpan.add(swim);playpan.add(runn);
// adding the two panels to the card deck panelhobcards.add(readpan,"READING");hobcards.add(playpan,"PLAYING");
// adding cards to the main applet panneladd(hobcards);
// register to receive action eventsreading.addActionListener(this);playing.addActionListener(this);
// registering mouse movementsaddMouseListener(this);
}
public void mousePressed(MouseEvent m){
cardlo.next(hobcards);}public void mouseClicked(MouseEvent m){}public void mouseEntered(MouseEvent m){}public void mouseExited(MouseEvent m){}public void mouseReleased(MouseEvent m){}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == reading){
cardlo.show(hobcards,"READING" );}else{
cardlo.show(hobcards,"PLAYING");}
}}
-
7/27/2019 Lesson 05 (Layout Manager)
25/25
Example (Output)Output