共两个类。还只是完成+、-、×、÷运算而已。
gui只是用了awt,很简单,相信一看就能懂了。
calculator.java
public class calculator{
private string result = "0";
private int op = 0,add = 1,sub = 2,mul = 3,div = 4;
private double stringtodouble(string x){
double y = double.parsedouble(x);
return y;
}
private void operate(string x){
double x1 = stringtodouble(x);
double y = stringtodouble(result);
switch (op){
case 0:
result = x;
break;
case 1:
result = string.valueof(y+x1);
break;
case 2:
result = string.valueof(y-x1);
break;
case 3:
result = string.valueof(y*x1);
break;
case 4:
if(x1!=0){
result = string.valueof(y/x1);
}else{
result = "the divisor can't be zero!";
}
break;
}
}
public string opadd(string x){
operate(x);
op = add;
return result;
}
public string opsubtract(string x){
operate(x);
op = sub;
return result;
}
public string opmultiply(string x){
operate(x);
op = mul;
return result;
}
public string opdivide(string x){
operate(x);
op = div;
return result;
}
public string opequals(string x){
operate(x);
op = 0;
return result;
}
public void opclean(){
op = 0;
result = "0";
}
}
-------------------------------------------------------------------
第二个
calculatorgui.java
import java.awt.*;
import java.awt.event.*;
import java.util.eventobject;
public class calculatorgui{
private frame f;
private panel p1,p2;
private button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private button bpoint,badd,bdec,bmul,bdiv,bcal;
private textfield tf;
private string s,op;
private calculator cal = new calculator();
private boolean ifop;
public calculatorgui(){
f = new frame("calculator");
p1 = new panel();
p2 = new panel();
b0 = new button("0");
b1 = new button("1");
b2 = new button("2");
b3 = new button("3");
b4 = new button("4");
b5 = new button("5");
b6 = new button("6");
b7 = new button("7");
b8 = new button("8");
b9 = new button("9");
bpoint = new button(".");
badd = new button("+");
bdec = new button("-");
bmul = new button("*");
bdiv = new button("/");
bcal = new button("=");
tf = new textfield(25);
tf.seteditable(false);
}
public void launchframe(){
f.setsize(220,160);
f.setresizable(false);
f.addwindowlistener(new mywindowlistener());
p1.setlayout(new flowlayout(flowlayout.center));
p1.add(tf);
f.add(p1,borderlayout.north);
p2.setlayout(new gridlayout(4,4));
b0.addactionlistener(new setlabeltext_actionlistener());
b1.addactionlistener(new setlabeltext_actionlistener());
b2.addactionlistener(new setlabeltext_actionlistener());
b3.addactionlistener(new setlabeltext_actionlistener());
b4.addactionlistener(new setlabeltext_actionlistener());
b5.addactionlistener(new setlabeltext_actionlistener());
b6.addactionlistener(new setlabeltext_actionlistener());
b7.addactionlistener(new setlabeltext_actionlistener());
b8.addactionlistener(new setlabeltext_actionlistener());
b9.addactionlistener(new setlabeltext_actionlistener());
bpoint.addactionlistener(new setlabeltext_actionlistener());
badd.addactionlistener(new setoperator_actionlistener());
bdec.addactionlistener(new setoperator_actionlistener());
bmul.addactionlistener(new setoperator_actionlistener());
bdiv.addactionlistener(new setoperator_actionlistener());
bcal.addactionlistener(new setoperator_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);
}
public void settextfieldtext_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));
}
}
public void settextfieldtext(){
if(ifop){
ifop = false;
tf.settext("");
settextfieldtext_temp();
}else{
settextfieldtext_temp();
}
}
public static void main(string[] args){
calculatorgui calculator = new calculatorgui();
calculator.launchframe();
}
class mywindowlistener extends windowadapter{
public void windowclosing(windowevent e){
system.exit(0);
}
}
class setlabeltext_actionlistener implements actionlistener{
public void actionperformed(actionevent e){
button tempb = (button)e.getsource();
s = tempb.getlabel();
settextfieldtext();
}
}
class setoperator_actionlistener implements actionlistener{
public void actionperformed(actionevent e){
button tempb = (button)e.getsource();
op = tempb.getlabel();
if(op.equals("+")){
tf.settext(cal.opadd(tf.gettext()));
ifop = true;
}else if(op.equals("-")){
tf.settext(cal.opsubtract(tf.gettext()));
ifop = true;
}else if(op.equals("*")){
tf.settext(cal.opmultiply(tf.gettext()));
ifop = true;
}else if(op.equals("/")){
tf.settext(cal.opdivide(tf.gettext()));
ifop = true;
}else if(op.equals("=")){
tf.settext(cal.opequals(tf.gettext()));
ifop = true;
}
}
}
}