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);
}
}

解决方案 »

  1.   

    PoliceMan police;          //什么东西
    ---
    这个明显是自己定义的类么,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找到的
      

  2.   

    谢谢楼上回复
    API呀都是英文,很难看呀
      

  3.   

    api都不肯看,基本学java很困难
    你的教材是不是有问题,怎么老是这些gui问题,书上都没讲的吗
      

  4.   

    String number=e.getActionCommand();//还是不太明白text1.addActionListener(this);//this是浏览器吗?什么时候调用下面那个actionPerformed方法Example9_3 a//定义的是Example9_3.JAVA,可以这样用使用中的类来定义吗?a也是对象吧
    this.a算是怎么回事呢
      

  5.   

    我建议你看一下<java how to programme>这本书吧,是分上下册的那种,上面讲的还可以,至少对于初学者是这样子。如果你总是GUI上面有问题的话就看下O`Reilly的<java swing>第二版,网上有电子版的可以去下载,这个讲的还不错,不过是英文版的。
      

  6.   

    建议看一下java的GUI基础 
    e.getActionCommand()中的e应该是事件处理默认的对象把,
    text1.addActionListener(this);this应该是值的这个类里的对象把
    我现在正看这些呢,很有意思的。
      

  7.   

    String number=e.getActionCommand();//还是不太明白
    返回与这个动作相关联的command string.text1.addActionListener(this);//this是浏览器吗?
    this是指ActionListener.当ActionEvent被触发的时候,就调用了actionPerformed方法将Example9_3类 中定义的a初始化为参数的a,这个是构造函数啊?
    this.a就表示Example9_3中定义的a,建议楼主看看this关键字的用法.