[code]import java.awt.*;
import java.awt.event.*;
import java.awt.Button;public class Calculator extends WindowAdapter{
Panel p1=new Panel();
Panel p2=new Panel();
TextField txt;

private String ss[]={"7","8","9","+","4","5","6","-","1","2","3","*","清空","0","=","/"};
private Button[] b=new Button[16];//按钮的个数
static double x,y;
static String s;
static int z;
StringBuffer str=new StringBuffer(); public static void main(String[] args) {//主函数
        (new Calculator()).Myframe();
       }

  public void Myframe(){
MyFrame mf =new MyFrame("MyCalculator");

 //添加按钮到p2界面
 for(int i=0;i<=15;i++){
 b[i]=new Button(ss[i]);
 }
 for(int j=0;j<=15;j++){
 p2.add(b[j]);
 }
// b[16].setBackground(Color.yellow);
 //建立一个文本框用于显示和输入;
 txt=new TextField(15);
 txt.setEditable(false);//设置该对话框不能直接输入;
 for(int i=0;i<=15;i++){
 b[i].addActionListener(new buttonlistener());
 }
 //mf.setBackground(Color.red);
 //设置p1的界面
 p1.setLayout(new BorderLayout());
 p1.add(txt,"North");

 //建立数字按键布局Panel p2;
 p2.setLayout(new GridLayout(4,4));//4*4的网格布局  
 
mf.add(p1,"North");
    mf.add(p2,"South");
    mf.pack();
    mf.setVisible(true);
 
 
}
class buttonlistener implements ActionListener {//
public void actionPerformed(ActionEvent e) {
//获取发生事件的按钮
// 其他的数字键 
try{
if(e.getSource()==b[12])//选择"CE"清零 
{  
   txt.setText("0");//把显示屏清零 
   str.setLength(0);//清空字符串缓冲区以准备接收新的输入运算数 } 
}
else if(e.getSource()==b[3])//"+"号的监听事件;

  x=Double.parseDouble(txt.getText().trim()); 
  str.setLength(0); y=0d; z=0; 

else if(e.getSource()==b[7])//"-"号的监听事件;

  x=Double.parseDouble(txt.getText().trim()); 
  
  str.setLength(0); y=0d; z=1; 

else if(e.getSource()==b[11])// 单击乘号按钮获得x的值和z的值并清空y的值 
{ x=Double.parseDouble(txt.getText().trim());
  str.setLength(0); y=0d; z=2; }
else if(e.getSource()==b[15])// 单击除号按钮获得x的值和z的值并清空y的值 
{ x=Double.parseDouble(txt.getText().trim()); 
  str.setLength(0); y=0d; z=3; }

else if(e.getSource()==b[14])// 单击等号按钮输出计算结果 
{ str.setLength(0); 
  switch(z) 
{
case 0 : txt.setText(""+(x+y));
  break; 
case 1 : txt.setText(""+(x-y));
break;
case 2 : txt.setText(""+(x*y));
break; 
case 3 : txt.setText(""+(x/y));
break; 

} {  //用getActionCommand()返回按键的值
txt.setText(str.append(e.getActionCommand()).toString());
                 y=Double.parseDouble(txt.getText().trim());
}    }catch(NumberFormatException e1) {  
                txt.setText("数字格式异常");  
          }catch(StringIndexOutOfBoundsException e1) {  
                txt.setText("字符串索引越界");  
           }  
  
class MyFrame extends Frame{//构建主要面板
public MyFrame(String string) {
     super(string);  
     this.setLocation(300, 300);
     this.setSize(300,300);
     this.setVisible(true);
     this.setLayout(new BorderLayout());
//增加一个可以关闭的监听任务!
 this.addWindowListener(new WindowAdapter(){
 public void windowClosing(WindowEvent e){
 setVisible(false);
 System.exit(0);
 }
 });
 
}
}[code=java][code] {  //用getActionCommand()返回按键的值
txt.setText(str.append(e.getActionCommand()).toString());
                 y=Double.parseDouble(txt.getText().trim());
} [code=java]这块是我用来返回按键值的监听任务的。
但是  刚刚开始的时候thows Exception 按下1+2= 结果就是不显示出来;
后来用了try——catch之后,一直报错,一按运算符,就显示数据格式异常,跪求高手指点一下。
如果看不明白,也可以帮我做一个监听,不胜感激,奉上80分!Java异常简易计算器

解决方案 »

  1.   

    实现几个ActionListener,数字类的一个,其他的几个,不要放到一个里。
      

  2.   

    我debug了一下你的代码~ 在那段代码加个else 就行了~ 建议学会用debug~ 常用的异常都去了解~ 
    你先试下~ 还有不懂的可以问我~ 

    }else{ // 用getActionCommand()返回按键的值
    txt.setText(str.append(e.getActionCommand()).toString());
    y = Double.parseDouble(txt.getText().trim());
    }
      

  3.   


    import java.awt.*;
    import java.awt.event.*;
    public class Calc  
    {
    static Button bt1 = new Button("1");
    static Button bt2 = new Button("2");
    static Button bt3 = new Button("3");
    static Button bt4 = new Button("4");
    static Button bt5 = new Button("5");
    static Button bt6 = new Button("6");
    static Button bt7 = new Button("7");
    static Button bt8 = new Button("8");
    static Button bt9 = new Button("9");
    static Button bt0 = new Button("0");
    static Button add = new Button("+");
    static Button sub = new Button("-");
    static Button mul = new Button("*");
    static Button div = new Button("/");
    static Button point = new Button(".");
    static Button dengyu = new Button("=");
    static Label lb = new Label("0");
    static boolean clr=true; //是否清屏
    static String cmd; //运算符号 + - * /
    static Double left,right,result;//左,右操作数,结果
    static boolean pointHave; //已有小数点,不能再打小数点
    static boolean firstPoint=true; //第一个是小数点,需加0.
    Calc()
    {
    Frame fr = new Frame();
    Panel p3 = new Panel();
    p3.setLayout(new GridLayout(4,4));
    p3.add(bt1);
    p3.add(bt2);
    p3.add(bt3);
    p3.add(add);
    p3.add(bt4);
    p3.add(bt5);
    p3.add(bt6);
    p3.add(sub);
    p3.add(bt7);
    p3.add(bt8);
    p3.add(bt9);
    p3.add(mul);
    p3.add(bt0);
    p3.add(point);
    p3.add(dengyu);
    p3.add(div);
    fr.setSize(200,230);
    fr.setLocation(50,50);
    fr.add(lb,BorderLayout.NORTH);
    fr.add(p3,BorderLayout.CENTER);
    addListener();
    fr.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(1);
    }
    });
    fr.setVisible(true);
    }
    public void addListener()//添加监听器
    {
    bt0.addActionListener(new NumHandle(0));
    bt1.addActionListener(new NumHandle(1));
    bt2.addActionListener(new NumHandle(2));
    bt3.addActionListener(new NumHandle(3));
    bt4.addActionListener(new NumHandle(4));
    bt5.addActionListener(new NumHandle(5));
    bt6.addActionListener(new NumHandle(6));
    bt7.addActionListener(new NumHandle(7));
    bt8.addActionListener(new NumHandle(8));
    bt9.addActionListener(new NumHandle(9));
    add.addActionListener(new CalcHandle("+"));
    sub.addActionListener(new CalcHandle("-"));
    mul.addActionListener(new CalcHandle("*"));
    div.addActionListener(new CalcHandle("/"));
    dengyu.addActionListener(new DengHandle());
    point.addActionListener(new PointHandle());
    }
    public static void main(String[] args)
    {
    new Calc();
    }
    }
    //按下数字键
    class NumHandle implements ActionListener
    {
    int num;
    NumHandle(int num)
    {
    this.num = num;
    }
    public void actionPerformed(ActionEvent e)
    {
    if(Calc.clr)
    {
    Calc.lb.setText("");
    Calc.clr = false;
    Calc.firstPoint = false;
    }
    Calc.lb.setText(Calc.lb.getText()+num);
    }
    }
    //按下小数点
    class PointHandle implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if(Calc.pointHave)//已有小数点,再按无反应
    {
    return;
    }
    if(Calc.firstPoint)//未按数字键,按下小数点,则在前面加上0.
    {
    Calc.lb.setText("0.");
    }
    else
    Calc.lb.setText(Calc.lb.getText()+".");
    Calc.pointHave = true;
    Calc.clr = false;
    }
    }
    //按下+ - * / 运算键
    class CalcHandle implements ActionListener
    {
    String cmd;
    CalcHandle(String cmd)
    {
    this.cmd = cmd;
    }
    public void actionPerformed(ActionEvent e)
    {
    Calc.cmd = cmd;
    Calc.left = Double.parseDouble(Calc.lb.getText());
    Calc.clr = true;
    Calc.pointHave = false;
    }
    }
    //按下等于号
    class DengHandle implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    Calc.right = Double.parseDouble(Calc.lb.getText());
    if(Calc.cmd == "+")
    {
    Calc.result = Calc.left + Calc.right;
    }
    else if(Calc.cmd == "-")
    {
    Calc.result = Calc.left - Calc.right;
    }
    else if(Calc.cmd == "*")
    {
    Calc.result = Calc.left * Calc.right;
    }
    else if(Calc.cmd == "/")
    {
    Calc.result = Calc.left / Calc.right;
    }
    Calc.lb.setText(Calc.result.toString());
    Calc.clr = true;
    Calc.pointHave = false;
    Calc.firstPoint = true;
    }
    }