您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > eclipse实现简单计算器代码
packageComputer;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.border.*;importjava.util.LinkedList;importjava.text.NumberFormat;publicclassCacultorextendsFrameimplementsActionListener{/***@paramargs*/NumberButtonnumberButton[];OperationButtonoperationButton[];Button小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮;Panelpanel;JTextFieldresultShow;String运算符号[]={+,-,*,/};LinkedList链表;boolean是否按下等号=false;publicCacultor(){super(计算器);链表=newLinkedList();numberButton=newNumberButton[10];for(inti=0;i=9;i++){numberButton[i]=newNumberButton(i);numberButton[i].addActionListener(this);}operationButton=newOperationButton[4];for(inti=0;i4;i++){operationButton[i]=newOperationButton(运算符号[i]);operationButton[i].addActionListener(this);}小数点按钮=newButton(.);正负号按钮=newButton(+/-);等号按钮=newButton(=);求倒数按钮=newButton(1/x);退格按钮=newButton(退格);清零按钮=newButton(c);清零按钮.setForeground(Color.red);退格按钮.setForeground(Color.red);等号按钮.setForeground(Color.red);求倒数按钮.setForeground(Color.blue);正负号按钮.setForeground(Color.blue);小数点按钮.setForeground(Color.blue);退格按钮.addActionListener(this);清零按钮.addActionListener(this);等号按钮.addActionListener(this);小数点按钮.addActionListener(this);正负号按钮.addActionListener(this);求倒数按钮.addActionListener(this);resultShow=newJTextField(10);resultShow.setHorizontalAlignment(JTextField.RIGHT);resultShow.setForeground(Color.blue);resultShow.setFont(newFont(TimesRoman,Font.PLAIN,14));resultShow.setBorder(newSoftBevelBorder(BevelBorder.LOWERED));resultShow.setBackground(Color.white);resultShow.setEditable(false);panel=newPanel();panel.setLayout(newGridLayout(4,5));panel.add(numberButton[1]);panel.add(numberButton[2]);panel.add(numberButton[3]);panel.add(operationButton[0]);panel.add(清零按钮);panel.add(numberButton[4]);panel.add(numberButton[5]);panel.add(numberButton[6]);panel.add(operationButton[1]);panel.add(退格按钮);panel.add(numberButton[7]);panel.add(numberButton[8]);panel.add(numberButton[9]);panel.add(operationButton[2]);panel.add(求倒数按钮);panel.add(numberButton[0]);panel.add(正负号按钮);panel.add(小数点按钮);panel.add(operationButton[3]);panel.add(等号按钮);add(panel,BorderLayout.CENTER);add(resultShow,BorderLayout.NORTH);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setVisible(true);setBounds(100,50,240,180);setResizable(false);validate();}//按钮事件的处理publicvoidactionPerformed(ActionEvente){if(e.getSource()instanceofNumberButton){NumberButtonb=(NumberButton)e.getSource();if(链表.size()==0){intnumber=b.getNumber();链表.add(+number);resultShow.setText(+number);是否按下等号=false;}elseif(链表.size()==1&&是否按下等号==false){intnumber=b.getNumber();Stringnum=(String)链表.getFirst();Strings=num.concat(+number);链表.set(0,s);resultShow.setText(s);}elseif(链表.size()==1&&是否按下等号==true){intnumber=b.getNumber();链表.removeFirst();链表.add(+number);是否按下等号=false;resultShow.setText(+number);}elseif(链表.size()==2){intnumber=b.getNumber();链表.add(+number);resultShow.setText(+number);}elseif(链表.size()==3){intnumber=b.getNumber();Stringnum=(String)链表.getLast();Strings=num.concat(+number);链表.set(2,s);resultShow.setText(s);}}elseif(e.getSource()instanceofOperationButton){OperationButtonb=(OperationButton)e.getSource();if(链表.size()==1){Stringfuhao=b.get运算符号();链表.add(fuhao);}elseif(链表.size()==2){Stringfuhao=b.get运算符号();链表.set(1,fuhao);}elseif(链表.size()==3){Stringfuhao=b.get运算符号();Stringnumber1=(String)链表.getFirst();Stringnumber2=(String)链表.getLast();String运算符号=(String)链表.get(1);try{doublen1=Double.parseDouble(number1);doublen2=Double.parseDouble(number2);doublen=0;if(运算符号.equals(+)){n=n1+n2;}elseif(运算符号.equals(-)){n=n1-n2;}elseif(运算符号.equals(*)){n=n1*n2;}elseif(运算符号.equals(/)){n=n1/n2;}链表.clear();链表.add(+n);链表.add(fuhao);resultShow.setText(+n);}catch(Exceptionee){}}}elseif(e.getSource()==等号按钮){是否按下等号=true;if(链表.size()==1&&链表.size()==2){Stringnum=(String)链表.getFirst();resultShow.setText(+num);}elseif(链表.size()==3){Stringnumber1=(String)链表.getFirst();Stringnumber2=(String)链表.getLast();String运算符号=(String)链表.get(1);try{doublen1=Double.parseDouble(number1);doublen2=Double.parseDouble(number2);doublen=0;if(运算符号.equals(+)){n=n1+n2;}elseif(运算符号.equals(-)){n=n1-n2;}elseif(运算符号.equals(*)){n=n1*n2;}elseif(运算符号.equals(/)){n=n1/n2;}resultShow.setText(+n);链表.set(0,+n);链表.removeLast();链表.removeLast();}catch(Exceptionee){}}}elseif(e.getSource()==小数点按钮){if(链表.size()==0){是否按下等号=false;}elseif(链表.size()==1){Stringdot=小数点按钮.getLabel();Stringnum=(String)链表.getFirst();Strings=null;if(num.indexOf(dot)==-1){s=num.concat(dot);链表.set(0,s);}else{s=num;}链表.set(0,s);resultShow.setText(s);}elseif(链表.size()==3){Stringdot=小数点按钮.getLabel();Stringnum=(String)链表.getLast();Strings=null;if(num.indexOf(dot)==-1){s=num.concat(dot);链表.set(2,s);}else{s=num;}resultShow.setText(s);}}elseif(e.getSource()==退格按钮){if(链表.size()==1){Stringnum=(String)链表.getFirst();if(num.length()=1){num=num.substring(0,num.length()-1);链表.set(0,num);resultShow.setText(num);}else{链表.removeLast();resultShow.setText(0);}}elseif(链表.size()==3){Stringnum=(String)链表.getLast();if(num.length()=1){num=num.substri
本文标题:eclipse实现简单计算器代码
链接地址:https://www.777doc.com/doc-5853122 .html