这是一个很简单的计算器,当除数为0时为什么不能作出错误提示呢?是哪里出错了??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
//显示计算器结果的窗体
class Result extends JPanel{
JTextField text=new JTextField( "0 ");
Result(){
text.setHorizontalAlignment(SwingConstants.LEFT);
text.enable(false);
setLayout(new BorderLayout());
add(text,BorderLayout.CENTER);
}
}
//计算器数字按纽定义面板
class Number_Key extends JPanel{
JButton one=new JButton( " 1 ");
JButton two=new JButton( " 2 ");
JButton three=new JButton( " 3 ");
JButton plus=new JButton( " + ");
JButton ce=new JButton( " C ");
JButton four=new JButton( " 4 ");
JButton five=new JButton( " 5 ");
JButton six=new JButton( " 6 ");
JButton sub=new JButton( " - ");
JButton b=new JButton( " B ");
JButton seven=new JButton( " 7 ");
JButton eight=new JButton( " 8 ");
JButton nine=new JButton( " 9 ");
JButton mul=new JButton( " * ");
JButton ds=new JButton( "1/x ");
JButton zero=new JButton( " 0 ");
JButton zf=new JButton( "+/- ");
JButton point=new JButton( " . ");
JButton div=new JButton( " / ");
JButton equal=new JButton( " = ");
Number_Key(){
setLayout(new GridLayout(4,5,10,10));
add(one);
add(two);
add(three);
add(plus);
add(ce);
add(four);
add(five);
add(six);
add(sub);
add(b);
add(seven);
add(eight);
add(nine);
add(mul);
add(ds);
add(zero);
add(zf);
add(point);
add(div);
add(equal);
}
}
//计算器主类
public class Calculator extends JFrame implements ActionListener
{
Result result =new Result(); //定义text的面板
Number_Key number_key =new Number_Key(); //定义按钮面板
boolean com=false;//当单击按钮+/-/*/ /时,com=true
int i=0;//当i=0时说明是我们第一次输入,字符串sum不会累加
String sum= " ";//存放text的内容