1
//例子1
2
import java.applet.*;import java.awt.*;
3
import java.awt.event.*;
4
import javax.swing.JTextArea;
5
public class Example14_1 extends Applet implements ItemListener
6

{ List list ;
7
JTextArea text;
8
public void init()
9
{ list=new List(6,false);
10
text=new JTextArea(6,15);text.setForeground(Color.blue);
11
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
12
String fontName[]=ge.getAvailableFontFamilyNames();
13
for(int i=0;i<fontName.length;i++)
14
{ list.add(fontName[i]);
15
}
16
add(list);
17
Panel p=new Panel();
18
p.setBackground(Color.pink);
19
p.add(text);
20
add(p);
21
list.addItemListener(this);
22
}
23
public void itemStateChanged(ItemEvent e)
24
{ String name=list.getSelectedItem();
25
Font f=new Font(name,Font.BOLD,16);
26
text.setFont(f);
27
text.setText("\nWelcome 欢迎您");
28
}
29
}
30
31
//例子2
32
import java.awt.*;import java.awt.event.*;
33
public class Example14_2 extends java.applet.Applet implements ActionListener
34

{ Button button;
35
Label label;
36
public void init()
37
{ button=new Button("横向走动"); button.setBackground(Color.red);
38
button.addActionListener(this);
39
label=new Label("我可以被碰掉",Label.CENTER);
40
label.setBackground(Color.yellow);
41
add(button); add(label);
42
}
43
public void actionPerformed(ActionEvent e)
44
{ Rectangle rect=button.getBounds();
45
if(rect.intersects(label.getBounds()))
46
{ label.setVisible(false);
47
}
48
if(label.isVisible())
49
{ button.setLocation(rect.x+3,rect.y);
50
}
51
else
52
{ button.setLocation(rect.x,rect.y+3);
53
button.setLabel("纵向走动");
54
}
55
}
56
}
57
58
//例子3
59
import java.awt.*;import java.awt.event.*;
60
class MyCanvas extends Canvas
61

{ int n=-1;
62
MyCanvas()
63
{ setSize(150,120);setBackground(Color.pink);
64
}
65
public void paint(Graphics g)
66
{ g.setColor(Color.red);
67
g.drawString("部分清除时我将消失",10,12);
68
g.drawString("我们正在学习repaint方法",10,80);
69
}
70
public void setN(int n)
71
{ this.n=n;
72
}
73
public void update(Graphics g)
74
{ int width=0, height=0;
75
width=getSize().width;height=getSize().height;
76
if(n==0)
77
{ g.clearRect(0,0,width,height);
78
//paint(g); //如果取消该注释,update的功能就与父类相同。
79
}
80
else if(n==1)
81
{ g.clearRect(2,2,width,40);
82
}
83
}
84
}
85
public class Example14_3 extends java.applet.Applet implements ActionListener
86

{ Button b1,b2;MyCanvas canvas;
87
public void init()
88
{ canvas=new MyCanvas();
89
b1=new Button("全部清除"); b1.addActionListener(this);
90
b2=new Button("部分清除"); b2.addActionListener(this);
91
add(b1); add(b2); add(canvas);
92
}
93
public void actionPerformed(ActionEvent e)
94
{ if(e.getSource()==b1)
95
{ canvas.setN(0);canvas.repaint();
96
}
97
if(e.getSource()==b2)
98
{ canvas.setN(1);canvas.repaint();
99
}
100
}
101
}
102

2

3

4

5

6



7

8

9



10

11

12

13

14



15

16

17

18

19

20

21

22

23

24



25

26

27

28

29

30

31

32

33

34



35

36

37



38

39

40

41

42

43

44



45

46



47

48

49



50

51

52



53

54

55

56

57

58

59

60

61



62

63



64

65

66



67

68

69

70

71



72

73

74



75

76

77



78

79

80

81



82

83

84

85

86



87

88



89

90

91

92

93

94



95



96

97

98



99

100

101

102
