WIN7计算器JAVA程序代码

  • 格式:pdf
  • 大小:79.82 KB
  • 文档页数:9
bMC.addActionListener(this); bMR.addActionListener(thisM.addActionListener(this);
p6.add(tf2); p3.add(p6); p3.add(bMC); p3.add(bMR); p3.add(bMS); p3.add(bM);
前输入值?
Frame f; Panel p1, p2, p3, p4, p5, p6; TextField tf1, tf2; Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0; Button bDiv, bSqrt, bMulti, bMinus, bPercent, bPlus, bReciprocal, bEqual,
bDiv.addActionListener(this); bSqrt.addActionListener(this); bMulti.addActionListener(this); bMinus.addActionListener(this); bPercent.addActionListener(this); bPlus.addActionListener(this); bReciprocal.addActionListener(this); bEqual.addActionListener(this); bDot.addActionListener(this); bNegative.addActionListener(this);
bBackspace = new Button("Backspace"); bCE = new Button("CE"); bC = new Button("C");
bBackspace.addActionListener(this);
bCE.addActionListener(this); bC.addActionListener(this);
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"); b0 = new Button("0");
f.setLayout(new BorderLayout(3, 3));
p1 = new Panel(new GridLayout(1, 3, 5, 5)); // 用于存放 backspace,ce,c 三键 p2 = new Panel(new GridLayout(4, 5, 5, 5)); // 用于存放数字区及附近共 20 键,
else { double temp = Double.parseDouble(e.getActionCommand()); for (int i = this.n; i < 0; i++) { temp *= 0.1; } this.dNowInput += temp; this.n--;
} this.tf1.setText(Double.toString(this.dNowInput)); } // key dot if (this.keyAvailable && e.getActionCommand() == ".") { if (this.alreadyHaveDot == false) {
p1.add(bBackspace); p1.add(bCE); p1.add(bC);
tf2 = new TextField(2); tf2.setEnabled(false); bMC = new Button("MC"); bMR = new Button("MR"); bMS = new Button("MS"); bM = new Button("M+");
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListener(this);
bDiv = new Button("/"); bSqrt = new Button("sqrt"); bMulti = new Button("*"); bMinus = new Button("-");
bPercent = new Button("%"); bPlus = new Button("+"); bReciprocal = new Button("1/x"); bEqual = new Button("="); bDot = new Button("."); bNegative = new Button("+/-");
package calculator;
import java.awt.*; import java.awt.event.*;
import javax.swing.JLabel; import javax.swing.JPanel;
public class calwin extends WindowAdapter implements ActionListener { private JLabel display; private JPanel numpanel,companel1,companel2; private double result; private String lastCommand; private boolean start; double dResult = 0; double dNowInput = 0; double dMemory; int n = 0; // 记载小数位数 int nOperation = 1; // 记录运算符类型 int nBitsNum = 0; // 记录总共输入的位数 boolean alreadyHaveDot = false; // 已经有小数点? boolean keyAvailable = true; boolean alreadyClickedEqueal = false; // 是否按下过"="? boolean isTempNowInput = false; // 是否在计算出结果后直接按运算符将结果赋给了当
tf1 = new TextField(35); // 存放显示区 tf1.setText("0."); tf1.setEditable(false); p5.add(tf1); f.add(p5, BorderLayout.NORTH); f.add(p4, BorderLayout.CENTER); f.add(p3, BorderLayout.WEST);
f.setVisible(true); f.addWindowListener(this);
}
public void actionPerformed(ActionEvent e) { // key 0 to 9 if (this.keyAvailable && e.getActionCommand().length() == 1 && e.getActionCommand().compareTo("0") >= 0 && e.getActionCommand().compareTo("9") <= 0) { if (this.isTempNowInput) { this.dNowInput = 0; this.isTempNowInput = false; } this.nBitsNum++; if (this.alreadyHaveDot == false) this.dNowInput = this.dNowInput * 10 + Double.parseDouble(e.getActionCommand());
this.nBitsNum++; this.alreadyHaveDot = true; this.n = -1; } } // key "+","-","*","/" if (this.keyAvailable && e.getActionCommand() == "+" || e.getActionCommand() == "-" || e.getActionCommand() == "*" || e.getActionCommand() == "/") { if (this.alreadyClickedEqueal) { this.dNowInput = this.dResult; this.isTempNowInput = true; } else { switch (this.nOperation) { case 1: