[分享]计算器原代码
import lundin.SymbolicMath.Eval;
import lundin.SymbolicMath.Derive;
import java.applet.Applet;
import java.util.*;
import java.awt.*;
public class Calculator extends Applet{
Panel p = null;
Panel p1 = null;
Panel p2 = null;
Panel p3 = null;
Label lbl = null;
Eval ev = null; //
Derive de = null; //??等号
TextField tf = null;
Button info = null;
Button clear = null;
Button equa = null;
Button plus = null;
Button minus = null;
Button divide = null;
Button mult = null;
Button zerow,one,two,three,four,five,six,seven,eight,nine,point;
Hashtable values = null;
Font fo = null;
FontMetrics fm = null;
public void init()
{
int color = 0x999966;
GridBagLayout gridb = new GridBagLayout();
GridBagLayout gridb2 = new GridBagLayout();
GridBagLayout gridb3 = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
String temp = "";
ev = new Eval();
de = new Derive();
values = new Hashtable();
fo = new Font("Times Roman", Font.PLAIN, 9 );
this.setFont( fo );
fm = this.getFontMetrics( fo );
p = new Panel();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
tf = new TextField();
info = new Button( "Info" );
clear = new Button( "Clear" );
plus = new Button( "+" );
minus = new Button( "-" );
mult = new Button( "*" );
divide = new Button( "/" );
equa = new Button( "=" );
point = new Button(".");
zerow = new Button("0");
one = new Button("1");
two = new Button("2");
three = new Button("3");
four = new Button("4");
five = new Button("5");
six = new Button("6");
seven = new Button("7");
eight = new Button("8");
nine = new Button("9");
this.setBackground( new Color(color) ); //设置背景颜色
p2.setLayout( gridb );
p2.setBackground( new Color(color) );
p2.setForeground( Color.black );
p3.setBackground( new Color(color) );
p3.setForeground( Color.black );
tf.setBackground( Color.white );
tf.setForeground( Color.black );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 0;
constr.ipadx = 3;
constr.ipady = 3;
gridb.setConstraints( seven , constr );
p2.add( seven);
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 1;
constr.gridy = 0;
gridb.setConstraints( eight , constr );
p2.add( eight );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 2;
constr.gridy = 0;
gridb.setConstraints( nine , constr );
p2.add( nine );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 3;
constr.gridy = 0;
gridb.setConstraints( plus , constr );
p2.add( plus );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 1;
gridb.setConstraints( four , constr );
p2.add( four );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 1;
constr.gridy = 1;
gridb.setConstraints( five , constr );
p2.add( five );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 2;
constr.gridy = 1;
gridb.setConstraints( six , constr );
p2.add( six );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 3;
constr.gridy = 1;
gridb.setConstraints( minus , constr );
p2.add( minus );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 2;
gridb.setConstraints( one , constr );
p2.add( one );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 1;
constr.gridy = 2;
gridb.setConstraints( two , constr );
p2.add( two );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 2;
constr.gridy = 2;
gridb.setConstraints( three , constr );
p2.add( three );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 3;
constr.gridy = 2;
gridb.setConstraints( mult , constr );
p2.add( mult );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 3;
gridb.setConstraints( equa , constr );
p2.add( equa );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 1;
constr.gridy = 3;
gridb.setConstraints( zerow , constr );
p2.add( zerow );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 2;
constr.gridy = 3;
gridb.setConstraints( point , constr );
p2.add( point );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 3;
constr.gridy = 3;
gridb.setConstraints( divide , constr );
p2.add( divide );
p1.setLayout( gridb3 );
p1.setBackground( new Color(color) );
p1.setForeground( Color.black );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 0;
gridb3.setConstraints( clear , constr );
p1.add( clear );
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 1;
constr.gridy = 0;
gridb3.setConstraints( info , constr );
p1.add( info );
p.setLayout( gridb2 );
p.setBackground( new Color(color) );
p.setForeground( Color.black );
constr.fill = GridBagConstraints.HORIZONTAL;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 0;
gridb2.setConstraints( tf , constr );
p.add( tf );
constr.fill = GridBagConstraints.HORIZONTAL;
constr.anchor = GridBagConstraints.CENTER;
constr.gridx = 0;
constr.gridy = 1;
gridb2.setConstraints( p2 , constr );