📄 example9_3.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example9_3 extends Applet implements ActionListener
{
TextField text1,text2,text3;
Policeman police;
public void init()
{
text1=new TextField(10);
text2=new TextField(10);
text3=new TextField(10);
police=new Policeman(this);//主类的对象作为参数传递给police的构造方法,
add(text1);add(text2);add(text3);
text1.addActionListener(this);
text1.addActionListener(police);
}
public void actionPerformed(ActionEvent e)
{
String number=e.getActionCommand();
int n=Integer.parseInt(number);
int m=n*n;
text2.setText(n+"的平方是:"+m);
}
}
class Policeman implements ActionListener
{
Example9_3 a=null;//定义Pliceman类的成员变量
Policeman(Example9_3 a)
{this.a=a;}//把参数赋给Policeman类的成员变量
//注意,如此一来,Policeman类的成员变量就与参数的对象变成同一的了(一个对象的两个别名)
//因此,在actionPerformed()方法中,对Policemanman的成员变量的操作就等价于对主类的对象的操作
public void actionPerformed(ActionEvent e)
{
String number=e.getActionCommand();
int n=Integer.parseInt(number);
int m=n*n*n;
a.text3.setText(n+"的立方是:"+m);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -