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);
add(text1);add(text2);add(text3);
text1.addActionListener(this);
text1.addActionListener(police);
}
public void actionPerformed(ActionEvent e)
{
String number=e.getActionCommand();//getActionCommand()什么用法,起的什么作用
int n=Integer.parseInt(number);//不懂
int m=n*n;
text2.setText(n+"的平方是:"+m);
}
}
class PoliceMan implements ActionListener
{
Example9_3 a=null;//Example9_3??不是我定义 的类吧?这样就可以用了吗?
PoliceMan(Example9_3 a)//啥意思
{
this.a=a;
}
public void actionPerformed(ActionEvent e)
{
String number=e.getActionCommand();
int n=Integer.parseInt(number);
int m=n*n*n;
a.text3.setText(n+"的立方是:"+m);
}
}
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);
add(text1);add(text2);add(text3);
text1.addActionListener(this);
text1.addActionListener(police);
}
public void actionPerformed(ActionEvent e)
{
String number=e.getActionCommand();//getActionCommand()什么用法,起的什么作用
int n=Integer.parseInt(number);//不懂
int m=n*n;
text2.setText(n+"的平方是:"+m);
}
}
class PoliceMan implements ActionListener
{
Example9_3 a=null;//Example9_3??不是我定义 的类吧?这样就可以用了吗?
PoliceMan(Example9_3 a)//啥意思
{
this.a=a;
}
public void actionPerformed(ActionEvent e)
{
String number=e.getActionCommand();
int n=Integer.parseInt(number);
int m=n*n*n;
a.text3.setText(n+"的立方是:"+m);
}
}
---
这个明显是自己定义的类么,api中怎么可能有PoliceMan这样的类
忽然发现你后面自己定义了这个类,怎么还问?String number=e.getActionCommand();//getActionCommand()什么用法,起的什么作用
----------
比如有个Button b,b.setActionCommand("Buttonb"),此时如果e.getActionCommand().equals("Buttonb"),则表示b触发了
Example9_3 a=null;//Example9_3??不是我定义 的类吧?这样就可以用了吗?
----------------
这个明显是你自己定义的类么,或者在其他java文件中定义了,自己找一下,不然肯定编译通不过
int n=Integer.parseInt(number);//不懂
-------------
把字符串数字转换为int,比如"123",parse之后就是int型的123
PoliceMan(Example9_3 a)//啥意思
----------
明显是以Example9_3为参数的构造函数
ps:很多问题楼主怎么不看api文档呢,我看你老是一大堆问题,很多都可以查api找到的
API呀都是英文,很难看呀
你的教材是不是有问题,怎么老是这些gui问题,书上都没讲的吗
this.a算是怎么回事呢
e.getActionCommand()中的e应该是事件处理默认的对象把,
text1.addActionListener(this);this应该是值的这个类里的对象把
我现在正看这些呢,很有意思的。
返回与这个动作相关联的command string.text1.addActionListener(this);//this是浏览器吗?
this是指ActionListener.当ActionEvent被触发的时候,就调用了actionPerformed方法将Example9_3类 中定义的a初始化为参数的a,这个是构造函数啊?
this.a就表示Example9_3中定义的a,建议楼主看看this关键字的用法.