1 packagecom.lovo.calculator;2
3 importjava.awt.BorderLayout;4 importjava.awt.GridLayout;5 importjava.awt.event.ActionEvent;6 importjava.awt.event.ActionListener;7 importjava.awt.event.KeyAdapter;8 importjava.awt.event.KeyEvent;9
10 importjavax.swing.JButton;11 importjavax.swing.JFrame;12 importjavax.swing.JPanel;13 importjavax.swing.JTextField;14
15 /**
16 * 计算器类17 *18 *@authorAdministrator19 *20 */
21 public class Calculator extends JFrame implementsActionListener {22
23 private static final long serialVersionUID = 3868243398506940702L;24
25 //文本框
26 privateJTextField result;27 //按钮数组
28 privateJButton[] buttons;29 //按钮文本
30 private final String[] characters = { "7", "8", "9", "/", "4", "5", "6",31 "*", "1", "2", "3", "-", "0", ".", "=", "+"};32 //是否为第一个输入的数字
33 private boolean isFirstDigit = true;34 //运算结果
35 private double resultNum = 0.0;36 //运算符
37 private String operator = "=";38
39 publicCalculator(String title) {40 //设置标题栏
41 super(title);42 //初始化各组件
43 init();44 //注册各组件监听器
45 registerListener();46
47 //显示窗体
48 setVisible(true);49 }50
51 /**
52 * 初始化各组件53 */
54 private voidinit() {55 //常用属性初始化
56 setSize(220, 200);57 setResizable(false);58 setLocationRelativeTo(null);59 setDefaultCloseOperation(EXIT_ON_CLOSE);60
61 /*文本框对象初始化*/
62 result = new JTextField("0");63 //文本右对齐
64 result.setHorizontalAlignment(JTextField.RIGHT);65 //设置是否可编辑
66 result.setEditable(false);67 /*按钮初始化*/
68 buttons = newJButton[characters.length];69 for (int i = 0; i < buttons.length; i++) {70 buttons[i] = newJButton(characters[i]);71 buttons[i].setFocusable(false); //不允许按钮定位焦点
72 }73 /*将文本框与按钮添加到窗体中*/
74 add(result, BorderLayout.NORTH);75 JPanel pnl = new JPanel(new GridLayout(4, 4, 5, 5));76 for(JButton jButton : buttons) {77 pnl.add(jButton);78 }79 add(pnl);80
81 this.getContentPane().setFocusable(true);82 }83
84 /**
85 * 注册监听器86 */
87 private voidregisterListener() {88 for(JButton jButton : buttons) {89 jButton.addActionListener(this);90 }91
92 //注册键盘事件
93 this.getContentPane().addKeyListener(newKeyAdapter() {94 @Override95 public voidkeyPressed(KeyEvent e) {96 String text =String.valueOf(e.getKeyChar());97 if (Character.isDigit(text.charAt(0)) || ".".equals(text)) { //数字或小数点
98 handleNumber(text);99 } else if ("+-*/=".indexOf(text) != -1) { //运算符
100 handleOperator(text);101 } else if (e.getKeyCode() == 8) { //退格键
102 String tmp =result.getText().trim();103 if (tmp.length() == 1) {104 result.setText("0");105 isFirstDigit = true;106 } else{107 result.setText(tmp.substring(0, tmp.length() - 1));108 }109 }110 }111 });112 }113
114 @Override115 public voidactionPerformed(ActionEvent e) {116 JButton btn =(JButton) e.getSource();117 String text =btn.getText().trim();118
119 if (Character.isDigit(text.charAt(0)) || ".".equals(text)) { //处理数字和小数点
120 handleNumber(text);121 } else { //处理运算符
122 handleOperator(text);123 }124 }125
126 /**
127 * 处理数字和小数点128 *129 *@paramtext130 */
131 private voidhandleNumber(String text) {132 if (isFirstDigit) { //第一次输入
133 if (".".equals(text)) {134 this.result.setText("0.");135 } else{136 this.result.setText(text);137 }138 } else if ("0".equals(text) && "0".equals(this.result.getText())) {139 isFirstDigit = true;140 return;141 } else if (".".equals(text) && this.result.getText().indexOf(".") == -1) {142 this.result.setText(this.result.getText() + ".");143 } else if (!".".equals(text)) {144 this.result.setText(this.result.getText() +text);145 }146
147 isFirstDigit = false;148 }149
150 /**
151 * 处理运算符152 *153 *@paramtext154 */
155 private voidhandleOperator(String text) {156 switch (operator) { //处理各项运算 适用于JDK1.7版本的
157 case "+":158 resultNum += Double.parseDouble(this.result.getText());159 break;160 case "-":161 resultNum -= Double.parseDouble(this.result.getText());162 break;163 case "*":164 resultNum *= Double.parseDouble(this.result.getText());165 break;166 case "/":167 resultNum /= Double.parseDouble(this.result.getText());168 break;169 case "=":170 resultNum = Double.parseDouble(this.result.getText());171 break;172 }173 //将文本框的值修改为运算结果
174 this.result.setText(String.valueOf(resultNum));175 //将点击的运算符放入operator保存
176 operator =text;177 //下一个数字第一次点击
178 isFirstDigit = true;179 }180 }