1、四则运算计算器设计说明书一、设计目标本次计算器的程序设计,通过使用 JAVA 中的 AWT 包和 Swing 包的类库设计图形界面的计算器。此计算器能够完成加减乘除的四则混合运算。利用面向对象程序设计的思想,将各个组件的事件响应分别用不同的方式表达出来,并且使用了图形界面中的事件委托机制来处理事件响应。二、设计流程1.分析该计算器需要完成的功能。用户能够完成添加负号的四则混合运算,开方,取倒数,并且计算器能够自动识别运算符的优先级,根据用户输入的运算表达式,自动计算出相应的结果。同时还完成了计算器中 C 按钮清屏功能和 Backspace 退格键。2. 考虑异常处理。(1)当输入的表达式中出现
2、除零的操作,显示框将显示“Infinity(无穷大)”。(2)当输入的表达式错误时,将弹出提示框显示“表达式错误请重新输入” (3)当计算器的显示文本框里为没有输入内容时直接点击等号按钮3. 编码实现计算器的功能。(1)新建相关的文件。(2)引入 JAVA 中相关的包。(3)定义相关的变量,创建相关组件,并对组件的属性进行设置。(4)对所创建的组件进行布局,完成界面的实现。(5)为各个组件添加事件监听器。(6)重写事件接口 ActionListener 的方法 public void actionPerformed(ActionEvent e)。(7)为各个组件编写事件代码,完成每个按钮的不同
3、功能。三、程序截图四、程序代码 import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;i
4、mport javax.swing.JTextField;public class Calc extends JFrame implements ActionListener JPanel topPanel = null;JPanel midPanel = null;JPanel bottomPanel = null;JTextField tResult = null;JButton backspaceButton = null;JButton ceButton = null;JButton cButton = null;JButton button1 = null;JButton butto
5、n2 = null;JButton button3 = null;JButton button4 = null;JButton button5 = null;JButton button6 = null;JButton button7 = null;JButton button8 = null;JButton button9 = null;JButton button0 = null;JButton buttonDiv = null;JButton buttonPlus = null;JButton buttonMinus = null;JButton buttonMul = null;JBu
6、tton buttonSqrt = null;JButton buttonMod = null;JButton buttonPM = null;JButton buttonX = null;JButton buttonPoint = null;JButton buttonEquals = null;StringBuffer str = new StringBuffer();boolean isDouble = false;/ 是否为实数int opFlag = -1;static double t1 = 0, t2 = 0, t3 = 0, result = 0;static int opfl
7、ag1 = -1, opflag2 = -1, flag = 0, resflag = 1;int preOp, currentOp = 0;/ 标准位double op1 = 0, op2 = 0;/ 操作数double n3;/ 取得屏幕对象Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();/ 取得屏幕的宽度int width = screenSize.width;/ 取得屏幕的高度int heigth = screenSize.height;public Calc() tResult = new JTex
8、tField(“0.“);tResult.setColumns(26);tResult.setHorizontalAlignment(JTextField.RIGHT);topPanel = new JPanel();topPanel.add(tResult);backspaceButton = new JButton(“Backspace“);backspaceButton.addActionListener(this);ceButton = new JButton(“CE“);ceButton.addActionListener(this);cButton = new JButton(“C
9、“);cButton.addActionListener(this);midPanel = new JPanel();midPanel.add(backspaceButton);midPanel.add(ceButton);midPanel.add(cButton);bottomPanel = new JPanel(new GridLayout(4, 5, 3, 3);button7 = new JButton(“7“);button7.addActionListener(this);bottomPanel.add(button7);button8 = new JButton(“8“);but
10、ton8.addActionListener(this);bottomPanel.add(button8);button9 = new JButton(“9“);button9.addActionListener(this);bottomPanel.add(button9);buttonDiv = new JButton(“/“);buttonDiv.addActionListener(this);bottomPanel.add(buttonDiv);buttonSqrt = new JButton(“sqrt“);buttonSqrt.addActionListener(this);bott
11、omPanel.add(buttonSqrt);button4 = new JButton(“4“);button4.addActionListener(this);bottomPanel.add(button4);button5 = new JButton(“5“);button5.addActionListener(this);bottomPanel.add(button5);button6 = new JButton(“6“);button6.addActionListener(this);bottomPanel.add(button6);buttonMul = new JButton(
12、“*“);buttonMul.addActionListener(this);bottomPanel.add(buttonMul);buttonMod = new JButton(“%“);buttonMod.addActionListener(this);bottomPanel.add(buttonMod);button1 = new JButton(“1“);button1.addActionListener(this);bottomPanel.add(button1);button2 = new JButton(“2“);button2.addActionListener(this);b
13、ottomPanel.add(button2);button3 = new JButton(“3“);button3.addActionListener(this);bottomPanel.add(button3);buttonMinus = new JButton(“-“);buttonMinus.addActionListener(this);bottomPanel.add(buttonMinus);buttonX = new JButton(“1/x“);buttonX.addActionListener(this);bottomPanel.add(buttonX);button0 =
14、new JButton(“0“);button0.addActionListener(this);bottomPanel.add(button0);buttonPM = new JButton(“+/-“);buttonPM.addActionListener(this);bottomPanel.add(buttonPM);buttonPoint = new JButton(“.“);buttonPoint.addActionListener(this);bottomPanel.add(buttonPoint);buttonPlus = new JButton(“+“);buttonPlus.
15、addActionListener(this);bottomPanel.add(buttonPlus);buttonEquals = new JButton(“=“);buttonEquals.addActionListener(this);bottomPanel.add(buttonEquals);this.setLayout(new BorderLayout();this.add(topPanel, “North“);this.add(midPanel, “Center“);this.add(bottomPanel, “South“);this.setDefaultCloseOperati
16、on(JFrame.EXIT_ON_CLOSE);this.setSize(310, 225);this.setResizable(false);/ 设置窗体出现在屏幕的中央this.setLocation(width - this.getWidth() / 2,(heigth - this.getHeight() / 2);this.setVisible(true);/* param args*/public static void main(String args) new Calc();Overridepublic void actionPerformed(ActionEvent e)
17、String s = e.getActionCommand().trim();if (s.equals(“CE“) / 如果是 CE 则清除文本框tResult.setText(“0.“); else if (s.equals(“Backspace“) if (!tResult.getText().trim().equals(“0.“) / 如果文本框中有内容if (str.length() != 1 else tResult.setText(“0.“);str.setLength(0);op2 = Double.parseDouble(tResult.getText().trim(); el
18、se if (s.equals(“C“) / 如果是 C 删除当前计算tResult.setText(“0.“);op1 = op2 = 0;str.replace(0, str.length(), “ “);preOp = currentOp = 0; else if (s.equals(“1/x“) / 如果按键为 1/x 则将文本框中的数据为它的倒数String temp = tResult.getText().trim();double dtemp = Double.parseDouble(temp);tResult.setText(“ + 1 / dtemp); else if (s
19、.equals(“sqrt“) / 如果按键为 sqrt 则将文本框中的内容求平方根String temp = tResult.getText().trim();double dtemp = Double.parseDouble(temp);tResult.setText(“ + Math.sqrt(dtemp); else if (s.equals(“+“) str.setLength(0);if (currentOp = 0) preOp = currentOp = 1;op2 = 0;tResult.setText(“ + op1); else currentOp = preOp;pre
20、Op = 1;switch (currentOp) case 1:op1 += op2;tResult.setText(“ + op1);break;case 2:op1 -= op2;tResult.setText(“ + op1);break;case 3:op1 *= op2;tResult.setText(“ + op1);break;case 4:op1 /= op2;tResult.setText(“ + op1);break; else if (s.equals(“-“) str.setLength(0);if (currentOp = 0) preOp = currentOp
21、= 2;/ op1=op2;op2=0;tResult.setText(“ + op1); else currentOp = preOp;preOp = 2;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText(“ + op1);break;case 2:op1 = op1 - op2;tResult.setText(“ + op1);break;case 3:op1 = op1 * op2;tResult.setText(“ + op1);break;case 4:op1 = op1 / op2;tResult.setText(“
22、 + op1);break; else if (s.equals(“*“)/ *str.setLength(0);if (currentOp = 0) preOp = currentOp = 3;/ op1=op2;op2=1;tResult.setText(“ + op1);/ op1=op2; else currentOp = preOp;preOp = 3;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText(“ + op1);break;case 2:op1 = op1 - op2;tResult.setText(“ + o
23、p1);break;case 3:op1 = op1 * op2;tResult.setText(“ + op1);break;case 4:op1 = op1 / op2;tResult.setText(“ + op1);break; else if (s.equals(“/“)/ /str.setLength(0);if (currentOp = 0) preOp = currentOp = 4;/ op2=1;tResult.setText(“ + op1);/ op1=op2; else currentOp = preOp;preOp = 4;switch (currentOp) ca
24、se 1:op1 = op1 + op2;tResult.setText(“ + op1);break;case 2:op1 = op1 - op2;tResult.setText(“ + op1);break;case 3:op1 = op1 * op2;tResult.setText(“ + op1);break;case 4:op1 = op1 / op2;tResult.setText(“ + op1);break; else if (s.equals(“=“)/ =if (currentOp = 0) str.setLength(0);tResult.setText(“ + op2)
25、; else str.setLength(0);currentOp = preOp;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText(“ + op1);break;case 2:op1 = op1 - op2;tResult.setText(“ + op1);break;case 3:op1 = op1 * op2;tResult.setText(“ + op1);break;case 4:op1 = op1 / op2;tResult.setText(“ + op1);break;currentOp = 0;op2 = 0;
26、else if (s.equals(“.“) isDouble = true;if (tResult.getText().trim().indexOf(.) != -1);else if (tResult.getText().trim().equals(“0“) str.setLength(0);tResult.setText(str.append(“0“ + s).toString();/ else/ if(tResult.getText().trim().equals(“)/如果初时显示为空则不做任何操作else tResult.setText(str.append(s).toString
27、(); else if (s.equals(“0“)/ 如果选择的是“0“这个数字键if (tResult.getText().trim().equals(“0.“) else tResult.setText(str.append(s).toString();op2 = Double.parseDouble(tResult.getText().trim(); else if (s.equals(“%“)/ 如果选择的是“0“ 这个数字键 else tResult.setText(str.append(s).toString();op2 = Double.parseDouble(tResult.getText().trim();if (currentOp = 0)op1 = op2;