看看下面的代码,不过你计算器还不能正常工作,继续加油吧!
package cjsdn;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CalculatorApplet extends Applet implements ActionListener {
private Color background;
private Label result;
private Button c_key, eq, div, times, d7, d8, d9, minus, d4, d5, d6, plus, d1,
d2, d3, eq2, d0, dp;
private Panel row1, row2, row3, p12, p120, p3p, p3peq;
private String lastOptr;
private String Filler = " ";
private boolean sawDecimal, newOpd, sawAnOpd;
private double opd1;
private int appletWidth, appletHeight;
static final String initialString = "0.00000";
static final String digits = "0123456789";
static final String operators = "+-*/=";
private Button makeButton(String label, Color color, Font font) {
Button b = new Button(label);
b.setBackground(color);
b.setFont(font);
return b;
}
private Panel makePanel(LayoutManager lm, Color color) {
Panel p = new Panel();
p.setLayout(lm);
p.setBackground(color);
return p;
}
private void makeButton() {
Font font = new Font("Courier", Font.BOLD, 10);
Color lightRed = new Color(255, 100, 100);
Color lightBlue = new Color(100, 100, 255);
Color yellow = new Color(255, 255, 0);
c_key = makeButton("C", lightRed, font);
eq = makeButton("=", lightBlue, font);
div = makeButton("/", lightBlue, font);
times = makeButton("*", lightBlue, font);
d7 = makeButton("7", yellow, font);
d8 = makeButton("8", yellow, font);
d9 = makeButton("9", yellow, font);
minus = makeButton("-", lightBlue, font);
d4 = makeButton("4", yellow, font);
d5 = makeButton("5", yellow, font);
d6 = makeButton("6", yellow, font);
plus = makeButton("+", lightBlue, font);
d1 = makeButton("1", yellow, font);
d2 = makeButton("2", yellow, font);
d3 = makeButton("3", yellow, font);
d0 = makeButton("0", yellow, font);
dp = makeButton(".", yellow, font);
eq2 = makeButton("=", lightRed, font);
}
public void init() {
background = new Color(200, 255, 255);
this.setLayout(new FlowLayout(FlowLayout.CENTER, 4, 1));
result = new Label(initialString, Label.RIGHT);
result.setBackground(new Color(255, 255, 255));
add(result);
makeButton();
row1 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), background);
row1.add(c_key);
row1.add(eq);
row1.add(div);
row1.add(times);
row2 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), background);
row2.add(d7);
row2.add(d8);
row2.add(d9);
row2.add(minus);
row3 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), background);
row3.add(d4);
row3.add(d5);
row3.add(d6);
row3.add(plus);
add(row1);
add(row2);
add(row3);
p12 = makePanel(new BorderLayout(2, 2), background);
p12.add("West", d1);
p12.add("East", d2);
p120 = makePanel(new BorderLayout(2, 2), background);
p120.add("North", p12);
p120.add("South", d0);
p3p = makePanel(new BorderLayout(2, 2), background);
p3p.add("North", d3);
p3p.add("South", dp);
p3peq = makePanel(new BorderLayout(2, 2), background);
p3peq.add("West", p3p);
p3peq.add("East", eq2);
add(p120);
add(p3peq);
setBackground(background);
c_key.addActionListener(this);
eq.addActionListener(this);
eq2.addActionListener(this);
dp.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
times.addActionListener(this);
div.addActionListener(this);
d1.addActionListener(this);
d2.addActionListener(this);
d3.addActionListener(this);
d4.addActionListener(this);
d5.addActionListener(this);
d6.addActionListener(this);
d7.addActionListener(this);
d8.addActionListener(this);
d9.addActionListener(this);
d0.addActionListener(this);
resize(100,168);
clearCalc();
}
public void paint(Graphics g) {
validate();
}
public void actionPerformed(ActionEvent e) {
String str = (String) e.getActionCommand();
if (digits.indexOf(str) != -1) {
handleDigit(str);
} else if (str.equals(".")) {
handleDecimal();
} else if (operators.indexOf(str) != -1) {
handleOperator(str);
} else if (str.equals("C")) {
handleClear();
}
}
private void handleDigit(String str) {
if (newOpd) {
result.setText(str);
newOpd = false;
} else {
result.setText(result.getText() + str);
sawAnOpd = true;
}
}
private void handleClear() {
clearCalc();
}
private void clearCalc() {
sawDecimal = false;
newOpd = true;
sawAnOpd = false;
lastOptr = "";
//result.setText(initialString);
}
private void handleOperator(String s) {
if (!sawAnOpd) {
return;
}
if (lastOptr.equals("+")) {
opd1 += getDisplay();
} else if (lastOptr.equals("-")) {
opd1 -= getDisplay();
} else if (lastOptr.equals("*")) {
opd1 *= getDisplay();
} else if (lastOptr.equals("/")) {
opd1 /= getDisplay();
}
result.setText(opd1 + "");
lastOptr = s;
sawDecimal = false;
newOpd = true;
}
private void handleDecimal() {
if (newOpd) {
result.setText("0.");
newOpd = false;
} else {
result.setText(result.getText() + ".");
sawDecimal = true;
}
//result.setText(result.getText()+".");
}
private double getDisplay() {
return Double.parseDouble(result.getText());
}
}