推荐回答
给你一个参考,希望不要被吞了当晚餐importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.text.DecimalFormat;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassCalculator{//计算器面板privateJFramef=newJFrame"Calculator";//输入面扳privateJPanelinputPanel=newJPanel;//加减乘除面板privateJPaneloperationPanel=newJPanel;//数字面板privateJPanelbuttonsPanel=newJPanel;//输入数据文本框privateJTextFieldinput=newJTextField20;//退格键privateJButtonbackspace=newJButton"BackSpace";//清空privateJButtonCE=newJButton"CE";//删除privateJButtonC=newJButton"C";//四则运算符号键privateJButtonadd=newJButton"+";privateJButtonsub=newJButton"-";privateJButtonmul=newJButton"*";privateJButtondiv=newJButton"/";//小数点privateJButtondecimal=newJButton".";//等号privateJButtonequal=newJButton"=";//数字键privateJButtonzero=newJButton"0";privateJButtonone=newJButton"1";privateJButtontwo=newJButton"2";privateJButtonthree=newJButton"3";privateJButtonfour=newJButton"4";privateJButtonfive=newJButton"5";privateJButtonsix=newJButton"6";privateJButtonseven=newJButton"7";privateJButtoneight=newJButton"8";privateJButtonnine=newJButton"9";privateStringnum1="";//保存第一个运算数字privateStringoperator="";//保存运算符号publicstaticvoidmainStringargs{newCalculator;//new计算器实例}publicCalculator{//添加组件,布局inputPanel.addinput;f.addinputPanel,BorderLayout.NORTH;operationPanel.addbackspace;operationPanel.addCE;operationPanel.addC;f.addoperationPanel,BorderLayout.CENTER;buttonsPanel.addadd;buttonsPanel.addsub;buttonsPanel.addmul;buttonsPanel.adddiv;buttonsPanel.addone;buttonsPanel.addtwo;buttonsPanel.addthree;buttonsPanel.addzero;buttonsPanel.addfour;buttonsPanel.addfive;buttonsPanel.addsix;buttonsPanel.adddecimal;buttonsPanel.addseven;buttonsPanel.addeight;buttonsPanel.addnine;buttonsPanel.addequal;buttonsPanel.setLayoutnewGridLayout4,4;f.addbuttonsPanel,BorderLayout.SOUTH;//注册各个组件监听事件backspace.addMouseListenernewOperationMouseListener;CE.addMouseListenernewOperationMouseListener;C.addMouseListenernewOperationMouseListener;decimal.addMouseListenernewOperationMouseListener;equal.addMouseListenernewOperationMouseListener;//注册四则运算监听add.addMouseListenernewCalcMouseListener;sub.addMouseListenernewCalcMouseListener;mul.addMouseListenernewCalcMouseListener;div.addMouseListenernewCalcMouseListener;//注册数字监听事件zero.addMouseListenernewNumberMouseListener;one.addMouseListenernewNumberMouseListener;two.addMouseListenernewNumberMouseListener;three.addMouseListenernewNumberMouseListener;four.addMouseListenernewNumberMouseListener;five.addMouseListenernewNumberMouseListener;six.addMouseListenernewNumberMouseListener;seven.addMouseListenernewNumberMouseListener;eight.addMouseListenernewNumberMouseListener;nine.addMouseListenernewNumberMouseListener;f.setVisibletrue;f.pack;f.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE;}privateclassNumberMouseListenerimplementsMouseListener{publicvoidmouseClickedMouseEvente{ifinput.getText.trim.equals"0"{//如果文本框已经是0,结果还是0input.setTextJButtone.getSource.getText.trim;}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10input.setTextinput.getText.concatJButtone.getSource.getText.trim;}}publicvoidmouseEnteredMouseEvente{}publicvoidmouseExitedMouseEvente{}publicvoidmousePressedMouseEvente{}publicvoidmouseReleasedMouseEvente{}}privateclassCalcMouseListenerimplementsMouseListener{//如果输入的是运算符号,保存第一个结果和运算符号publicvoidmouseClickedMouseEvente{num1=input.getText.trim;input.setText"";operator=JButtone.getSource.getText.trim;}publicvoidmouseEnteredMouseEvente{}publicvoidmouseExitedMouseEvente{}publicvoidmousePressedMouseEvente{}publicvoidmouseReleasedMouseEvente{}}privateclassOperationMouseListenerimplementsMouseListener{publicvoidmouseClickedMouseEvente{ife.getSource==backspace{//退格键,删除一个直到没有字符删除StringinputText=input.getText;ifinputText.length>0{input.setTextinputText.substring0,inputText.length-1;}}elseife.getSource==C{input.setText"0";//C,清空所有运算数字和符号num1="";}elseife.getSource==CE{input.setText"0";//CE-->将文本框置为0}elseife.getSource==decimal{Stringtext=input.getText.trim;//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点iftext.indexOf"."==-1{input.setTexttext.concat".";}}elseife.getSource==equal{//如果是等号if!operator.trim.equals""{if!input.getText.trim.equals""{doubleresult=0D;ifoperator.equals"+"{//执行加法运算result=Double.parseDoublenum1+Double.parseDoubleinput.getText.trim;}elseifoperator.equals"-"{//减法运算result=Double.parseDoublenum1-Double.parseDoubleinput.getText.trim;}elseifoperator.equals"*"{//乘法运算result=Double.parseDoublenum1*Double.parseDoubleinput.getText.trim;}elseifoperator.equals"/"{//除法运算result=Double.parseDoublenum1/Double.parseDoubleinput.getText.trim;}//格式化最终结果,保留两位小数点input.setTextnewDecimalFormat"0.00".formatresult;}}}}publicvoidmouseEnteredMouseEvente{}publicvoidmouseExitedMouseEvente{}publicvoidmousePressedMouseEvente{}publicvoidmouseReleasedMouseEvente{}}。
赵高启2019-12-21 21:07:34
提示您:回答为网友贡献,仅供参考。
其他回答
-
importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;importjavax.swing.SwingConstants;publicclassJisuanqiextendsJFrameimplementsActionListener{/****/privatestaticfinallongserialVersionUID=1L;Resultresult=newResult;//定义text的面板Number_Keynumber_key=newNumber_Key;//定义按钮面板//当点击按钮+、-、*、/时,com=truebooleancom=false;//当i=0时说明是我们第一次输入,字符串text不会累加inti=0;//存放text的内容Stringtext="";//存放点击按钮+、-、*、/之前的数值doubledefbutton=0;//+、-、*、/的代号分别为1,2,3,4intsymbol=0;//构造函数Jisuanqi{super"计算器";//设定标题setDefaultCloseOperationJFrame.EXIT_ON_CLOSE;//设定关闭窗体时退出程序JPanelpane=newJPanel;//定义主面板pane.setLayoutnewBorderLayout;setBounds380,220,30,80;//前两个参数是在屏幕上显示的坐标,后两个是大小//替换图标ImageIconicon=newImageIcon"F:1.GIF";//Jisuanqi.class.getResource"APPLE.GIF"//;setIconImageicon.getImage;pane.addresult,BorderLayout.NORTH;pane.addnumber_key,BorderLayout.CENTER;pane.addnumber_key.equal,BorderLayout.SOUTH;number_key.one.addActionListenerthis;//对1按钮添加监听事件number_key.two.addActionListenerthis;//对2按钮添加监听事件number_key.three.addActionListenerthis;//对3按钮添加监听事件number_key.four.addActionListenerthis;//对4按钮添加监听事件number_key.five.addActionListenerthis;//对5按钮添加监听事件number_key.six.addActionListenerthis;//对6按钮添加监听事件number_key.seven.addActionListenerthis;//对7按钮添加监听事件number_key.eight.addActionListenerthis;//对8按钮添加监听事件number_key.nine.addActionListenerthis;//对9按钮添加监听事件number_key.zero.addActionListenerthis;//对0按钮添加监听事件number_key.ce.addActionListenerthis;//对置零按钮添加监听事件number_key.plus.addActionListenerthis;//对+按钮添加监听事件number_key.equal.addActionListenerthis;//对=按钮添加监听事件number_key.sub.addActionListenerthis;//对-按钮添加监听事件number_key.mul.addActionListenerthis;//对*按钮添加监听事件number_key.div.addActionListenerthis;//对/按钮添加监听事件number_key.point.addActionListenerthis;//对.按钮添加监听事件setContentPanepane;pack;//初始化窗体大小为正好盛放所有按钮}//各个按钮触发的事件publicvoidactionPerformedActionEvente{/**如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true如果没有com=*false;或者是否点击数字键,如果是i=1,如果没有i=0;*/ife.getSource==number_key.one{ifcom||i==0{result.text.setText"1";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"1";}}elseife.getSource==number_key.two{ifcom||i==0{result.text.setText"2";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"2";}}elseife.getSource==number_key.three{ifcom||i==0{result.text.setText"3";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"3";}}elseife.getSource==number_key.four{ifcom||i==0{result.text.setText"4";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"4";}}elseife.getSource==number_key.five{ifcom||i==0{result.text.setText"5";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"5";}}elseife.getSource==number_key.six{ifcom||i==0{result.text.setText"6";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"6";}}elseife.getSource==number_key.seven{ifcom||i==0{result.text.setText"7";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"7";}}elseife.getSource==number_key.eight{ifcom||i==0{result.text.setText"8";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"8";}}elseife.getSource==number_key.nine{ifcom||i==0{result.text.setText"9";com=false;i=1;}else{text=result.text.getText;result.text.setTexttext+"9";}}/**对于0这个按钮有一定的说法,在程序里不会出现如00000这样的情况,加了判断条件就是*如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添加0;否则保持原来的数值不变*/elseife.getSource==number_key.zero{//result.text.getText是得到text里内容的意思ifcom||i==0{result.text.setText"0";com=false;i=1;}else{text=result.text.getText;ifFloat.parseFloattext>0||Float.parseFloattext<0{//Float.parseFloattext就是类型转换了,下面都是一样result.text.setTexttext+"0";}else{iftext.trim.indexOf"."==-1{result.text.setTexttext;}else{result.text.setTexttext+"0";}}}}elseife.getSource==number_key.ce{result.text.setText"0";i=0;com=true;//text="";defbutton=0;}/**本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加,不存在就添加.*/elseife.getSource==number_key.point{ifcom||i==0{result.text.setText"0.";com=false;i=1;}else{text=result.text.getText;iftext.trim.indexOf"."==-1{result.text.setTexttext+".";}else{result.text.setTexttext;}}}//获得点击+之前的数值elseife.getSource==number_key.plus{com=true;i=0;defbutton=Double.parseDoubleresult.text.getText;symbol=1;}//获得点击-之前的数值elseife.getSource==number_key.sub{com=true;i=0;defbutton=Double.parseDoubleresult.text.getText;symbol=2;}//获得点击*之前的数值elseife.getSource==number_key.mul{com=true;i=0;defbutton=Double.parseDoubleresult.text.getText;System.out.printlndefbutton;symbol=3;}//获得点击/之前的数值elseife.getSource==number_key.div{com=true;i=0;defbutton=Double.parseDoubleresult.text.getText;symbol=4;}elseife.getSource==number_key.equal{switchsymbol{case1:{//计算加法doublead=defbutton+Double.parseDoubleresult.text.getText;result.text.setTextad+"";i=0;text="";break;}case2:{//计算减法doublead=defbutton-Double.parseDoubleresult.text.getText;result.text.setTextString.valueOfad;i=0;text="";break;}case3:{//计算乘法doublead=defbutton*Double.parseDoubleresult.text.getText;result.text.setTextad+"";i=0;text="";break;}case4:{//计算除法doublead=defbutton/Double.parseDoubleresult.text.getText;result.text.setTextad+"";i=0;text="";break;}}System.out.printlncom;}System.out.printlnresult.text.getText;}@SuppressWarnings"deprecation"publicstaticvoidmainStringargs{Jisuanqiloveyou=newJisuanqi;loveyou.show;}}//计算器数字按钮定义面板classNumber_KeyextendsJPanel{/****/privatestaticfinallongserialVersionUID=1L;JButtonzero=newJButton"0";//数字键0JButtonone=newJButton"1";//数字键1JButtontwo=newJButton"2";//数字键2JButtonthree=newJButton"3";//数字键3JButtonfour=newJButton"4";//数字键4JButtonfive=newJButton"5";//数字键5JButtonsix=newJButton"6";//数字键6JButtonseven=newJButton"7";//数字键7JButtoneight=newJButton"8";//数字键8JButtonnine=newJButton"9";//数字键9JButtonplus=newJButton"+";JButtonsub=newJButton"-";JButtonmul=newJButton"*";JButtondiv=newJButton"/";JButtonequal=newJButton"=";JButtonce=newJButton"清零";//置零键JButtonpoint=newJButton".";Number_Key{setLayoutnewGridLayout4,4,1,1;//定义布局管理器为网格布局setBackgroundColor.blue;//设置背景颜色//添加按钮addone;addtwo;addthree;addfour;addfive;addsix;addseven;addeight;addnine;addzero;addplus;addsub;addmul;adddiv;addpoint;addequal;addce;}}//计算器显示结果的窗体classResultextendsJPanel{/****/privatestaticfinallongserialVersionUID=1L;//text先是输入和结果JTextFieldtext=newJTextField"0";@SuppressWarnings"deprecation"Result{//讲输入的数字或得到的结果在text的右边显示text.setHorizontalAlignmentSwingConstants.RIGHT;text.enablefalse;//文本框不能编辑setLayoutnewBorderLayout;//设定布局管理器边框布局addtext,BorderLayout.CENTER;//text放置在窗体的中间}。
黄看看2019-12-21 21:20:57
-
//布局没有调整,需要你自己去调整代码如下:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class JieMian extends JFrame implements ActionListener{JTextField c1;JTextField c2;JLabel c;JLabel equal=new JLabel"=";JLabel result;JLabel choose=new JLabel"请选择";JButton add=new JButton"+";JButton sub=new JButton"-";JButton mul=new JButton"×";JButton div=new JButton"÷";JButton cal=new JButton"计算";JButton clear=new JButton"清除";public JieMian{setLayoutnew FlowLayout;c1=new JTextField5;c2=new JTextField5;c=new JLabel"+";result=new JLabel"";addc1;addc;addc2;addequal;addresult;addchoose;addadd;addsub;addmul;adddiv;addcal;addclear;add.addActionListenerthis;sub.addActionListenerthis;mul.addActionListenerthis;div.addActionListenerthis;cal.addActionListenerthis;clear.addActionListenerthis;setVisibletrue;pack;}@Overridepublic void actionPerformedActionEvent e {ife.getSource==add{c.setText"+";}else ife.getSource==sub{c.setText"-";}else ife.getSource==mul{c.setText"×";}else ife.getSource==div{c.setText"÷";}else ife.getSource==cal{double cc1,cc2;try{cc1=Double.parseDoublec1.getText;}catchException ex{cc1=0;}try{cc2=Double.parseDoublec2.getText;}catchException ex{cc2=0;}ifc.getText=="+"{result.setTextString.valueOfcc1+cc2;}else ifc.getText=="-"{result.setTextString.valueOfcc1-cc2;}else ifc.getText=="×"{result.setTextString.valueOfcc1*cc2;}else ifc.getText=="÷"{ifcc2!=0{result.setTextString.valueOfcc1/cc2;}else{result.setText"NAN";}}}else ife.getSource==clear{c1.setText"";c2.setText"";result.setText"";}}public static void mainString args{JieMian jiemian=new JieMian;}。
连俸平2019-12-21 21:02:44
-
这个主要是处理后台的表达式计算,有几种方法,介绍两种,一种是:jdk6以上用的;类ScriptEngine,用法如下:publicclassCalTest{publicstaticvoidmainStringargsthrowsException{Evaluatorevl=newEvaluator;Stringexp="2+7-5*3.14159*#{x}+sin#{y}";evl.putVariable"x",5;evl.putVariable"y",30;doubleresult=Double.parseDoubleevl.evaluateexp;System.out.printlnresult;}。
黄盛章2019-12-21 20:58:08