参考:http://yuncode.net/code/c_50b0c000f0c2e62
作者:程序猿style
界面.png
源码
package top.warmj.calculator;
import java.awt.*;
import java.awt.event.*;
public class Calc extends Frame implements ActionListener {
Font font = new Font("宋体", Font.PLAIN, 24); // 全局字体,字体无效
private Button b_7;
private Button b_8;
private Button b_9;
private Button b_divi; // 除号
private Button b_4;
private Button b_5;
private Button b_6;
private Button b_multi; // 乘号
private Button b_1;
private Button b_2;
private Button b_3;
private Button b_sub; // 减号
private Button b_dot; // 小数点
private Button b_0;
private Button b_equal; // 等于号
private Button b_plus; // 加号
private TextField tf_result; // 结果显示框
private Button b_clear;
private double resultNum = 0.0;
private Button b_backspace;
private Boolean isFirstNum = true; // 是否第一个数字,或按下运算按钮后的第一个数字
private boolean isValid = true; // 操作是否合法
private String operator = "="; // 当前运算的运算符
private Button[] numButtonArrs; // 数字按钮数组
private Button[] opeButtonArrs; // 运算符等按钮数组
public static void main(String[] args) {
new Calc("Simple Calculator —— WarMj");
}
// 程序功能
public Calc() {
init();
}
public Calc(String name) {
super(name);
init();
}
// 初始化
public void init() {
this.setSize(500, 270);
this.setLocation(750, 400);
this.setIconImage(Toolkit.getDefaultToolkit().createImage("calc.png"));
this.setVisible(true);
this.setResizable(false);
northPanel();
centerPanel();
event();
}
// 按键点击动作监听
public void actionPerformed(ActionEvent e) {
// 获取事件源的标签
String label = e.getActionCommand();
if (label.equals(opeButtonArrs[5].getLabel())) {
// 点击"Backspace"键
handleBackspace();
} else if (label.equals(opeButtonArrs[6].getLabel())) {
// 点击"CE"键
tf_result.setText("");
} else if ("0123456789.".indexOf(label) >= 0) {
// 点击数字键或者小数点键
handleNumber(label);
} else {
// 用户点击运算符键
handleOperator(label);
}
}
// 运算符键功能
private void handleOperator(String key) {
if (operator.equals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0) {
// 操作不合法
isValid = false;
tf_result.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")) {
// 加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")) {
// 减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")) {
// 乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("=")) {
// 赋值运算
resultNum = getNumberFromText();
}
if (isValid) {
// 双精度浮点数的运算
long t1; // 存放长整数
double t2; // 判断差值
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
// 没有差值,说明是值是整数,这样没有小数点
tf_result.setText(String.valueOf(t1));
} else {
// 有差值,说明是浮点数
tf_result.setText(String.valueOf(resultNum));
}
}
// 运算符等于用户按的按钮
operator = key;
isFirstNum = true;
isValid = true;
}
// 数字键或小数点键功能
private void handleNumber(String key) {
if (isFirstNum) {
// 如果输入的是第一个数字
tf_result.setText(key);
} else if (key.equals(".") && (tf_result.getText().indexOf(".") < 0)) {
tf_result.setText(tf_result.getText() + ".");
} else if (!key.equals(".")) {
tf_result.setText(tf_result.getText() + key);
}
// 以后输入的就不是第一个数字了
isFirstNum = false;
}
// 退格键功能
private void handleBackspace() {
if (tf_result.getText().equals("")) {
isFirstNum = true;
resultNum = 0.0;
operator = "=";
} else {
String str = tf_result.getText();
tf_result.setText(str.substring(0, str.length() - 1));
}
}
// 事件
public void event() {
// 创建键盘监听对象
KeyAdapter keyadapter = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() > 47 && e.getKeyCode() < 58) {
handleNumber(e.getKeyText(e.getKeyCode()));
} else if (e.getKeyCode() > 95 && e.getKeyCode() < 106) {
handleNumber(e.getKeyText(e.getKeyCode()).substring(7, 8));
}
switch (e.getKeyCode()) {
case 106: // 乘号 *
handleOperator("*");
break;
case 61:
case 107: // 加号 +
handleOperator("+");
break;
case 45:
case 109: // 减号 -
handleOperator("-");
break;
case 111: // 除号 /
handleOperator("/");
break;
case 10: // 回车 Enter
handleOperator("=");
break;
case 8: // 退格 Backpace
handleBackspace();
break;
case 46:
case 110: // 小数点 .
handleNumber(".");
break;
case 27:
tf_result.setText("");
break;
default:
break;
}
}
};
// 添加键盘监听事件
tf_result.addKeyListener(keyadapter);
for (int i = 0; i < numButtonArrs.length; i++) {
// 给按钮添加监听事件,由于本类已经实现 ActionListener 接口,所以直接用 this
numButtonArrs[i].addActionListener(this); // 点击
numButtonArrs[i].addKeyListener(keyadapter); // 按钮
}
for (int i = 0; i < opeButtonArrs.length; i++) {
opeButtonArrs[i].addActionListener(this); // 点击
opeButtonArrs[i].addKeyListener(keyadapter); // 按钮
}
// 关闭窗口
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 北布局,计算结果和清零按钮
public void northPanel() {
Panel north = new Panel();
// 结果框
tf_result = new TextField();
tf_result.setEditable(false);
tf_result.setBackground(Color.WHITE);
tf_result.setColumns(25);
tf_result.setFont(font);
tf_result.setText("");
// 退格按钮
b_backspace = new Button("←");
b_backspace.setPreferredSize(new Dimension(50, 35));
b_backspace.setFont(font);
// 清零按钮
b_clear = new Button("CE");
b_clear.setPreferredSize(new Dimension(50, 35));
b_clear.setFont(font);
north.add(tf_result);
north.add(b_backspace);
north.add(b_clear);
this.add(north, BorderLayout.NORTH);
}
// 中间布局,其他运算按钮
public void centerPanel() {
Panel center = new Panel();
b_7 = getButton("7");
b_8 = getButton("8");
b_9 = getButton("9");
b_divi = getButton("/");
b_4 = getButton("4");
b_5 = getButton("5");
b_6 = getButton("6");
b_multi = getButton("*");
b_1 = getButton("1");
b_2 = getButton("2");
b_3 = getButton("3");
b_sub = getButton("-");
b_dot = getButton(".");
b_0 = getButton("0");
b_equal = getButton("=");
b_plus = getButton("+");
// 数字和小数点按钮数组
numButtonArrs = new Button[] { b_0, b_1, b_2, b_3, b_4, b_5, b_6, b_7, b_8, b_9, b_dot };
// 运算符按钮数组
opeButtonArrs = new Button[] { b_plus, b_sub, b_multi, b_divi, b_equal, b_backspace, b_clear };
center.add(b_7);
center.add(b_8);
center.add(b_9);
center.add(b_divi);
center.add(b_4);
center.add(b_5);
center.add(b_6);
center.add(b_multi);
center.add(b_1);
center.add(b_2);
center.add(b_3);
center.add(b_sub);
center.add(b_dot);
center.add(b_0);
center.add(b_equal);
center.add(b_plus);
center.setVisible(true);
this.add(center, BorderLayout.CENTER);
}
// 获取按钮功能
public Button getButton(String name) {
Button button = new Button(name);
button.setPreferredSize(new Dimension(115, 40));
button.setFont(font);
return button;
}
// 获取当前数字
private double getNumberFromText() {
double result = 0.0;
try {
result = Double.valueOf(tf_result.getText()).doubleValue();
} catch (NumberFormatException e) {
}
return result;
}
}
calc.png
网友评论