package jisuanqi; import java.awt.*; import java.awt.event.*; public class Calculator implements ActionListener{ private double data1=0.0,data2=0.0; private String t_content; boolean number=false; //key用来标示用户所按下的按钮即加减乘除 //key为0标示+,为1表示-,为2表示*,为3表示/ short key=-1; //pkey的取值为-1~5 //pkey为-1表示第一次按下=按钮,为5说明不是第一次按下=按钮 //pkey为其他值0,1,2,3时分别代表+,-,*,/ short pkey=-1; Frame frame=new Frame("我的计算器");//定义标题为计算器的窗体 TextField textfield=new TextField(30);//定义计算器 //定义backspace,ce,c0按钮 Button backspace=new Button("Backspace"); Button ce=new Button("CE"); Button c0=new Button("C"); //定义面板,其中backspace,ce,c0按钮和计算机兰textfeild放在面板p1中 //其余按钮放在面板p2中 Panel p1=new Panel(); Panel p2=new Panel(); //定义界面上的按钮数组,即除去backspace,ce,c0按钮的所有按钮 String names[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="}; Button bb[]=new Button[names.length]; public static void main(String[] args){ Calculator cal=new Calculator(); cal.go(); } public void go(){ frame.setSize(500,300); frame.setLayout(new BorderLayout()); Font fonts=new Font("草体_GB2312",Font.PLAIN,12); // 第一页 //设置面板p1的字体和布局管理类型,将textfeild对象添加到面板中 p1.setFont(fonts); p1.setLayout(new GridLayout(2,1,5,10)); textfield.setFont(fonts); p1.add(textfield,null); //backspace,ce,c0按钮注册addActionListener方法 backspace.addActionListener(this); ce.addActionListener(this); c0.addActionListener(this); //将backspace,ce,c0按钮添加到p1面板中 p1.add(backspace,null); p1.add(ce,null); p1.add(c0,null); //设置面板p2的布局管理器类型为GridLayout p2.setLayout(new GridLayout(4,5,5,5)); //为按钮组中的按钮注册addActionListener方法 for(int i=0;i