1
//例子1
2
import java.applet.*;import java.awt.*;import java.awt.event.*;
3
public class Example10_1 extends Applet implements ActionListener
4

{ TextField text;
5
Button buttonEnter,buttonQuit;
6
public void init()
{
7
{ text=new TextField("0",10); add(text);
8
buttonEnter=new Button("确定"); buttonQuit =new Button("清除");
9
add(buttonEnter); add(buttonQuit);
10
buttonEnter.addActionListener(this);
11
buttonQuit.addActionListener(this);
12
text.addActionListener(this);
13
}
14
public void paint(Graphics g)
15
{ g.drawString("在文本框输入数字字符回车或单击按钮",10,100);
16
g.drawString("第文本框显示该数的平方根",10,120);
17
}
18
public void actionPerformed(ActionEvent e)
19
{ if(e.getSource()==buttonEnter||e.getSource()==text)
20
{ double number=0;
21
try
{ number=Double.valueOf(text.getText()).doubleValue();
22
text.setText(""+Math.sqrt(number));
23
}
24
catch(NumberFormatException event)
25
{ text.setText("请输入数字字符");
26
}
27
}
28
else if(e.getSource()==buttonQuit)
29
{ text.setText("0");
30
}
31
}
32
}
33
34
//例子2
35
import java.awt.*;import java.applet.*;import java.awt.event.*;
36
//写一个按扭类的子类,增加一些新的功能:
37
class MyButton extends Button implements ActionListener,TextListener
38

{ TextArea text1,text2; //类的成员变量。
39
char save[];
40
MyButton(String s,Container con)
41
{ super(s);
42
text1=new TextArea(6,12); text2=new TextArea(6,12);
43
text2.setEditable(false);
44
text1.addTextListener(this); //创建的按扭监视其中一个文本区。
45
this.addActionListener(this); //创建的按扭自己监视自己。
46
con.add(text1);con.add(text2);con.add(this);
47
}
48
public void textValueChanged(TextEvent e) //实现接口。
49
{
50
String s=text1.getText();
51
StringBuffer strbuffer=new StringBuffer(s);
52
String temp=new String(strbuffer.reverse());
53
text2.setText(temp);
54
}
55
public void actionPerformed(ActionEvent e) //实现接口。
56
{ text2.setText(null);
57
String s=text1.getText();
58
int length=s.length();
59
save=new char[length];
60
//将字符串拷贝到数组save:
61
s.getChars(0,length,save,0);
62
for(int i=0;i<save.length;i++)
63
{ save[i]=(char)(save[i]^'你');
64
}
65
String temp=new String(save);
66
text2.setText(temp);
67
}
68
}
69
public class Example10_2 extends Applet implements ActionListener
70

{ MyButton mybutton;
71
Button button;
72
public void init()
73
{ mybutton=new MyButton("加密",this);
74
button=new Button("解密");
75
button.addActionListener(this);
76
add(button);
77
}
78
public void actionPerformed(ActionEvent e) //实现接口。
79
{ for(int i=0;i<mybutton.save.length;i++)
80
{ mybutton.save[i]=(char)(mybutton.save[i]^'你');
81
}
82
String temp=new String(mybutton.save);
83
mybutton.text2.setText(temp);
84
}
85
}
86
87
//例子3
88
import java.awt.*;import java.applet.*;
89
import java.awt.event.*;
90
public class Example10_3 extends Applet
91

{ public void init()
92
{ MyButton1 button1=new MyButton1();
93
MyButton2 button2=new MyButton2();
94
setLayout(null);
95
add(button1);add(button2);
96
button1.setLocation(12,12);
97
button2.setLocation(60,12);
98
}
99
}
100
class MyButton1 extends Button implements ActionListener
101

{ int n=-1;
102
MyButton1()
103
{ setSize(25,160); addActionListener(this);
104
}
105
public void paint(Graphics g)
106
{ g.drawString("我",6,14); g.drawString("是",6,34);
107
g.drawString("一",6,54); g.drawString("个",6,74);
108
g.drawString("竖",6,94); g.drawString("按",6,114);
109
g.drawString("钮",6,134); g.drawString("!",8,154);
110
}
111
public void actionPerformed(ActionEvent e)
112
{ n=(n+1)%3;
113
if(n==0)
114
this.setBackground(Color.cyan);
115
else if(n==1)
116
this.setBackground(Color.orange);
117
else if(n==2)
118
this.setBackground(Color.pink);
119
}
120
}
121
class MyButton2 extends Button
122

{ MyButton2()
123
{ setSize(38,80); setBackground(Color.cyan);
124
}
125
public void paint(Graphics g)
126
{ g.setColor(Color.red);
127
g.fillOval(10,3,20,20); //在按扭上画圆,见17章。
128
g.setColor(Color.yellow); g.fillOval(10,28,20,20);
129
g.setColor(Color.green); g.fillOval(10,53,20,20);
130
}
131
}
132
133
//例子4
134
import java.awt.*;import java.awt.event.*;import java.applet.*;
135
class MyLabel extends Label implements ActionListener
136

{ String 标签上的初始名称;
137
TextField inputNumber;TextArea showResult;Button button;
138
MyLabel(String s,Container con)
139
{ super(s);
140
标签上的初始名称=s;
141
inputNumber=new TextField(10); showResult =new TextArea(10,10);
142
button=new Button("Enter");
143
button.addActionListener(this);inputNumber.addActionListener(this);
144
con.add(this);con.add(inputNumber);con.add(showResult);con.add(button);
145
}
146
public void actionPerformed(ActionEvent e)
147
{ long n=0;
148
showResult.setText(null);
149
try
{ n=Long.valueOf(inputNumber.getText()).longValue();
150
this.setText(标签上的初始名称);
151
}
152
catch(NumberFormatException e1)
153
{ this.setText("请输入数字字符");
154
}
155
if(e.getSource()==inputNumber)
156
{ 求因子(n);
157
}
158
if(e.getSource()==button)
159
{ 求素数(n);
160
}
161
}
162
public void 求因子(long n)
163
{ for(int i=1;i<=n;i++)
164
{ if(n%i==0)
165
showResult.append("\n"+i);
166
}
167
}
168
public void 求素数(long n)
169
{ showResult.append("小于"+n+"的素数有:");
170
for(int i=1;i<=n;i++)
171
{ int j=0;
172
for(j=2;j<i;j++)
173
{ if(i%j==0) break;
174
}
175
if(j>=i)
176
{ showResult.append("\n"+i);
177
}
178
}
179
}
180
}
181
public class Example10_4 extends Applet
182

{ MyLabel lab;
183
public void init()
184
{ lab=new MyLabel("回车求该数的因子,单击按钮求出小于这个数的素数",this);
185
}
186
}
187

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

103



104

105

106



107

108

109

110

111

112



113

114

115

116

117

118

119

120

121

122



123



124

125

126



127

128

129

130

131

132

133

134

135

136



137

138

139



140

141

142

143

144

145

146

147



148

149



150

151

152

153



154

155

156



157

158

159



160

161

162

163



164



165

166

167

168

169



170

171



172

173



174

175

176



177

178

179

180

181

182



183

184



185

186

187
