java实现简单的计算器类实例
本文实例讲述了java实现简单的计算器类。分享给大家供大家参考。具体如下:
packagechap; importjava.awt.BorderLayout; importjava.awt.Color; importjava.awt.FlowLayout; importjava.awt.GridLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.text.DecimalFormat; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JPanel; importjavax.swing.JTextField; publicclassCalculator{ privateJFrameframe; privateJPanelpanel,panelKeys,panelKeys_up,panelKeys_down; privateJTextFieldtextComputer;//计算区域 privateJButtonbuttonBk,buttonC;//退格键和清零键 privateJButtonbutton[];//数字键组 privateJButtonbuttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual; privatedoubleresult;//计算结果 privatefinalshortADD=1; privatefinalshortSUB=2; privatefinalshortMUL=3; privatefinalshortDIV=4; privateshortoperator=-1;//运算符 publicCalculator(){ frame=newJFrame("计算机"); frame.setSize(400,250); panel=newJPanel();//全局面板 panel.setVisible(true); frame.setVisible(true); frame.getContentPane().add(panel); ActionListenerlistener=newComputerActionListener();//按键监听器 //计算区 textComputer=newJTextField(15); textComputer.setText(""); textComputer.setEditable(false); textComputer.setBackground(newColor(255,255,255)); //功能键上半部分 panelKeys_up=newJPanel(); panelKeys_up.setLayout(newFlowLayout(FlowLayout.RIGHT)); buttonBk=newJButton("Backspace"); buttonBk.setForeground(newColor(255,0,0)); buttonC=newJButton("C"); buttonC.setForeground(newColor(255,0,0)); buttonBk.addActionListener(listener); buttonC.addActionListener(listener); panelKeys_up.add(buttonBk); panelKeys_up.add(buttonC); //功能键下半部分 panelKeys_down=newJPanel(); panelKeys_down.setLayout(newGridLayout(4,5)); button=newJButton[10]; for(inti=0;i<button.length;i++){ button[i]=newJButton(Integer.toString(i)); button[i].setForeground(newColor(255,0,0)); } buttonAddSub=newJButton("+/-"); buttonAddSub.setForeground(newColor(255,0,0)); buttonAdd=newJButton("+"); buttonAdd.setForeground(newColor(255,0,0)); buttonSub=newJButton("-"); buttonSub.setForeground(newColor(255,0,0)); buttonMul=newJButton("*"); buttonMul.setForeground(newColor(255,0,0)); buttonDiv=newJButton("/"); buttonDiv.setForeground(newColor(255,0,0)); button1=newJButton(); button2=newJButton(); button3=newJButton(); button1.setForeground(newColor(255,0,0)); button2.setForeground(newColor(255,0,0)); button3.setForeground(newColor(255,0,0)); buttonEqual=newJButton("="); buttonEqual.setForeground(newColor(255,0,0)); buttonAddSub.addActionListener(listener); buttonAdd.addActionListener(listener); buttonSub.addActionListener(listener); buttonMul.addActionListener(listener); buttonDiv.addActionListener(listener); buttonEqual.addActionListener(listener); for(inti=0;i<=9;i++){ button[i].addActionListener(listener); } for(inti=0;i<=9;i++){ panelKeys_down.add(button[i]); } panelKeys_down.add(buttonAddSub); panelKeys_down.add(buttonAdd); panelKeys_down.add(buttonSub); panelKeys_down.add(buttonMul); panelKeys_down.add(buttonDiv); panelKeys_down.add(buttonEqual); panel.setLayout(newBorderLayout()); panel.add(textComputer,BorderLayout.NORTH); panel.add(panelKeys_up,BorderLayout.CENTER); panel.add(panelKeys_down,BorderLayout.SOUTH); } classComputerActionListenerimplementsActionListener{ @Override publicvoidactionPerformed(ActionEventevent){ //TODOAuto-generatedmethodstub ObjectkeyButton=event.getSource(); Stringtext=textComputer.getText(); DecimalFormatdf=newDecimalFormat("0.###########"); //Backspace if(keyButton==buttonBk&&text.length()>0){ textComputer.setText(text.substring(0,text.length()-1)); } //C键 if(keyButton==buttonC){ result=0; textComputer.setText(""); } //数字键 for(inti=0;i<10;i++){ if(keyButton==button[i]){ textComputer.setText(text+i); } } if(keyButton==buttonAdd){ operator=1; } if(keyButton==buttonSub){ operator=2; } if(keyButton==buttonMul){ operator=3; } if(keyButton==buttonDiv){ operator=4; } //符号键 if(keyButton==buttonAdd||keyButton==buttonSub||keyButton==buttonMul||keyButton==buttonDiv||keyButton==buttonEqual){ switch(operator){ caseADD: result+=Double.parseDouble(text); break; caseSUB: result-=Double.parseDouble(text); break; caseMUL: result*=Double.parseDouble(text); break; caseDIV: result/=Double.parseDouble(text); break; default: result=Double.parseDouble(text); } textComputer.setText(""); } if(keyButton==buttonEqual){ textComputer.setText(String.valueOf(result)); } } } publicstaticvoidmain(Stringargs[]){ newCalculator(); } }
希望本文所述对大家的java程序设计有所帮助。