You are on page 1of 6

ng k i tng nghe

ng k i tng nghe ta s dng tn phng thc c cu trc nh sau:


add + loi s kin + Listener(lp nghe s kin)

V d vi nt Button
addActionListener(ActionListener)

V d vi danh sch List


addActionListener(ActionListener) addItemListener(ItemListener)

19

Mt s thnh phn GUI


Event TextComponent Checkbox Container Object Component Label Button List Choice CheckboxGroup Panel Applet TextField

20

Nhn (Label)
Nhn c dng trnh by mt chui vn bn ra mn hnh Mt s phng thc ca Label:
public Label(); // to nhn public Label(String s); // to nhn vi ni dung s public Label(String s, int align); // to v canh l void setText(String s); // t ni dung nhn void setAlignment(int align); // canh l nhn ...

21

Nhn (Label)
import java.applet.Applet; import java.awt.*; public class DemoLabel extends Applet { private Label label; public void init() { Font font = new Font("Courier", Font.BOLD, 20); label = new Label("Thu nghiem voi Label"); label.setFont(font); add(label); } public void paint(Graphics g) { showStatus("Noi dung cua Label la: + label.getText()); }
22

Nhn (Label)

23

Nt nhn (Button)
Mt s phng thc ca Button
Button(); // to nt nhn Button(String s); // to nt nhn c tn s void setLabel(String s); // i tn nt String getLabel(); // ly tn nt nhn

i tng nghe s kin nhn nt cn ci t giao tip ActionListener

24

You might also like