当前位置: 首页 > >

Java语言编写的标准计算器(实现了windows标准型计算器的全部功能)

发布时间:

标准型计算器(Java 语言编写) 下面的 java 源码是本人连续工作 48 小时的成果,也许对于高手 来说这只是小菜一碟,但是如果你不是高手,我推荐你研究下面的 程序。程序中包含了 Java 反射机制、图形界面编程等一系列的知识 运用。研究完后你不一定会变成高手,但对于 Java 和 C 语言你都会 有更深度的理解。源码中的注释很详细。
1 ○建立 java 工程

2 ○输入工程名

3 ○新建一个包

4 ○输入包名

5 ○新建一个类

6 ○输入类名

以上步骤都是基础,会的可以略过。
源码:

package three.num1; import java.awt.*; import java.awt.event.ActionEvent;

import java.awt.event.ActionListener; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private JTextField text1;// 未实例化 private JLabel lable_text; private JButton bt_backspace, bt_ce, bt_c; private JPanel gridpanel; private String str = ""; private String str_note = " "; private char operator = '\0'; private double data_before; private boolean equal_flag = false; public Calculator() { super("计算器");// 标题栏 setBounds(100, 100, 198, 244); // 程序在屏幕上的显示位置(100,100), 宽*高=300*233 this.setResizable(false); this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT)); lable_text = new JLabel(" ", JLabel.RIGHT); this.getContentPane().add(lable_text); text1 = new JTextField("0", 16); // 将文本框text1实例化,并设置 文本框的长度 text1.setHorizontalAlignment(JTextField.RIGHT); // 文字在文本框 中从右边开始显示 text1.setEditable(false); // 不可编辑 this.getContentPane().add(text1); bt_backspace = new JButton("backspace"); bt_backspace.setMargin(new Insets(2, 5, 2, 6)); bt_ce = new JButton("CE"); // bt_ce.setMargin(new Insets(2,2,2,2)); bt_c = new JButton("C"); // bt_c.setMargin(new Insets(2,2,2,2)); this.getContentPane().add(bt_backspace); this.getContentPane().add(bt_ce); this.getContentPane().add(bt_c); bt_backspace.addActionListener(this);

