精品主页 | 软件下载 | 系统下载 | 精品导航| 精彩图片 | 转帖工具 | 版主申请 | 影视下载
发新话题
打印

开发实例:利用java做一个简单的计算器

开发实例:利用java做一个简单的计算器

  


                  两个类。还只是完成+、-、×、÷运算而已。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;

}

}

}

}

TOP

发新话题