当前位置:文档之家› java简单计算器源代码

java简单计算器源代码

j a v a简单计算器源代码集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-简单计算器代码packagecalcultorthree;/***本例实现了简单计算器代码,具备加减乘除和正弦功能,旨在抱砖引玉。

熟悉java的同学,可以在此基础上实现更复杂的功能。

*@authorFjsh*/publicclassCalcultorThree{//新建对象,在构造函数中进行初始化JFrameframe;//新建窗体对象JButtonbuttonzero,buttondot,buttonequal;//新建按钮“0”“.”“=”JButtonbuttonplus,buttonminus,buttonmultiple,buttondevision,buttonsin,buttontozero;//新建按钮“+”“-”“*”“/”“sin”和归零按钮JButtonbuttonone,buttontwo,buttonthree,buttonfour,buttonfive,buttonsix, buttonseven,buttoneight,buttonnine;//新建数字按钮“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”JPanelpanelwest,panelcenter,paneleast;//新建三个面板TextFieldtf;//新建文本区域对象publicCalcultorThree(){//初始化对象tf=newTextField(30);//构造空文本字段,字符宽度为30frame=newJFrame("CalculatorThree");//构造窗体对象,名称为“CalculatorThree”panelcenter=newJPanel();//构造面板,放到窗体中央panelwest=newJPanel();//构造面板,放到窗体西边paneleast=newJPanel();//构造面板,放到窗体东边Handleh=newHandle();//新建Handle类对象,Handle类为事件监听类//创建数字按钮对象,1、2、3、4、5、6、7、8、9buttonone=newJButton("1");buttontwo=newJButton("2");buttonthree=newJButton("3");buttonfour=newJButton("4");buttonfive=newJButton("5");buttonsix=newJButton("6");buttonseven=newJButton("7");buttoneight=newJButton("8");buttonnine=newJButton("9");panelcenter.setLayout(newGridLayout(3,3));//设置面板布局为网格布局,3行3列//将数字按钮添加到中间面板panelcenter.add(buttonone);panelcenter.add(buttontwo);panelcenter.add(buttonthree);panelcenter.add(buttonfour);panelcenter.add(buttonfive);panelcenter.add(buttonsix);panelcenter.add(buttonseven);panelcenter.add(buttonnine);//为10个按钮注册事件监听器buttonone.addActionListener(h);buttontwo.addActionListener(h);buttonthree.addActionListener(h);buttonfour.addActionListener(h);buttonfive.addActionListener(h);buttonsix.addActionListener(h);buttonseven.addActionListener(h);buttoneight.addActionListener(h);buttonnine.addActionListener(h);//构造按钮“0”“.”“=”,注册事件监听器,设置1行3列的布局,添加到到西边的面板buttonzero=newJButton("0");buttondot=newJButton(".");buttonequal=newJButton("=");buttonzero.addActionListener(h);buttondot.addActionListener(h);buttonequal.addActionListener(h);panelwest.setLayout(newGridLayout(3,1));panelwest.add(buttonzero);panelwest.add(buttondot);//构造操作按钮“+”“-”“*”“/”“sin”“>0”,其中“>0”为归零按钮buttonplus=newJButton("+");buttonminus=newJButton("-");buttonmultiple=newJButton("*");buttondevision=newJButton("/");buttonsin=newJButton("sin");buttontozero=newJButton(">0");paneleast.setLayout(newGridLayout(3,1));//设置西边的布局为3行1列//将操作按钮“+”“-”“*”“/”“sin”“>0”添加到西边的面板中paneleast.add(buttonplus);paneleast.add(buttonminus);paneleast.add(buttonmultiple);paneleast.add(buttondevision);paneleast.add(buttonsin);paneleast.add(buttontozero);//为操作按钮“+”“-”“*”“/”“sin”“>0”注册监听器buttonplus.addActionListener(h);buttonminus.addActionListener(h);buttonmultiple.addActionListener(h);buttondevision.addActionListener(h);buttonsin.addActionListener(h);buttontozero.addActionListener(h);frame.setLayout(newBorderLayout());//设置窗体为边界布局frame.add(paneleast,"East");//将东边面板paneleast添加到窗体的东边frame.add(tf,BorderLayout.NORTH);//将tf文本区域添加到窗体的北边,即顶部frame.add(panelwest,BorderLayout.WEST);//将panelwest面板添加到窗体西边frame.add(panelcenter,BorderLayout.CENTER);//将panelcenter面板添加到窗体中间frame.pack();//设置窗体大小,适合其子组件的首选大小和布局frame.setLocation(500,500);//设置窗体显示位置为(500,500)frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置布局窗体默认关闭方式frame.setVisible(true);//设置窗体可见}publicstaticvoidmain(String[]args){newCalcultorThree();//主方法中新建对象}classHandleimplementsActionListener{//实现动作监听器类intbiaozhi=0;//此标志标志加减乘除操作doubleflag1=0,flag2=0,flag3=0;//flag1、flag2为两个操作数,flag3为结果@OverridepublicvoidactionPerformed(ActionEvente){//方法重写try{//此处可能会产生异常,用try、catch捕捉异常,不用处理if(e.getSource()==buttondot){//小数点tf.setText("0.");}if(e.getSource()==buttontozero){//归零操作tf.setText("");}if(e.getSource()==buttonzero){//按键0操作tf.setText(tf.getText()+"0");flag1=Double.parseDouble(tf.getText());//将文本区域转换成Double类型,赋给flag1}if(e.getSource()==buttonone){//按键1操作tf.setText(tf.getText()+"1");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttontwo){//按键2操作tf.setText(tf.getText()+"2");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttonthree){//按键3操作tf.setText(tf.getText()+"3");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttonfour){//按键4操作tf.setText(tf.getText()+"4");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttonfive){//按键5操作tf.setText(tf.getText()+"5");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttonsix){//按键6操作tf.setText(tf.getText()+"6");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttonseven){//按键7操作tf.setText(tf.getText()+"7");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttoneight){//按键8操作tf.setText(tf.getText()+"8");flag1=Double.parseDouble(tf.getText());}elseif(e.getSource()==buttonnine){//按键9操作tf.setText(tf.getText()+"9");flag1=Double.parseDouble(tf.getText());}if(e.getSource()==buttonplus){//加法操作tf.setText("");flag2=flag1;biaozhi=0;}if(e.getSource()==buttonminus){//减法操作tf.setText("");flag2=flag1;biaozhi=1;}if(e.getSource()==buttonmultiple){//乘法操作tf.setText("");flag2=flag1;biaozhi=2;}if(e.getSource()==buttondevision){//除法操作tf.setText("");flag2=flag1;biaozhi=3;}if(e.getSource()==buttonsin){//正弦操作flag3=Math.sin(flag1);tf.setText(flag3+"");}if(e.getSource()==buttonequal){//等号操作,利用biaozhi判断进行相应加减乘除操作if(biaozhi==0){flag3=flag1+flag2;}if(biaozhi==1){flag3=flag1-flag2;}if(biaozhi==2){flag3=flag1*flag2;}if(biaozhi==3){flag3=flag1/flag2;}tf.setText(flag3+"");}}catch(Exceptionex){}}}}。

相关主题