检查此代码时出现运行时错误

时间:2014-04-03 11:44:30

标签: java applet

在这里我无法读取整数值,任何人都可以建议我在thios code.iam试图编写计算器代码这里一步一步写我的eroor,而checkig代码是否会采取整数来自butten anfd textfield的输入,如果我删除int cv = Integer.parseInt(t.getText());代码将正常工作,如果添加它给出错误

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
 public class ca_today extends Applet implements ActionListener{
    TextField t = new TextField(5);
    String str[] = {"+","-","/","%","*","<-","1/x","=","C",".","MR","MS"};
    Button b[]= new Button[12];
    Button b1[]= new Button[10];
    Button exit = new Button("EXIT");
    String pv,op;

    public void init() {
        setBackground( Color.CYAN );
        t.setBackground(Color.yellow);              
        Panel p = new Panel();
        p.add(t);
        t.addActionListener(this);        
        p.setLayout(new GridLayout(3,6));
        for( int i = 0 ; i < 10; i++ ){
            b1[i]=new Button(""+i);
            p.add(b1[i]);       
            b1[i].addActionListener(this);          
        }
        for(int j = 0 ; j <12; j++ ){
            b[j]=new Button(str[j]);
            p.add(b[j]);        
            b[j].addActionListener(this);
        }   
        p.add(exit);
        exit.addActionListener(this);
        add(p); 
    }

    public void actionPerformed(ActionEvent a){
        int res=0;
        String c=a.getActionCommand();
        int cv=Integer.parseInt(t.getText());//takes the i/p from textarea
        t.setText(""+cv);
        }
}

1 个答案:

答案 0 :(得分:0)

最好像这样添加

    public void actionPerformed(ActionEvent a){
    int res=0;
    String c=a.getActionCommand();
    t.setText(""+t.getText());
    }