1
//
例子1
2 import java.applet. * ;import java.awt. * ;
3 public class Boy extends Applet
4 { TextField text1,text2,text3;
5 public void init()
6 { text1=new TextField("输入密码:",10);
7 text1.setEditable(false);
8 text2=new TextField(10);
9 text2.setEchoChar('*');
10 text3=new TextField("我是一个文本框",20);
11 add(text1);add(text2);add(text3);
12 text3.setText("重新设置了文本");
13 }
14}
15
16 // 例子2
17 import java.applet. * ;import java.awt. * ;import java.awt. event . * ;
18 public class Example9_2 extends Applet implements ActionListener
19 { TextField text1,text2,text3;
20 public void init()
21 { text1=new TextField(10);
22 text2=new TextField(10);
23 text3=new TextField(20);
24 add(text1);add(text2);add(text3);
25 text1.addActionListener(this); //将主类的实例作为text1的监视器,
26 //因此主类必须实现接口ActionListener 。
27 text2.addActionListener(this);
28 }
29 public void actionPerformed(ActionEvent e)
30 { if(e.getSource()==text1)
31 { String word=text1.getText();
32 if(word.equals("boy"))
33 { text3.setText("男孩");
34 }
35 else if (word.equals("girl"))
36 { text3.setText("女孩");
37 }
38 else if (word.equals("sun"))
39 { text3.setText("太阳");
40 }
41 else
42 {text3.setText("没有该单词");
43 }
44 }
45 else if(e.getSource()==text2)
46 { String word=text2.getText();
47 if(word.equals("男孩"))
48 { text3.setText("boy");
49 }
50 else if (word.equals("女孩"))
51 { text3.setText("girl");
52 }
53 else if (word.equals("太阳"))
54 { text3.setText("sun");
55 }
56 else
57 { text3.setText("没有该单词");
58 }
59 }
60 }
61}
62
63 // 例子3
64 import java.applet. * ;import java.awt. * ;import java.awt. event . * ;
65 public class Example9_3 extends Applet implements ActionListener
66 { TextField text1,text2,text3;
67 PoliceMan police;
68 public void init()
69 { text1=new TextField(10);
70 text2=new TextField(10);
71 text3=new TextField(10);
72 police=new PoliceMan(this);
73 add(text1);add(text2);add(text3);
74 text1.addActionListener(this);
75 text1.addActionListener(police);
76 }
77 public void actionPerformed(ActionEvent e)
78 { String number=e.getActionCommand();
79 int n=Integer.parseInt(number);
80 int m=n*n;text2.setText(n+"的平方是:"+m);
81 }
82}
83 class PoliceMan implements ActionListener
84 { Example9_3 a=null;
85 PoliceMan(Example9_3 a)
86 { this.a=a;
87 }
88 public void actionPerformed(ActionEvent e)
89 { String number=e.getActionCommand();
90 int n=Integer.parseInt(number);
91 int m=n*n*n;a.text3.setText(n+"的立方是:"+m);
92 }
93}
94
95 // 例子4
96 import java.applet. * ;import java.awt. * ;
97 public class Example9_4 extends Applet
98 { TextArea text1,text2;
99 public void init()
100 { text1=new TextArea("我是学生",6,16);
101 text2=new TextArea(6,16);
102 add(text1);add(text2);
103 text2.append("我在学习java语言");
104 text1.insert("们",1);
105 text1.selectAll();
106 int length=text2.getText().length();
107 text2.setSelectionStart(2);
108 text2.setSelectionEnd(length);
109 }
110}
111
112 // 例子5
113 import java.util. * ;import java.applet. * ;
114 import java.awt. * ;import java.awt. event . * ;
115 public class Example9_5 extends Applet implements TextListener
116 { TextArea text1,text2;
117 public void init()
118 { text1=new TextArea(6,15);
119 text2=new TextArea(6,15);
120 add(text1);add(text2);
121 text2.setEditable(false);
122 text1.addTextListener(this) ;
123 }
124 public void textValueChanged(TextEvent e)
125 { if(e.getSource()==text1)
126 { String s=text1.getText();
127 StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");
128 int n=fenxi.countTokens();
129 String a[]=new String[n];
130 for(int i=0;i<=n-1;i++)
131 { String temp=fenxi.nextToken();
132 a[i]=temp;
133 }
134 for(int i=0;i<=n-1;i++) //按字典序从小到大排序。
135 { for(int j=i+1;j<=n-1;j++)
136 { if(a[j].compareTo(a[i])<0)
137 { String t=a[j]; a[j]=a[i]; a[i]=t;
138 }
139 }
140 }
141 text2.setText(null); //刷新显示。
142 for(int i=0;i<n;i++)
143 { text2.append(a[i]+"\n");
144 }
145 }
146 }
147}
148
2 import java.applet. * ;import java.awt. * ;
3 public class Boy extends Applet
4 { TextField text1,text2,text3;
5 public void init()
6 { text1=new TextField("输入密码:",10);
7 text1.setEditable(false);
8 text2=new TextField(10);
9 text2.setEchoChar('*');
10 text3=new TextField("我是一个文本框",20);
11 add(text1);add(text2);add(text3);
12 text3.setText("重新设置了文本");
13 }
14}
15
16 // 例子2
17 import java.applet. * ;import java.awt. * ;import java.awt. event . * ;
18 public class Example9_2 extends Applet implements ActionListener
19 { TextField text1,text2,text3;
20 public void init()
21 { text1=new TextField(10);
22 text2=new TextField(10);
23 text3=new TextField(20);
24 add(text1);add(text2);add(text3);
25 text1.addActionListener(this); //将主类的实例作为text1的监视器,
26 //因此主类必须实现接口ActionListener 。
27 text2.addActionListener(this);
28 }
29 public void actionPerformed(ActionEvent e)
30 { if(e.getSource()==text1)
31 { String word=text1.getText();
32 if(word.equals("boy"))
33 { text3.setText("男孩");
34 }
35 else if (word.equals("girl"))
36 { text3.setText("女孩");
37 }
38 else if (word.equals("sun"))
39 { text3.setText("太阳");
40 }
41 else
42 {text3.setText("没有该单词");
43 }
44 }
45 else if(e.getSource()==text2)
46 { String word=text2.getText();
47 if(word.equals("男孩"))
48 { text3.setText("boy");
49 }
50 else if (word.equals("女孩"))
51 { text3.setText("girl");
52 }
53 else if (word.equals("太阳"))
54 { text3.setText("sun");
55 }
56 else
57 { text3.setText("没有该单词");
58 }
59 }
60 }
61}
62
63 // 例子3
64 import java.applet. * ;import java.awt. * ;import java.awt. event . * ;
65 public class Example9_3 extends Applet implements ActionListener
66 { TextField text1,text2,text3;
67 PoliceMan police;
68 public void init()
69 { text1=new TextField(10);
70 text2=new TextField(10);
71 text3=new TextField(10);
72 police=new PoliceMan(this);
73 add(text1);add(text2);add(text3);
74 text1.addActionListener(this);
75 text1.addActionListener(police);
76 }
77 public void actionPerformed(ActionEvent e)
78 { String number=e.getActionCommand();
79 int n=Integer.parseInt(number);
80 int m=n*n;text2.setText(n+"的平方是:"+m);
81 }
82}
83 class PoliceMan implements ActionListener
84 { Example9_3 a=null;
85 PoliceMan(Example9_3 a)
86 { this.a=a;
87 }
88 public void actionPerformed(ActionEvent e)
89 { String number=e.getActionCommand();
90 int n=Integer.parseInt(number);
91 int m=n*n*n;a.text3.setText(n+"的立方是:"+m);
92 }
93}
94
95 // 例子4
96 import java.applet. * ;import java.awt. * ;
97 public class Example9_4 extends Applet
98 { TextArea text1,text2;
99 public void init()
100 { text1=new TextArea("我是学生",6,16);
101 text2=new TextArea(6,16);
102 add(text1);add(text2);
103 text2.append("我在学习java语言");
104 text1.insert("们",1);
105 text1.selectAll();
106 int length=text2.getText().length();
107 text2.setSelectionStart(2);
108 text2.setSelectionEnd(length);
109 }
110}
111
112 // 例子5
113 import java.util. * ;import java.applet. * ;
114 import java.awt. * ;import java.awt. event . * ;
115 public class Example9_5 extends Applet implements TextListener
116 { TextArea text1,text2;
117 public void init()
118 { text1=new TextArea(6,15);
119 text2=new TextArea(6,15);
120 add(text1);add(text2);
121 text2.setEditable(false);
122 text1.addTextListener(this) ;
123 }
124 public void textValueChanged(TextEvent e)
125 { if(e.getSource()==text1)
126 { String s=text1.getText();
127 StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");
128 int n=fenxi.countTokens();
129 String a[]=new String[n];
130 for(int i=0;i<=n-1;i++)
131 { String temp=fenxi.nextToken();
132 a[i]=temp;
133 }
134 for(int i=0;i<=n-1;i++) //按字典序从小到大排序。
135 { for(int j=i+1;j<=n-1;j++)
136 { if(a[j].compareTo(a[i])<0)
137 { String t=a[j]; a[j]=a[i]; a[i]=t;
138 }
139 }
140 }
141 text2.setText(null); //刷新显示。
142 for(int i=0;i<n;i++)
143 { text2.append(a[i]+"\n");
144 }
145 }
146 }
147}
148