import java.awt.*;
import java.awt.event.*;
public class T13 extends Frame{
   Button m[] = new Button[10];
       Button b1 = new Button("+");
       Button b2 = new Button("-");
       Button b3 = new Button("*");
       Button b4 = new Button("/");
       Button b5 = new Button("=");
       TextField tf = new TextField(10);
       A a = new A();
     int op1,op2,result,fuhao;
     
       T13(){
        this.setLayout(new GridLayout(4,3));
         for(int i = 0;i<10;i++){
     m[i]=new Button(""+i);
        this.add(m[i]);
        m[i].addActionListener(a);
      
        }
        this.add(b1);
        this.add(b2);
        this.add(b3);
        this.add(b4);
        this.add(b5);
        b1.addActionListener(a);
        b2.addActionListener(a);
        b3.addActionListener(a);
        b4.addActionListener(a);
        b5.addActionListener(a);
           this.add(tf);
        this.setBackground(Color.BLUE);
       }
       class A implements ActionListener{
        public void actionPerformed(ActionEvent e){
       String command = e.getActionCommand();//为什么我这样就不行啊
        tf.setText(tf.getText()+command);//输入一个数后 在输入加号什么的就不清空啊
          /* for(int i = 0;i<10;i++){
     if(e.getSource()==m[i])//用这个方法就行 
     {
     tf.setText(tf.getText()+m[i].getLabel());
     }
     }    */
           if(e.getSource()==b1)
   {
   op1 = Integer.parseInt(tf.getText());
  
   fuhao=1;
   tf.setText("");
   }
       if(e.getSource()==b2)
   {
   op1=Integer.parseInt(tf.getText());
   tf.setText("-");
   fuhao=2;
   tf.setText("");
   }
  
   if(e.getSource()==b3)
   {
   op1=Integer.parseInt(tf.getText());
   tf.setText("*");
   fuhao=3;
   tf.setText("");
   }
  
   if(e.getSource()==b4)
   {
   op1=Integer.parseInt(tf.getText());
   tf.setText("/");
   fuhao=4;
   tf.setText(""); 
   }
   if(e.getSource()==b5)
{
op2=Integer.parseInt(tf.getText());
if(fuhao==1)
result=op1+op2;
if(fuhao==2)
result=op1-op2;
if(fuhao==3)
result=op1*op2;
if(fuhao==4)
result=op1/op2;
tf.setText(""+result);
}
          
       }
}
    public static void main(String[] args) {

          T13 obj = new T13();
          obj.setSize(300,200);
          obj.setVisible(true);
}}///////////////////////////////////////////////问题在这呢 嘿嘿 那为大哥救救我吧给我解释一下
                String command = e.getActionCommand();//为什么用这两句 就不行啊
        tf.setText(tf.getText()+command);//输入一个数后 在输入"加号"什么的就不清空啊 
          /* for(int i = 0;i<10;i++){
     if(e.getSource()==m[i])     // 但是用这个方法就行 这是为什么? 
     {
     tf.setText(tf.getText()+m[i].getLabel());
     }
     }    */

解决方案 »

  1.   

    public String getActionCommand()
    返回与此动作相关的命令字符串。这使得 "modal" 组件可以由其当前的状态,通过此字符串来得到它能够产生的几个命令之一。例如,单个按钮可以在“显示详细信息”和“隐藏详细信息”之间切换。在所有情况下,源对象和事件都将是相同的,但命令字符串将会标识出实际对应的动作。
    而public Object getSource()
    最初发生 Event 的对象。你这里是判断哪个按钮发生了事件,肯定是用getSource()方法取得发生事件的对象咯
    上面那个方法大概瞅一眼肯定不是这个作用吧
      

  2.   

    有一点明白 但是还是很糊涂 那个不也是对事件源捕捉吗? 那个number不也是最后输入的数吗?为什么我用
               if(number==b1) //不用getsource() 就算用 也不行啊 只要用了getactioncommand()到这就不能存哪个数
       { 
       op1 = Integer.parseInt(tf.getText()); //这里就不行啊 存不上这个数啊 把它去掉就行了                                                 可是去掉就没办法存第1次输入的数了
       
       fuhao=1; 
       tf.setText(""); 
       }