bt_ce.addActionListener(this); bt_c.addActionListener(this); keyboard(); // 调用 keyboard() this.setVisible(true); } /*** 添加按钮 ***/ public void keyboard() { String str[] = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/X", "0", "+/-", ".", "+", "=" }; gridpanel = new JPanel(); gridpanel.setLayout(new GridLayout(4, 5, 9, 8));// 4行5列,水* 间距9,垂直间距7 this.getContentPane().add(gridpanel); JButton button[] = new JButton[str.length]; for (int i = 0; i < button.length; i++) { button[i] = new JButton(str[i]); button[i].setMargin(new Insets(0, 0, 0, 0)); button[i].setForeground(new Color(252, 51, 51));// button[i].setBackground(new // Color(131,95,56)); gridpanel.add(button[i]); button[i].addActionListener(this); // 为按钮注册单击事件监听 器 } } /*** 实现监听器接口 ***/ public void actionPerformed(ActionEvent e) { double data_after; String bt = e.getActionCommand(); /*** 按0数字键 */ if (bt == "0")// bt.equals("0") { if (equal_flag == false) { if (text1.getText().equals("0")) { return; } else { str += bt; text1.setText(str); }

} else { equal_flag = false; // 添加这行代码使下次按数字键从if行执行, 否则每次按0键都执行本行else语句,即str清空。 str = ""; str += bt; text1.setText(str); } } /*** 按非零数字键 */ if (bt.charAt(0) > '0' && bt.charAt(0) <= '9' && bt != "1/X")// bt是字符串, 当它是数字键时, 它只有一个字符。 故用bt.charAt(0)来得到这个字符。 { if (equal_flag == false) { if (text1.getText().equals("0")) { str = ""; // 清零之后的str="0",文本框显示0,再按数字键应 该先将str清空,否则会显示023.9,012等带零数。 str += bt; text1.setText(str); } else { str += bt; text1.setText(str); } } else { // "="键按下,equal_flag=true equal_flag = false; // 添加这行代码使下次按数字键从 if(equal_flag == // false)行执行,否则每次按数字键都执行本行 else语句,即str和str_note清空,导致只显示一位数。 str = ""; str += bt; text1.setText(str); } // Calculator.this.setVisible(false);//隐藏之 } /** 按符号键及功能键 */ switch (bt) { case ".": if (equal_flag == true) { str = "0"; str += bt; text1.setText(str); equal_flag = false; } if (text1.getText().contains(".") == true)// 或 text1.getText().indexOf(".")>=0,表示不能连续输入‘.’。

{ return; } else { if (text1.getText().equals("0") || text1.getText().length() == 0) {// 文本框是0或 空时,按.键显示0.。 str = "0"; str += bt; text1.setText(str); } else { str += bt; text1.setText(str); } } equal_flag = false;// 按完?.?键后,按数字键会在.后添加该数字。参见 102行;另外此行代码还处理了按?=?键后再按?.?键的显示问题。 break; case "+": // if(operator!='\0')//刚刚按了符号键,就不能再次处理了,直接 退出函数。 // return; if (operator != '\0' && text1.getText().length() != 0) { if(str.charAt(str.length()-1)=='.') str_note =str_note+str.substring(0,str.length()-1)+bt; else str_note =str_note+ str + bt; lable_text.setText(str_note); switch (operator) { case '+': data_before += Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '-': data_before -= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '*': data_before *= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break;

case '/': data_before /= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; default: break; } equal_flag = true;// 这行代码是为了按新的数字键时,先清空str 和text1,再显示新的数字键值。 operator = '+';// 将当前的(最新的)运算符给operator,以使下 次运算匹配正确的运算符。 } else { operator = '+'; data_before = Double.parseDouble(text1.getText()); if(text1.getText().charAt(text1.getText().length()-1)=='.')//去. str_note =text1.getText().substring(0,str.length()-1)+bt; else str_note = text1.getText() + bt; lable_text.setText(str_note); str = ""; text1.setText(str); } break; case "-": if (operator != '\0' && text1.getText().length() != 0) { if(str.charAt(str.length()-1)=='.') str_note =str_note+str.substring(0,str.length()-1)+bt; else str_note =str_note+ str + bt; lable_text.setText(str_note); switch (operator) { case '+': data_before += Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '-': data_before -= Double.parseDouble(str);

str = Double.toString(data_before); text1.setText(str); break; case '*': data_before *= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '/': data_before /= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; default: break; } equal_flag = true;// 这行代码是为了按新的数字键时,先清空str和 text1,再显示新的数字键值。 operator = '-';// 将当前的(最新的)运算符给operator,以使下 次运算匹配正确的运算符。 } else { operator = '-'; data_before = Double.parseDouble(text1.getText()); if(text1.getText().charAt(text1.getText().length()-1)=='.')//去. str_note =text1.getText().substring(0,str.length()-1)+bt; else str_note = text1.getText() + bt; lable_text.setText(str_note); str = ""; text1.setText(str); } break; case "*": if (operator != '\0' && text1.getText().length() != 0) { if(str.charAt(str.length()-1)=='.') str_note =str_note+str.substring(0,str.length()-1)+bt; else str_note =str_note+ str + bt; lable_text.setText(str_note); switch (operator) {

case '+': data_before += Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '-': data_before -= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '*': data_before *= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '/': data_before /= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; default: break; } equal_flag = true;// 这行代码是为了按新的数字键时,先清空str 和text1,再显示新的数字键值。 operator = '*';// 将当前的(最新的)运算符给operator,以使下 次运算匹配正确的运算符。 } else { operator = '*'; data_before = Double.parseDouble(text1.getText()); if(text1.getText().charAt(text1.getText().length()-1)=='.')//去. str_note =text1.getText().substring(0,str.length()-1)+bt; else str_note = text1.getText() + bt; lable_text.setText(str_note); str = ""; text1.setText(str); } break; case "/": if (operator != '\0' && text1.getText().length() != 0) {

if(str.charAt(str.length()-1)=='.') str_note =str_note+str.substring(0,str.length()-1)+bt; else str_note =str_note+ str + bt; lable_text.setText(str_note); switch (operator) { case '+': data_before += Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '-': data_before -= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '*': data_before *= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; case '/': data_before /= Double.parseDouble(str); str = Double.toString(data_before); text1.setText(str); break; default: break; } equal_flag = true;// 这行代码是为了按新的数字键时,先清空str 和text1,再显示新的数字键值。 operator = '/';// 将当前的(最新的)运算符给operator,以使下 次运算匹配正确的运算符。 } else { operator = '/'; data_before = Double.parseDouble(text1.getText()); if(text1.getText().charAt(text1.getText().length()-1)=='.')//去. str_note =text1.getText().substring(0,str.length()-1)+bt; else str_note = text1.getText() + bt; lable_text.setText(str_note);

