利用java做一个简单的计算器

王朝java/jsp·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

共两个类。还只是完成+、-、×、÷运算而已。

GUI只是用了AWT,很简单,相信一看就能懂了。

Calculator.java

publicclassCalculator{

privateStringresult="0";

privateintop=0,add=1,sub=2,mul=3,div=4;

privatedoublestringToDouble(Stringx){

doubley=Double.parseDouble(x);

returny;

}

privatevoidoperate(Stringx){

doublex1=stringToDouble(x);

doubley=stringToDouble(result);

switch(op){

case0:

result=x;

break;

case1:

result=String.valueOf(y+x1);

break;

case2:

result=String.valueOf(y-x1);

break;

case3:

result=String.valueOf(y*x1);

break;

case4:

if(x1!=0){

result=String.valueOf(y/x1);

}else{

result="Thedivisorcan'tbezero!";

}

break;

}

}

publicStringopAdd(Stringx){

operate(x);

op=add;

returnresult;

}

publicStringopSubtract(Stringx){

operate(x);

op=sub;

returnresult;

}

publicStringopMultiply(Stringx){

operate(x);

op=mul;

returnresult;

}

publicStringopDivide(Stringx){

operate(x);

op=div;

returnresult;

}

publicStringopEquals(Stringx){

operate(x);

op=0;

returnresult;

}

publicvoidopClean(){

op=0;

result="0";

}

}

-------------------------------------------------------------------

第二个

CalculatorGUI.java

importjava.awt.*;

importjava.awt.event.*;

importjava.util.EventObject;

publicclassCalculatorGUI{

privateFramef;

privatePanelp1,p2;

privateButtonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9;

privateButtonbPoint,bAdd,bDec,bMul,bDiv,bCal;

privateTextFieldtf;

privateStrings,op;

privateCalculatorcal=newCalculator();

privatebooleanifOp;

publicCalculatorGUI(){

f=newFrame("Calculator");

p1=newPanel();

p2=newPanel();

b0=newButton("0");

b1=newButton("1");

b2=newButton("2");

b3=newButton("3");

b4=newButton("4");

b5=newButton("5");

b6=newButton("6");

b7=newButton("7");

b8=newButton("8");

b9=newButton("9");

bPoint=newButton(".");

bAdd=newButton("+");

bDec=newButton("-");

bMul=newButton("*");

bDiv=newButton("/");

bCal=newButton("=");

tf=newTextField(25);

tf.setEditable(false);

}

publicvoidlaunchFrame(){

f.setSize(220,160);

f.setResizable(false);

f.addWindowListener(newmyWindowListener());

p1.setLayout(newFlowLayout(FlowLayout.CENTER));

p1.add(tf);

f.add(p1,BorderLayout.NORTH);

p2.setLayout(newGridLayout(4,4));

b0.addActionListener(newsetLabelText_ActionListener());

b1.addActionListener(newsetLabelText_ActionListener());

b2.addActionListener(newsetLabelText_ActionListener());

b3.addActionListener(newsetLabelText_ActionListener());

b4.addActionListener(newsetLabelText_ActionListener());

b5.addActionListener(newsetLabelText_ActionListener());

b6.addActionListener(newsetLabelText_ActionListener());

b7.addActionListener(newsetLabelText_ActionListener());

b8.addActionListener(newsetLabelText_ActionListener());

b9.addActionListener(newsetLabelText_ActionListener());

bPoint.addActionListener(newsetLabelText_ActionListener());

bAdd.addActionListener(newsetOperator_ActionListener());

bDec.addActionListener(newsetOperator_ActionListener());

bMul.addActionListener(newsetOperator_ActionListener());

bDiv.addActionListener(newsetOperator_ActionListener());

bCal.addActionListener(newsetOperator_ActionListener());

p2.add(b7);

p2.add(b8);

p2.add(b9);

p2.add(bAdd);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(bDec);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(bMul);

p2.add(b0);

p2.add(bPoint);

p2.add(bCal);

p2.add(bDiv);

f.add(p2,BorderLayout.SOUTH);

f.setVisible(true);

}

publicvoidsetTextFieldText_Temp(){

if(tf.getText().length()<15&&(tf.getText().indexOf(".")==-1||!s.equals("."))){

tf.setText(tf.getText()+s);

}else{

tf.setText((tf.getText()+s).substring(0,15));

}

}

publicvoidsetTextFieldText(){

if(ifOp){

ifOp=false;

tf.setText("");

setTextFieldText_Temp();

}else{

setTextFieldText_Temp();

}

}

publicstaticvoidmain(String[]args){

CalculatorGUIcalculator=newCalculatorGUI();

calculator.launchFrame();

}

classmyWindowListenerextendsWindowAdapter{

publicvoidwindowClosing(WindowEvente){

System.exit(0);

}

}

classsetLabelText_ActionListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

ButtontempB=(Button)e.getSource();

s=tempB.getLabel();

setTextFieldText();

}

}

classsetOperator_ActionListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

ButtontempB=(Button)e.getSource();

op=tempB.getLabel();

if(op.equals("+")){

tf.setText(cal.opAdd(tf.getText()));

ifOp=true;

}elseif(op.equals("-")){

tf.setText(cal.opSubtract(tf.getText()));

ifOp=true;

}elseif(op.equals("*")){

tf.setText(cal.opMultiply(tf.getText()));

ifOp=true;

}elseif(op.equals("/")){

tf.setText(cal.opDivide(tf.getText()));

ifOp=true;

}elseif(op.equals("=")){

tf.setText(cal.opEquals(tf.getText()));

ifOp=true;

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航