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
35import java.awt.*;import java.applet.*;import java.awt.event.*;
36//写一个按扭类的子类,增加一些新的功能:
37class 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}
69public 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
88import java.awt.*;import java.applet.*;
89import java.awt.event.*;
90public 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}
100class 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}
121class 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
134import java.awt.*;import java.awt.event.*;import java.applet.*;
135class 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}
181public class Example10_4 extends Applet
182{ MyLabel lab;
183 public void init()
184 { lab=new MyLabel("回车求该数的因子,单击按钮求出小于这个数的素数",this);
185 }
186}
187
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
35import java.awt.*;import java.applet.*;import java.awt.event.*;
36//写一个按扭类的子类,增加一些新的功能:
37class 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}
69public 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
88import java.awt.*;import java.applet.*;
89import java.awt.event.*;
90public 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}
100class 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}
121class 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
134import java.awt.*;import java.awt.event.*;import java.applet.*;
135class 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}
181public class Example10_4 extends Applet
182{ MyLabel lab;
183 public void init()
184 { lab=new MyLabel("回车求该数的因子,单击按钮求出小于这个数的素数",this);
185 }
186}
187