《2022年2022年简单Java计算器程序代码有注释,大学罗老 .pdf》由会员分享,可在线阅读,更多相关《2022年2022年简单Java计算器程序代码有注释,大学罗老 .pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、package cn.caculor; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class caculator / 事件监听机制 -monitor班长/ 用数组来制作菜单,一维。/ 菜单栏 menubar,菜单 menu ,菜单选项menuitem String str1
2、= 编辑 , 查看 , 帮助 ; String str2 = 复制 , 粘贴 ; String str3 = 标准 , 科学 , 数字分组 ; String str4 = 帮助主题 , 关于计算器 ; JMenuBar jmb; JMenu jm; JMenuItem jmi; JFrame jf; private String left =MC,MR,MS,M+; private String top =Backspace,CE,C; private String bottom = 7,8,9,/,sqrt, 4,5,6,*,%, 1,2,3,-,1/x, 0,+/-,.,+,= ; pri
3、vate JPanel p1 ;/面板 1 左。面板的默认布局管理器是流式布局。private JPanel p2 ;/面板 2 上private JPanel p3 ;/面板 3 主private JPanel p4; private JPanel p5; private JButton jb; private JTextField jtf = new JTextField(0); private double d1 = 0.0; /记录上一步的运算结果值。private double d2 = 0.0; /记录当前文本框的内容的double 值。private boolean isOper
4、ator = false;/记录上次按的是否是运算符键private char oper = =;/记录上次运算符public void createMenu() jmb = new JMenuBar(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - for (int i = 0; i str1.length; i+) jm = new JMenu(str1i); if (i = 0) for (int j = 0; j s
5、tr2.length; j+) jmi = new JMenuItem(str2j); jm.add(jmi); jmb.add(jm); else if(i = 1) for (int j = 0; j str3.length; j+) jmi = new JMenuItem(str3j); jm.add(jmi); jmb.add(jm); else if(i = 2) for (int j = 0; j str4.length; j+) jmi = new JMenuItem(str4j); jm.add(jmi); jmb.add(jm); public void creatFace(
6、) p1 = new JPanel(new GridLayout(4, 1,5,5); for (int i = 0; i left.length; i+) jb = new JButton(lefti); jb.addActionListener(new Monitor(); jb.setForeground(Color.RED); p1.add(jb); p2 = new JPanel(new GridLayout(1, 3,5,5); for (int i = 0; i top.length; i+) jb = new JButton(topi); jb.addActionListene
7、r(new Monitor(); jb.setForeground(Color.RED); p2.add(jb); p3 = new JPanel(new GridLayout(4, 5,5,5); for (int i = 0; i bottom.length; i+) jb = new JButton(bottomi); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - jb.addActionListener(new Mon
8、itor(); if (i+2)%5 = 0 | i = 19) jb.setForeground(Color.RED); else jb.setForeground(Color.BLUE); p3.add(jb); p4 = new JPanel(new BorderLayout(10,10); p4.add(new JButton( ),BorderLayout.NORTH); p4.add(p1,BorderLayout.CENTER); p5 = new JPanel(new BorderLayout(10,10); p5.add(p2,BorderLayout.NORTH); p5.
9、add(p3,BorderLayout.CENTER); jf = new JFrame(叮叮当当计算器); jf.setLayout(new BorderLayout(15, 15); jtf.setHorizontalAlignment(JTextField.RIGHT); jtf.setEditable(false); /jtf.setEnabled(false); jf.add(jtf,BorderLayout.NORTH); jf.add(p4,BorderLayout.WEST); jf.add(p5,BorderLayout.CENTER); public void init()
10、 createMenu(); creatFace(); jf.setVisible(true); / jf.setSize(400,300); jf.setJMenuBar(jmb); jf.pack(); public static void main(String srg) new caculator().init(); public class Monitor implements ActionListener public void actionPerformed(ActionEvent e) String jtfText = jtf.getText(); 名师资料总结 - - -精品
11、资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - String action = e.getActionCommand(); char ch = action.charAt(0); double d; switch (ch) case 0: case 1: if (action.equals(1/x) try jtf.setText(Double.toString(1/Double.parseDouble(jtfText); catch (Exception
12、 e2) break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: if (jtfText.equals(0) | isOperator) jtf.setText(action); else if(jtfText.equals(除数不能为零 ) |jtfText.equals(函数输入无效 ) else jtf.setText(jtfText + action); isOperator = false; break; case B: if (jtfText.length() != 1) jtf.setText(
13、jtfText.substring(0, jtfText.length() - 1); else jtf.setText(0); break; case C: if (action.equals(C) d1 = 0; d2 = 0; oper = =; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - isOperator = false; else if (action.equals(CE) jtf.setText(0); br
14、eak; case .: if (!jtfText.contains(.) jtf.setText(jtfText + action); break; case s: / try / / d = Double.parseDouble(jtfText); / / d = Math.sqrt(d); / / jtfText = Double.toString(Math.sqrt(Double.parseDouble(jtfText); / / catch (Exception e2) / / TODO: handle exception / jtf.setText(Double.toString(
15、Math.sqrt(Double.parseDouble(jtfText); break; case +: if (action.equals(+/-) if(jtfText.startsWith(-) jtf.setText(jtfText.substring(1); else jtf.setText(-+jtfText); break; case -: case *: case /: case %: case =: if (!(jtfText.equals(0)& oper = / |jtfText.equals(0.0)& oper = /) if (ch = =) if (!isOpe
16、rator) d2 = Double.parseDouble(jtfText); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - jtfText = Double.toString(calculate(d2, oper); else if (isOperator) oper = ch; break; else d2 = Double.parseDouble(jtfText); jtfText = Double.toString(
17、calculate(d2, oper); oper = ch; if (!jtfText.endsWith(.0) jtf.setText( + d1); else jtf.setText(jtfText.substring(0, jtfText.length() - 2); isOperator = true; else jtf.setText(除数不能为零); break; / 定义一个函数,来完成数学运算public double calculate(double d2,char oper) if(oper = +) d1 += d2; else if(oper = -) d1 -= d2; else if(oper = *) d1 *= d2; else if(oper = /) d1 /= d2; else if(oper = %) d1 %= d2; else if(oper = =) d1 = d2; return d1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -