Professional Documents
Culture Documents
NullLayout l cch b tr t do. Mt GUI c b cc phc tp phn tch thnh nhiu thnh phn, mi thnh phn l 1 panel c layout ring.
3
Ni dung
Gii thiu lp trnh hng s kin M hnh x l s kin Hnh ng, s kin v lng nghe. Cc lp s kin AWT, interface lng nghe s kin
Generate an event
Notify listener
Trigger an event Source Object Register a listener object Listener Object Event Handler
7
}
8
Cc lp s kin java.awt.event
Cc lp s kin (tt)
Lp s kin ActionEvent M t Pht sinh khi mt button c nhn, mt item trong danh sch chn la c nhp i hay mt menu c chn. Pht sinh khi mt thanh scrollbar c s dng. Pht sinh khi mt thnh phn c thay i kch thc, c di chuyn, b n hay lm cho hot ng c. Pht sinh khi mt thnh phn mt hay nhn focus. Pht sinh khi input c nhn t bn phm.
10
AdjustmentEvent ComponentEvent
FocusEvent KeyEvent
Cc lp s kin (tt)
Lp s kin ItemEvent M t Pht sinh khi mt menu item c chn hay b chn; khi mt checkbox hay mt item trong danh sch c click. Pht sinh khi mt ca s c kch hot, c ng, c m hay thot. Pht sinh khi gi tr trong thnh phn text field hay text area b thay i. Pht sinh khi chut di chuyn, c click, c ko hay b th ra.
11
WindowEvent
TextEvent
MouseEvent
12
Hm x l
actionPerformed(ActionEvent) itemStateChanged(ItemEvent) windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent)
ContainerEvent .
ContainerListener
componentAdded(ContainerEvent) componentRemoved(ContainerEvent)
14
V d x l s kin
V d 1: To b lng nghe bin c cho i tng khung cha Frame, v x l bin c ng ca s. To v ng k mt b lng nghe cho mt component trn GUI (Frame). Ci t cc phng thc qun l v x l s kin tng ng.
16
WindowEventDemo.java
import java.awt.*; import java.awt.event.*; public class WindowEventDemo{ public static void main(String args[ ]){ Frame f = new Frame ("WindowClosing Demo"); WindowCloser closer = new WindowCloser(); f.addWindowListener(closer); f.setBounds(10, 10, 300, 200); f.setVisible(true); } }
17
WindowCloser.java
import java.awt.event.*; class WindowCloser implements WindowListener{ public void windowClosing(WindowEvent e){ System.out.println("windowClosing.."); System.exit(0); } public void windowActivated(WindowEvent e){ System.out.println("windowActivated..."); } public void windowClosed(WindowEvent e){ System.out.println("windowClosed..."); } //
18
WindowCloser.java
public void windowOpened(WindowEvent e){ System.out.println("windowOpened..."); } public void windowDeactivated(WindowEvent e){ System.out.println("windowDeactivated..."); } public void windowDeiconified(WindowEvent e){ System.out.println("windowDeiconified..."); } public void windowIconified(WindowEvent e){ System.out.println("windowIconified..."); } }
19
V d X l s kin (2)
C th dng lp tru tng WindowAdapter to ra b lng nghe.
WindowAdapter hin thc interface WindowListener nn lp o ny cng c 7 phng thc ging nh giao din WindowListener
20
V d X l s kin (3)
V d 2: Trnh by cch tnh gp i ca mt s c nhp vo. Chng trnh c thc hin bng cch kt hp cc phng thc ca lp, ngha l cc phng thc x l s kin v giao din. Vic click trn mt button s lm khi ng ActionEvent v gi phng thc actionPerformed(). N s kim tra button c click vi s tr gip ca hm getSource() v tr v kt qu thch hp.
21
24
Demo
25
26
Demo
28
Mt s v d minh ha (1)
V d 1: Xy dng chng trnh v x l s kin nh sau
Chng trnh
29
Mt s v d minh ha (2)
V d 2: Xy dng chng trnh v x l s kin nh sau
Chng trnh
30
Mt s v d minh ha (3)
V d 3: X l s kin khi ngi dng chn ngy (Monday, Tuesday, ) trong List th chng trnh s hin th ngy tng ng trn nhn pha trn
Chng trnh
31
Mt s v d minh ha (3)
V d 3: Hin th ngy gi ca h thng Dng i tng lp Date (java.util.Date) S dng Timer (javax.swing.Timer) Date now= new Date(); int h=now.getHours(); int m=now.getMinutes(); int s=now.getSeconds();
32
Bi tp (1)
1. Vit chng trnh minh ha vic s dng mt s component c bn trong th vin java.awt v javax.swing 2. Vit chng trnh MyNotePad tng t NotePad ca Windows. 3. Vit chng trnh minh ha hot ng my tnh c nhn n gin.
33
Bi tp (2)
4. Vit chng trnh minh ha x l s kin menu nh sau
Ngun pht sinh s kin (MenuItem) cn phi ng k b lng nghe x l khi ngi dng tc ng
34
Hi & p
35