Bai 04 (AWT in java)

Embed Size (px)

Citation preview

Bi 4 Abstract Window Toolkit

N i dung chnh

Gi i thch v AWT Gii thch v cc Containers v Components

Frame Panel Label TextFields and TextAreas Checkboxes and RadioButtons Choice

Xc nh cc s ki n c a cc component trn T o m t ng d ng AWT c l pBi 05 / 2 of 36

Abstract Window Toolkit

Graphical User Interface (GUI) cung c p cc giao di n h a thn thi n v i ng i dng. Abstract Window Toolkit (AWT) l m t t p cc l p trong java cho php ta t o GUI v cho php ng i dng nh p li u qua bn phm, chu t. AWT cung c p cc i t ng cho php t o ng d ng GUI thn thi n, p m t v hi u qu .

Bi 05 / 3 of 36

Containers

L m t vng c th ch a cc thnh ph n khc l l p Container trong gi java.awt v i 2 Class ph bi n nh t l Frame v Panel Frame l m t c a s tch bi t v c border. Panel l m t vng ch a khng c border v n c ch a trong m t c a s .

Bi 05 / 4 of 36

Containers - Frame

N l l p con c a l p Window v n c title bar, menu bar, borders v resizing corners. N th c s l m t component hay m t container. C th s d ng m t trong cc constructor sau t o Frame Frame()

T o m t Frame ko c title, Frame ny l invisible Frame(String Title)

T o m t Frame invisible v i title bar. visible m t Frame ta g i method sau: setVisible()

Bi 05 / 5 of 36

ExampleOutput import java.awt.*; class FrameDemo extends Frame { public FrameDemo(String title) { super(title); } public static void main (String args[]) { FrameDemo ObjFr = new FrameDemo("I have been Framed!!!"); ObjFr.setSize(500,500); ObjFr.setVisible(true); } }Bi 05 / 6 of 36

Containers - Panel

c s d ng nhm m t s cc component v i nhau T o m t Panel b ng cch g i constructor sau: Panel(); V Panel khng nhn th y nn n ph i c add vo m t Frame. Frame s c visible khi 2 method sau c thi t l p setSize() v setVisible()

Bi 05 / 7 of 36

