《Java_计算器课程设计.docx》由会员分享,可在线阅读,更多相关《Java_计算器课程设计.docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Java_计算器课程设计 实验目的 熟悉JAVA GUI程序开发的一般步骤,掌握事件驱动,并能掌握一定的Java编程能力,在实践中能感受Java的奥秘!同时检验学生的学生的学习成果。 实验目标 具有加减乘除的计算器,可以延伸到开方等其他的一些计算。开发工具与平台 jdk1.5+Eclipse 3.2 设计思路: 1.界面设计 2.逻辑设计 首先明确该实验设计的要求实现的基本功能,其次设计布局,按照GridLayout的布局,将空间划分为由行和列组成的网格单元,每个单元放一个组件,网格单元大小可以不同,尽量将相同属性的按钮放在一个区域,方便操作。 3.软件调试与测试 经过调试之后,能实现基本的四
2、则运算,并能实现开方和消除错误的数。 4.软件设计优化 该软件的界面应该更加流畅,同时我相信随着自己只是的日益丰富,开发软件的速度更快,效率更高,功能更加全面! 5.总结不足 没有实现Mc,MR,MS,M+四个功能,有待进一步完善。这样才能够完成自己的第一个应用软件设计! 源程序如下: import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; impo
3、rt java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JPanel;
4、 import javax.swing.JTextField; public class Calculator extends JFrame implements ActionListener boolean init = true; boolean isMath = false; boolean clear = true; boolean clickable = true; double qian; String fuhao; int all = 0; JTextField text = new JTextField(25); JButton bM = new JButton(); JBut
5、ton bMC = new JButton(MC); JButton bMR = new JButton(MR); JButton bMS = new JButton(MS); JButton bMADD = new JButton(M+); JButton b0 = new JButton(0); JButton b1 = new JButton(1); JButton b2 = new JButton(2); JButton b3 = new JButton(3); JButton b4 = new JButton(4); JButton b5 = new JButton(5); JBut
6、ton b6 = new JButton(6); JButton b7 = new JButton(7); JButton b8 = new JButton(8); JButton b9 = new JButton(9); JButton bNOP = new JButton(+/-); JButton bDot = new JButton(.); JButton bDiv = new JButton(/); JButton bMul = new JButton(*); JButton bSub = new JButton(-); JButton bAdd = new JButton(+);
7、JButton bSprt = new JButton(sprt); JButton bMod = new JButton(%); JButton bDao = new JButton(1/x); JButton bEqual = new JButton(=); JButton bBackspace = new JButton(Backspace); JButton bCE = new JButton(CE); JButton bC = new JButton(C); public Calculator() this.setTitle(计算器); JMenuBar mainMenu = new
8、 JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu(编辑); JMenu viewMenu = new JMenu(查看); JMenu helpMenu = new JMenu(帮助); mainMenu.add(editMenu); mainMenu.add(viewMenu); mainMenu.add(helpMenu); JPanel jpDisplay = new JPanel(); JPanel jpInput = new JPanel(); JPanel jpLeft = new JPanel(); JP
9、anel jpRight = new JPanel(); text.setText(0.); text.setHorizontalAlignment(JTextField.RIGHT); jpDisplay.add(text); bM.addActionListener(this); bMC.addActionListener(this); bMS.addActionListener(this); bMR.addActionListener(this); bMADD.addActionListener(this); jpLeft.setLayout(new GridLayout(5, 1);
10、jpLeft.add(bM); jpLeft.add(bMC); jpLeft.add(bMR); jpLeft.add(bMS); jpLeft.add(bMADD); JPanel jpInnerN = new JPanel(); JPanel jpInnerS = new JPanel(); bBackspace.addActionListener(this); bCE.addActionListener(this); bC.addActionListener(this); jpInnerN.setLayout(new GridLayout(1, 3); jpInnerN.add(bBa
11、ckspace); jpInnerN.add(bCE); jpInnerN.add(bC); b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b
12、9.addActionListener(this); bNOP.addActionListener(this); bDot.addActionListener(this); bDiv.addActionListener(this); bMul.addActionListener(this); bSub.addActionListener(this); bAdd.addActionListener(this); bSprt.addActionListener(this); bMod.addActionListener(this); bDao.addActionListener(this); bE
13、qual.addActionListener(this); jpInnerS.setLayout(new GridLayout(4, 5); jpInnerS.add(b7); jpInnerS.add(b8); jpInnerS.add(b9); jpInnerS.add(bDiv); jpInnerS.add(bSprt); jpInnerS.add(b4); jpInnerS.add(b5); jpInnerS.add(b6); jpInnerS.add(bMul); jpInnerS.add(bMod); jpInnerS.add(b1); jpInnerS.add(b2); jpIn
14、nerS.add(b3); jpInnerS.add(bSub); jpInnerS.add(bDao); jpInnerS.add(b0); jpInnerS.add(bNOP); jpInnerS.add(bDot); jpInnerS.add(bAdd); jpInnerS.add(bEqual); jpRight.setLayout(new BorderLayout(); jpRight.add(jpInnerN, BorderLayout.NORTH); jpRight.add(jpInnerS, BorderLayout.CENTER); jpInput.setLayout(new
15、 BorderLayout(); jpInput.add(jpLeft, BorderLayout.WEST); jpInput.add(jpRight, BorderLayout.CENTER); Container pane = this.getContentPane(); pane.setSize(333, 208); this.setLocation(300, 200); this.setLayout(new BorderLayout(); pane.add(jpDisplay, BorderLayout.CENTER); pane.add(jpInput, BorderLayout.
16、SOUTH); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.pack(); this.setVisible(true); public void actionPerformed(ActionEvent e) if (init) this.text.setText(0.); init = false; Object source = e.getSource(); if (source = bBackspace) String s = text.getText(); text.setText(); for (int i = 0; i = 0
17、 & s.charAt(i) = 9| s.charAt(i) = . | s.charAt(i) = -) isNumber = false; break; if (isNumber = true) / 如果当前字符串首字母有-号,代表现在是个负数,再按下时,则将首符号去掉 if (s.charAt(0) = -) text.setText(); for (int i = 1; i s.length(); i+) char a = s.charAt(i); text.setText(text.getText() + a); / 如果当前字符串第一个字符不是符号,则添加一个符号在首字母处 el
18、se text.setText(- + s); if (source = bDot) System.out.println(.); clickable = true; for (int i = 0; i text.getText().length(); i+) if (. = text.getText().charAt(i) clickable = false; break; / 第一层判断是否里面含有小数点; if (clickable = true)/ 第二层判断 text.setText(text.getText() + .); if (source = bDiv) System.out
19、.println(/); qian = Double.parseDouble(text.getText(); fuhao = /; clear = false; if (source = bMul) System.out.println(*); qian = Double.parseDouble(text.getText(); fuhao = *; clear = false; if (source = bSub) System.out.println(-); qian = Double.parseDouble(text.getText(); fuhao = -; clear = false;
20、 if (source = bAdd) System.out.println(+); qian = Double.parseDouble(text.getText(); fuhao = +; clear = false; if (source = bSprt) System.out.println(sprt); String s = text.getText(); if (s.charAt(0) = -) text.setText(负数不能开根号); else text.setText(Double.toString( ng.Math.sqrt(Double .parseDouble(text
21、.getText(); clear = false; if (source = bMod) System.out.println(%,功能末实现); if (source = bDao) System.out.println(1/x); if (text.getText().charAt(0) = 0 & text.getText().length() = 1) text.setText(除数不能为零); else boolean isDec = true; int i, j, k; String s= Double.toString(1 / Double.parseDouble(text.g
22、etText(); for (i = 0; i s.length(); i+) if (s.charAt(i) = .) break; for (j = i + 1; j s.length(); j+) if (s.charAt(j) != 0) isDec = false; break; if (isDec = true) String stemp = ; for (k = 0; k i; k+) stemp += s.charAt(k); text.setText(stemp); else text.setText(s); clear = false; if (source = bEqua
23、l) System.out.println(=); Double ss=Double.parseDouble(text.getText(); text.setText(); if (fuhao = +) text.setText(qian + ss + ); if (fuhao = -) text.setText(qian - ss + ); if (fuhao = *) text.setText(qian * ss + ); if (fuhao = /) text.setText(qian / ss + ); clear = false;/ 要清空前一次的数据 ; catch (Exception ee) System.out.println(请正确输入); text.setText(运算出错,给您带来不便,sorry); clear = false; public static void main(String args) new Calculator();