str = ""; text1.setText(str); } break; case "+/-": try { if (Double.parseDouble(str) > 0) { str = "-" + str; text1.setText(str); } else { if (Double.parseDouble(str) == 0) { return; } else { str = str.substring(1, str.length()); text1.setText(str); } } } catch (NumberFormatException e1) { return; } break; case "sqrt": equal_flag = true;// 这行代码是为了按新的数字键时,先清空str和 text1,再显示新的数字键值。 try { data_before = Double.parseDouble(str); // str_note ="sqrt("+str+")";//不会产生异常的语句不要放这 里,否则会跳过该语句 } catch (NumberFormatException e2) { // e2.printStackTrace(); // System.err.println("空字符串无法开*方!"); str = "0"; } if (str.charAt(str.length() - 1) == '.') {// 若0.、2.、23., 等类似"xx."的数开方,label_text应显示sqrt(xx),而不是sqrt(xx.)。 str_note = "sqrt(" + str.substring(0,str.length()-1) + ")"; } else { str_note = "sqrt(" + str + ")"; } str = Double.toString(Math.sqrt(data_before)); text1.setText(str); lable_text.setText(str_note); break;

case "%": equal_flag = true; switch (operator) { case '+': str_note = str_note + Double.toString(Double.parseDouble(str) lable_text.setText(str_note); str = Double.toString(data_before + Double.parseDouble(str) / 100); text1.setText(str); break; case '-': str_note = str_note + Double.toString(Double.parseDouble(str) lable_text.setText(str_note); str = Double.toString(data_before Double.parseDouble(str) / 100); text1.setText(str); break; case '*': str_note = str_note + Double.toString(Double.parseDouble(str) lable_text.setText(str_note); str = Double.toString(data_before * Double.parseDouble(str) / 100); text1.setText(str); break; case '/': str_note = str_note + Double.toString(Double.parseDouble(str) lable_text.setText(str_note); str = Double.toString(data_before / (Double.parseDouble(str) / 100)); text1.setText(str); break; default: str_note = str + "%"; lable_text.setText(str_note); str = Double.toString(Double.parseDouble(str) / text1.setText(str); break;

/ 100);

/ 100);

/ 100);

/ 100);

100);

} operator = '\0'; break; case "1/X": equal_flag = true; data_before = Double.parseDouble(str); str_note = "reciproc(" + str + ")"; str = Double.toString(1 / data_before); text1.setText(str); lable_text.setText(str_note); break; case "C": operator = '\0'; str = ""; str_note = " "; data_before = 0; data_after = 0; equal_flag = false; text1.setText("0"); lable_text.setText(str_note);// 设为空格,否则设空串label会很 小(label大小根据其内字符串而定),以至于显示在text1的左边 break; case "CE": operator = '\0'; str = ""; str_note = " "; data_before = 0; data_after = 0; equal_flag = false; text1.setText("0"); lable_text.setText(str_note); break; case "backspace": if (str.length() > 1) { str = str.substring(0, str.length() - 1); text1.setText(str); } else { // 一位数时按backspace键相当于清零 str = ""; str_note = " "; text1.setText("0"); } break; default: break;

} /** 按?=?键 ******/ if (bt.equals("=")) { try { equal_flag = true; // 下次按数字键从true行执行 switch (operator) { case '+': if (str.charAt(str.length() - 1) == '.'){ str_note +=str.substring(0,str.length()-1); } else str_note += str; // 例如1.5+0.5=2.0,则此处 str="0.5",str_note赋值前="1.5+"赋值后="1.5+0.5" lable_text.setText(str_note); data_after = Double.parseDouble(str); data_before += data_after; str = Double.toString(data_before); text1.setText(str); break; case '-': if (str.charAt(str.length() - 1) == '.'){ str_note +=str.substring(0,str.length()-1); } else str_note += str; lable_text.setText(str_note); data_after = Double.parseDouble(str); data_before -= data_after; str = Double.toString(data_before); text1.setText(str); break; case '*': if (str.charAt(str.length() - 1) == '.'){ str_note +=str.substring(0,str.length()-1); } else str_note += str; lable_text.setText(str_note); data_after = Double.parseDouble(str); data_before *= data_after; str = Double.toString(data_before); text1.setText(str); break; case '/':

if (str.charAt(str.length() - 1) == '.'){ str_note +=str.substring(0,str.length()-1); } else str_note += str; lable_text.setText(str_note); data_after = Double.parseDouble(str); data_before /= data_after; str = Double.toString(data_before); text1.setText(str); break; default: break; } } catch (NumberFormatException e1) { // TODO Auto-generated catch block // e1.printStackTrace(); return; } operator = '\0';// 每次运算完后,将运算符置空,防止连续运算,以 及防止按%键匹配到运算符?+、-、*、/?(匹配到会导致%运算结果显示异常) } } public static void main(String[] args) { Calculator c1 = new Calculator(); c1.setDefaultCloseOperation(EXIT_ON_CLOSE); } }




友情链接: