




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精选文档package two;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.util.*;public class calculator implements ActionListener int count = 0;private static int a;JFrame frame = new JFrame(计算器);JTextArea area = new JTextArea();JTextField fieldshow = new JTextField(0);JTextField fieldcalculator = new JTextField();JPanel leftpanel = new JPanel();JPanel rightpanel = new JPanel();JPanel buttonpanel = new JPanel();JPanel motionpanel = new JPanel();JButton button1 = new JButton(1);JButton button2 = new JButton(2);JButton button3 = new JButton(3);JButton button4 = new JButton(+);JButton button5 = new JButton(c);JButton button6 = new JButton(4);JButton button7 = new JButton(5);JButton button8 = new JButton(6);JButton button9 = new JButton(-);JButton button10 = new JButton(退格);JButton button11 = new JButton(7);JButton button12 = new JButton(8);JButton button13 = new JButton(9);JButton button14 = new JButton(*);JButton button15 = new JButton(sin);JButton button16 = new JButton(0);JButton button17 = new JButton(+/-);JButton button18 = new JButton(.);JButton button19 = new JButton(/);JButton button20 = new JButton(=);JButton button21 = new JButton(保存);JButton button22 = new JButton(复制);JButton button23 = new JButton(清除);StringBuffer S = new StringBuffer();/ t用来记录前一个运算符号是+ - * / =中的哪一个char t;/ count用来实现运算符号计数,a用来识别退格、正负号操作前一个操作是否是运算符号+ - * / sin =/ int count = 0, a = 0;Double x1, x2 = 0d, result;public static void main(String a) calculator that = new calculator();that.go();void go() leftpanel.setLayout(new BorderLayout();leftpanel.add(fieldshow, BorderLayout.NORTH);leftpanel.add(buttonpanel, BorderLayout.CENTER);buttonpanel.setLayout(new GridLayout(4, 5);buttonpanel.add(button1);buttonpanel.add(button2);buttonpanel.add(button3);buttonpanel.add(button4);buttonpanel.add(button5);buttonpanel.add(button6);buttonpanel.add(button7);buttonpanel.add(button8);buttonpanel.add(button9);buttonpanel.add(button10);buttonpanel.add(button11);buttonpanel.add(button12);buttonpanel.add(button13);buttonpanel.add(button14);buttonpanel.add(button15);buttonpanel.add(button16);buttonpanel.add(button17);buttonpanel.add(button18);buttonpanel.add(button19);buttonpanel.add(button20);button1.addActionListener(this);/ 事件监听button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);button6.addActionListener(this);button7.addActionListener(this);button8.addActionListener(this);button9.addActionListener(this);button10.addActionListener(this);button11.addActionListener(this);button12.addActionListener(this);button13.addActionListener(this);button14.addActionListener(this);button15.addActionListener(this);button16.addActionListener(this);button17.addActionListener(this);button18.addActionListener(this);button19.addActionListener(this);button20.addActionListener(this);button21.addActionListener(this);button22.addActionListener(this);button23.addActionListener(this);rightpanel.setLayout(new BorderLayout();rightpanel.add(fieldcalculator, BorderLayout.NORTH);rightpanel.add(area, BorderLayout.CENTER);rightpanel.add(motionpanel, BorderLayout.SOUTH);motionpanel.add(button21);motionpanel.add(button22);motionpanel.add(button23);Container con = frame.getContentPane();frame.setLayout(new GridLayout(1, 2);con.add(leftpanel);con.add(rightpanel);frame.setBounds(200, 200, 600, 300);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.addWindowListener(new WindowListener() / 关闭程序时弹出警告public void windowClosing(WindowEvent e) int result = JOptionPane.showConfirmDialog(frame, 确实要关闭程序吗?,警告, JOptionPane.OK_CANCEL_OPTION);if (result = JOptionPane.OK_CANCEL_OPTION) System.exit(0); else public void windowOpened(WindowEvent e) public void windowIconified(WindowEvent e) public void windowDeiconified(WindowEvent e) public void windowDeactivated(WindowEvent e) public void windowClosed(WindowEvent e) public void windowActivated(WindowEvent e) );public void actionPerformed(ActionEvent e) / 0123456789等按钮if (e.getSource() = button1 | e.getSource() = button2| e.getSource() = button3 | e.getSource() = button6| e.getSource() = button7 | e.getSource() = button8| e.getSource() = button11 | e.getSource() = button12| e.getSource() = button13 | e.getSource() = button16) a = 0;if (count = 0) S.append(e.getActionCommand();fieldshow.setText(S.toString();x1 = Double.parseDouble(fieldshow.getText(); else if (count = 1) S.append(e.getActionCommand();fieldshow.setText(S.toString();x2 = Double.parseDouble(fieldshow.getText();/ 小数点的容错性if (e.getSource() = button18)/ 单击.按钮输入小数a = 0;if (fieldshow.getText().trim().indexOf(.) != -1)/ 判断字符串中是否已经包含了小数点 else/ 如果没有小数点if (fieldshow.getText().trim().equals(0)/ 如果初时显示为0S.setLength(0);fieldshow.setText(S.append(0 + e.getActionCommand().toString(); else if (fieldshow.getText().trim().equals()/ 如果初时显示为空则不做任何操作 else fieldshow.setText(S.append(e.getActionCommand().toString();/ 运算符号 + - * /if (e.getSource() = button4 | e.getSource() = button9| e.getSource() = button14 | e.getSource() = button19) count+;a = 1;S.setLength(0);if (e.getSource() = button4)/ +法switch (t) case +:result = x1 + x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + + + x2 + = + result + n);break;case -:result = x1 - x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + - + x2 + = + result + n);break;case *:result = x1 * x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + * + x2 + = + result + n);break;case /:if (x2 = 0)fieldshow.setText(除数不能为零);else result = x1 / x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + / + x2 + = + result + n);break;case =:default:if (count = 2) x1 = result;t = +;if (e.getSource() = button9)/ -法switch (t) case +:result = x1 + x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + + + x2 + = + result + n);break;case -:result = x1 - x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + - + x2 + = + result);area.append(x1 + - + x2 + = + result + n);break;case *:result = x1 * x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + * + x2 + = + result);area.append(x1 + * + x2 + = + result + n);break;case /:if (x2 = 0)fieldshow.setText(除数不能为零);else result = x1 / x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + / + x2 + = + result);area.append(x1 + / + x2 + = + result + n);break;case =:default:if (count = 2) x1 = result;t = -;if (e.getSource() = button14)/ *法switch (t) case +:result = x1 + x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + + + x2 + = + result + n);break;case -:result = x1 - x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + - + x2 + = + result);area.append(x1 + - + x2 + = + result + n);break;case *:result = x1 * x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + * + x2 + = + result);area.append(x1 + * + x2 + = + result + n);break;case /:if (x2 = 0)fieldshow.setText(除数不能为零);else result = x1 / x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + / + x2 + = + result);area.append(x1 + / + x2 + = + result + n);break;case =:default:if (count = 2) x1 = result;t = *;if (e.getSource() = button19)/ /法switch (t) case +:result = x1 + x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + + + x2 + = + result + n);break;case -:result = x1 - x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + - + x2 + = + result);area.append(x1 + - + x2 + = + result + n);break;case *:result = x1 * x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + * + x2 + = + result);area.append(x1 + * + x2 + = + result + n);break;case /:if (x2 = 0)fieldshow.setText(除数不能为零);else result = x1 / x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + / + x2 + = + result);area.append(x1 + / + x2 + = + result + n);break;case =:default:if (count = 2) x1 = result;t = /;/ 清零操作if (e.getSource() = button5) fieldshow.setText(0);/ 把显示屏清零count = 0;a = 0;t = ;x1 = 0d;x2 = 0d;S.setLength(0);/ 清空字符串缓冲区以准备接收新的输入运算数/ 退格操作if (e.getSource() = button10) if (a = 1) / 判断前一个操作是否是运算符号和等于操作System.out.println(运算符号不能退格!); else if (!fieldshow.getText().trim().equals(0)/ 如果显示屏显示的不是零if (S.length() != 1) / 可能抛出字符串越界异常fieldshow.setText(S.delete(S.length() - 1, S.length().toString(); else fieldshow.setText(0);S.setLength(0);if (count = 0) x1 = Double.parseDouble(fieldshow.getText().trim(); else if (count = 1) x2 = Double.parseDouble(fieldshow.getText().trim();/ 求正弦值操作 实现 运算过程中正弦值的优先性if (e.getSource() = button15) a = 1;if (count = 0) if (S.length() = 0) / 第一个按键不能为sinfieldshow.setText(先输入数字); else result = Math.sin(x1);fieldshow.setText(result.toString();fieldcalculator.setText(sin( + x1 + ) + = + result);area.append(sin( + x1 + ) + = + result + n);x1 = result;/ 将正弦值求出来赋值给x1if (count = 1) result = Math.sin(x2);fieldcalculator.setText(sin( + x2 + ) + = + result);area.append(sin( + x2 + ) + = + result + n);x2 = result;/ 将正弦值求出来赋值给x2S.setLength(0);/ 正负值操作if (e.getSource() = button17) if (a = 1) / 判断前一个操作是否是运算符号和等于操作 else if (count = 0) x1 = Double.parseDouble(fieldshow.getText().trim();fieldshow.setText( + (-x1);x1 = -x1; else if (count = 1) x2 = Double.parseDouble(fieldshow.getText().trim();fieldshow.setText( + (-x2);x2 = -x2;/ 等于号操作if (e.getSource() = button20) switch (t) case +:result = x1 + x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + + + x2 + = + result);area.append(x1 + + + x2 + = + result + n);break;case -:result = x1 - x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + - + x2 + = + result);area.append(x1 + - + x2 + = + result + n);break;case *:result = x1 * x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + * + x2 + = + result);area.append(x1 + * + x2 + = + result + n);break;case /:if (x2 = 0)fieldshow.setText(除数不能为零);else result = x1 / x2;fieldshow.setText(result.toString();fieldcalculator.setText(x1 + / + x2 + = + result);area.append(x1 + / + x2 + = + result + n);break;case =:default:x1 = x2 = 0d;count = 0;a = 1;t = =;S.setLength(0);/ 保存操作if (e.getSource() = button21) JFileChooser jf = new JFileChooser();/ 实例化文件选择器JTextField jt = new JTextField();Calendar ca = Calendar.getInstance();int year = ca.get(Calendar.YEAR);/ 获取年份int month = ca.get(Calendar.MONTH);/ 获取月份int day = ca.get(Calendar.DATE);/ 获取日int minute = ca.get(Calendar.MINUTE);/ 分int hour = ca.get(Calendar.HOUR);/ 小时int second = ca.get(Calendar.SECOND);/ 秒int select = jf.showSaveDialog(frame);/ 显示对话框if (select = JFileChooser.APPROVE_OPTION) / 单击保存按钮File file = jf.getSelectedFile();/ 得到选择的文件try Writer out = null;out = new FileWriter(jf.getSelectedFile();/ 输出流String str = area.getText() + 时间: + year + -+ month + - + day + n + hour + : + minute+ : + second;out.w
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 客车公司化经营管理制度
- 科研项目超垫支管理制度
- 核酸检测采样管理制度
- 大餐饮公司员工管理制度
- 公司瓶装饮用水管理制度
- 厨房水龙头安装管理制度
- led信息安全管理制度
- 初中毕业生资助管理制度
- 江西危险货物管理制度
- 智慧工地系统管理制度
- 九师联盟2024-2025学年高二下学期6月摸底联考语文试题(含答案)
- 非遗文化掐丝珐琅景泰蓝
- 电动葫芦考试题及答案
- 2025广东省劳动合同样本
- 2025餐饮兼职合同样本
- 农资安全宣传课件
- 绿色营销试题及答案详解
- 2025年三级电子商务师(网商)理论考试题库(浓缩500题)
- 2025年下半年浙江省杭州建德市部分事业单位招聘(134人)易考易错模拟试题(共500题)试卷后附参考答案
- 2026年上海中考英语一轮复习:考纲词汇一词多义词清单
- 译文文学性再现与译者主体性发挥的对比研究
评论
0/150
提交评论