Exampleimport java.awt.*; class PanelTest extends Panel { public static void main(String args[]) { PanelTest ObjPanel = new PanelTest(); Frame ObjFr = new Frame("Testing a Panel!"); Output ObjFr.add(ObjPanel); ObjFr.setSize(400,400); ObjFr.setVisible(true); } public PanelTest() { // write code to override default behavior } }Bi 05 / 8 of 36

Component

L t t c nh ng g c nh v trn UI v c th visible v resize. Textfields, Labels, Checkboxes, Textareas l cc v d c a component M t vi cc component nng cao nh : scrollbars, scrollpanes v dialogs.

Bi 05 / 9 of 36

S k th a c a cc l p trong javaComponent

Button

Checkbox

Container

Choice

Canvas

Label

TextComponent

Panel

Window

TextArea

TextField

Applet

Frame

DialogBi 05 / 10 of 36

Cc componentsLabel Text field Checkbox

Radio button Text Area Button

Bi 05 / 11 of 36

Label

Th ng c s d ng ch ra m c ch c a m t item Ng i dng khng th edit C th t o b ng cch s d ng cc constructor sau:

Label( ) t o m t empty Label Label(String labeltext) t o m t label v i nhn Label(String labeltext, int alignment) t o label v i vi c canh l nh : Label.LEFT, Label.RIGHT or Label.CENTERBi 05 / 12 of 36

TextField

M t thnh ph n GUI cho php ng i dng input Ch cho php m t dng input. T o n b ng vi c s d ng cc constructor sau: TextField() : t o TextField m i TextField(int columns): t o TextField v is c t TextField(String s): t o TextField v i text c s n TextField(String s, int columns) t o textField v i text v s c t.Bi 05 / 13 of 36

Exampleimport java.awt.*; class AcceptName extends Frame { TextField txtName = new TextField(20); Label lblName = new Label("Name :"); public AcceptName (String title) { super(title); Output setLayout(new FlowLayout()); add(lblName); add(txtName); } public static void main(String args[]) { AcceptName ObjAccName = new AcceptName ("Testing components!"); ObjAccName.setSize(300,200); ObjAccName.show(); } }Bi 05 / 14 of 36

TextArea

S d ng khi input nhi u h n m t dng Bao g m m t ScrollBar. Cc constructor: TextArea() TextArea(int rows, int cols) TextArea(String text) TextArea(String text, int rows, int cols) TextArea (String text, int rows, int cols, int scrollbars) t o TextArea v i scrollbars

Bi 05 / 15 of 36

ExampleaddWindowListener(new WindowAdapter() import java.awt.*; { Output import java.awt.event.*; public void windowClosing(WindowEvent we) class TextComments extends Frame { { setVisible(false); TextArea txtComment = new TextArea(5,25); System.exit(0); Label}lblCom = new Label("Comments :"); public TextComments(String title) }); { } super(title); setLayout(new FlowLayout()); public static void main(String args[]) { add(lblCom); add(txtComment); TextComments ObjComment = new TextComments("Testing components!"); ObjComment.setSize(200,200); ObjComment.show(); } }Bi 05 / 16 of 36

Buttons

L m t ph n t c a GUI L cch d nh t ng i dng c th t v i ng d ng Cc constructor: Button() T o button m i Button(String text) T o button v i text

ng tc

Bi 05 / 17 of 36

ExampleaddWindowListener(new WindowAdapter() { import public void windowClosing(WindowEvent we) java.awt.*; import { java.awt.event.*; Output class ButtonTest extends Frame setVisible(false); { System.exit(0); Button btnBread = new Button("Bread!"); } Button btnButter = new Button("Butter!"); }); Button btnJam = new Button("Jam!"); } public ButtonTest(String title) { public static void main(String args[]) { super(title); setLayout(new FlowLayout()); ButtonTest ObjTest = new ButtonTest("The three little buttons!"); add(btnBread); ObjTest.setSize(500,500); add(btnButter); ObjTest.show(); } add(btnJam); }

Bi 05 / 18 of 36

Checkbox

a ra nhi u l a ch n cho ng i dng, ng i dng c th ch n hay b ch n b ng cch tch ln Cc constructor: Checkbox() t o m t empty checkbox

Checkbox(String text) T o m t checkbox v i nhn Checkbox(String text, boolean on) t o checkbox v i nhn v l a ch nBi 05 / 19 of 36

ExampleaddWindowListener(new WindowAdapter() import java.awt.*; { import java.awt.event.*; class Hobbies extends Frame public void windowClosing(WindowEvent we) { { Checkbox cboxRead = new Checkbox("Reading",false); setVisible(false); Checkbox cboxMus = new Checkbox("Music",false); System.exit(0); Checkbox cboxPaint = new Checkbox("Painting",false); } Checkbox cboxMovie = new Checkbox("Movies",false); }); Checkbox cboxDance = new Checkbox("Dancing",false); } Label lblQts = new Label("What's your hobby?" ); public Hobbies(String str ) { public static void main(String args[]) { super(str); setLayout(new GridLayout(6,1)); Hobbies ObjHobby = new Hobbies ("A basket full of add(lblQts); checkboxes!"); add(cboxRead); ObjHobby.setSize(300,300); add(cboxMus); // Objhobby.pack(); add(cboxPaint); add(cboxMovie); ObjHobby.show(); } add(cboxDance); }Bi 05 / 20 of 36

Output

Radiobuttons

Gi ng nh checkbox tuy nhin ch m t option c l a ch n t i m t th i i m. Cc radiobutton ph i c nhm vo trong m t group Tr c tin ta ph i t o CheckboxGroup: CheckboxGroup cg=new CheckboxGroup(); Thi t l p cc Checkbox: Checkbox male = new Checkbox(male,cg,true); Checkbox female= new Checkbox(female,cg,false);

Bi 05 / 21 of 36

Exampleimport java.awt.*; addWindowListener(new WindowAdapter() import java.awt.event.*; { class Qualification extends Frame { public void windowClosing(WindowEvent we) { CheckboxGroup cg = new CheckboxGroup(); setVisible(false); Checkbox radUnder = new Checkbox("Undergraduate",cg,false); System.exit(0); Checkbox radGra = new Checkbox("Graduate",cg,false); } Checkbox radPost = new Checkbox("Post Graduate",cg,false); Output }); Checkbox radDoc = new Checkbox("Doctorate",cg,false); } Label lblQts = new Label("What's your primary qualification?" ); public Qualification(String str) public static void main(String args[]) { { super(str); Qualification ObjQualification = new Qualification ("Literacy!"); setLayout(new GridLayout(6,1)); ObjQualification.pack(); add(lblQts); ObjQualification.show( ); add(radUnder); } add(radGra); } add(radPost); add(radDoc);Bi 05 / 22 of 36

Lists

Hi n th m t danh sch cc l a ch n c a ng i dng Ng i dng s ch n m t trong cc item trn danh sch. L p Choice cho php chng ta t o ra danh sch cc l a ch n.C php nh sau: Choice moviestars=new Choice(); Cc item s c add vo b ng cch s d ng method addItem() moviestars.addItem(Antonio Banderas); moviestars.addItem(Leonardo Dicaprio);Bi 05 / 23 of 36

Exampleimport java.awt.*; addWindowListener(new WindowAdapter() import java.awt.event.*; Output { class Stars extends Frame public void windowClosing(WindowEvent we) { { Choice moviestars = new Choice(); setVisible(false); Label lblQts = new Label("Who is your favorite movie star?"); System.exit(0); public Stars(String str) { } }); super(str); } setLayout(new FlowLayout()); public static void main(String args[]) moviestars.addItem("Antonio Banderas"); { moviestars.addItem("Leonardo DiCaprio"); Stars ObjStar = new Stars ("A sky full of stars!"); moviestars.addItem("Sandra Bullock"); ObjStar.setSize(400,400); moviestars.addItem("Hugh Grant"); ObjStar.show(); moviestars.addItem("Julia Roberts"); } add(lblQts); } add(moviestars);Bi 05 / 24 of 36

i u khi n s ki n v i component

Cc s ki n s c pht sinh khi ng i dng clicks chu t, presses/releases a key. M hnh h ng s ki n (event-driven) c s d ng qu n l cc s ki n ng k cc ph ng th c (handler), hay g i l listener v i cc i t ng c n b t s ki n. Handlers t ng c g i m i khi s ki n pht sinh M t Event Listener l ng nghe m t s ki n no m m t i t ng thi t l p. M i event listener cung c p cc ph ng th c x l nh ng s ki n nyBi 05 / 25 of 36

i u khi n s ki n v i component

Event listeners c ci t nh m t interfaces trong Java Cc b c x l s ki n: Ci t listener interface thch h p v i l p pht sinh s ki n Xc nh t t c cc component gy ra s ki n Xc nh t t c cc s ki n m ta mu n i u khi n Ci t cc method c a listener interface v vi t code i u khi n s ki n

Bi 05 / 26 of 36

Cc l p s ki n

ActionEvent: Pht sinh khi m t buttonitem trong danh sch ch n l a click) hay m t menu c ch n

c nh n, m t c nh n p (double-

WindowEvent MouseEvent KeyEvent TextEvent: Pht sinh khi gi tr trong thnh ph ntextfield hay textarea b thay i c ch n hay

ItemEvent: Pht sinh khi m t m c menub ch n

FocusEventBi 05 / 27 of 36

Cc interface t

ng ng

ActionListener WindowListener MouseListener MouseMotionListener KeyListener TextListener ItemListener FocusListenerBi 05 / 28 of 36

ActionListener

Cc component pht sinh ActionListener:ActionListener

Button List MenuItem TextFieldBi 05 / 29 of 36

ItemListener

Cc component pht sinh ItemListener:ItemListener

Choice Checkbox List

Bi 05 / 30 of 36

WindowListener

Cc component pht sinh WindowListener: WindowListener

Dialog FrameBi 05 / 31 of 36

Cc listener cho ComponentComponent

ComponentListener FocusListener KeyListener MouseListener MouseMotionLIstenerBi 05 / 32 of 36

MouseEvent v WindowEvent

Frame l m t l p con c a Component, n k th a t t c cc c i m c a Component WindowEvent: pht sinh khi m t c a s c kch ho t, c ng, c m hay thot MouseEvent: Pht sinh khi chu t di chuy n, c click, c ko hay th ra

Bi 05 / 33 of 36

KeyboardFocusManager

APIs cho FocusEvent v WindowEvent l khng b i v ta khng ki m sot c component no ang c focus hay component no ang active Vi c ny c th c hi n v i s h tr c a l p KeyboardFocusManager C m t t p cc API cho php ta xc nh focus ang u, kh i t o s thay i focus v thay i th t focus trn cc component.

Bi 05 / 34 of 36

6 s ki n AWT

c

nh ngh a b i

WindowEvent.WINDOW_ACTIVATED WindowEvent.WINDOW_GAINED_FOCUS FocusEvent.FOCUS_GAINED FocusEvent.FOCUS_LOST WindowEvent.WINDOW_LOST_FOCUS WindowEvent.WINDOW_DEACTIVATED

Bi 05 / 35 of 36

Pht sinh s ki n

N u ng i dng click ln m t child component l a thu c m t Frame b ang inactive th l n l t x y ra cc s ki n sau: Tr c tin b nh n c s ki n WINDOW_ACTIVATED Ti p theo b nh n c s ki n WINDOW_GAINED_FOCUS Cu i cng a nh n c s ki n FOCUS_GAINED

Bi 05 / 36 of 36