




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package Swing;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Work1 public static void main(String args) new JsqView(340, 240);SuppressWarnings(“serial“)class JsqView extends JFrame JTextField jtf;JButton jb1, jb2, jb3;JButton jb;JPanel jp1, jp2, jp3, jp4;MyActionListener l;public JsqView(int weith, int heigh) Container con = this.getContentPane();con.setLayout(new BorderLayout(5, 10);l = new MyActionListener(this);jtf = new JTextField(26);jp1 = new JPanel();jp1.add(jtf);jtf.setHorizontalAlignment(JTextField.RIGHT);jp3 = new JPanel();jp3.setLayout(new BorderLayout(5, 10);jb1 = new JButton(“Backspace“);jb2 = new JButton(“CE“);jb3 = new JButton(“C“);jb1.setForeground(Color.red);jb1.addActionListener(l);jb2.addActionListener(l);jb2.setForeground(Color.red);jb3.addActionListener(l);jb3.setForeground(Color.red);jp2 = new JPanel();jp2.setLayout(new GridLayout(1, 3, 5, 5);jp2.add(jb1);jp2.add(jb2);jp2.add(jb3);jp4 = new JPanel();jp4.setLayout(new GridLayout(4, 5, 5, 5);jb = new JButton20;String str = “7“, “8“, “9“, “/“, “sqrt“, “4“, “5“, “6“, “*“, “%“,“1“, “2“, “3“, “-“, “1/x“, “0“, “+/-“, “.“, “+“, “=“ ;for (int i = 0; i 20; i+) jbi = new JButton(stri);jbi.addActionListener(l);if (i = 3 | i = 8 | i = 13 | i = 18 | i = 19) jbi.setForeground(Color.red);jp4.add(jbi);jp3.add(jp2, BorderLayout.NORTH);jp3.add(jp4);con.add(jp1, BorderLayout.NORTH);con.add(jp3);this.setTitle(“计算器“);this.setSize(weith, heigh);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);class MyActionListener implements ActionListener JsqView jv;char operator;String temp;Count c = new Count();boolean b = false;public MyActionListener(JsqView jv) this.jv = jv;public void actionPerformed(ActionEvent e) String str = e.getActionCommand().trim();/System.out.println(str);if (str.matches(“d.“) /System.out.println(str);if(b)jv.jtf.setText(“);b = false;StringBuffer sbf = new StringBuffer();String s = jv.jtf.getText().trim();sbf.append(s);if(“.equals(s)else if(s.matches(“d+“)sbf.append(str);else if(str.matches(“d“)sbf.append(str);jv.jtf.setText(sbf.toString(); else if (str.matches(“+-*/%“) b =false;operator = str.charAt(0);/System.out.println(str);temp = jv.jtf.getText().trim();jv.jtf.setText(“); else if (str.equals(“=“) String s = jv.jtf.getText().trim();/ System.out.println(temp);/ System.out.println(s);if(“.equals(s)|s=null|temp=null)return;if(temp.matches(“d+.d+“)|(s.matches(“d+.d+“)double i = Double.parseDouble(temp);double j = Double.parseDouble(s);if(!b)temp=s;double sum =c.count(i, j, operator,jv);jv.jtf.setText(sum + “);elselong i = Long.parseLong(temp);long j = Long.parseLong(s);if(!b)temp=s;long sum = c.count(i, j, operator,jv);jv.jtf.setText(sum + “);b = true;else if(str.equals(“1/x“)String s = jv.jtf.getText().trim();double i = Double.parseDouble(s);double sum = c.count(i,jv);jv.jtf.setText(sum + “);b = true;else if(str.equals(“CE“)b= false;jv.jtf.setText(“);else if(str.equals(“Backspace“)b= false;jv.jtf.setText(temp);else if(str.equals(“sqrt“)b= true;String s = jv.jtf.getText().trim();double b = Math.sqrt(Double.parseDouble(s);jv.jtf.setText(b+“);else if(str.equals(“+/-“)String s = jv.jtf.getText().trim();if(s.startsWith(“-“)s= s.substring(1, s.length();elses = “-“+s;jv.jtf.setText(s);class Count public long count(long i,long j,char operator,JsqView jv)long num = 0;switch (operator) case +:num = i + j;break;case -:num = i - j;break;case *:num = i * j;break;case /:if(j=0)try throw new MyException(); catch (MyException e) JOptionPane.showMessageDialog(jv, “被除数不能为零“,“错误“,JOptionPane.ERROR_MESSAGE);return 0;num = i / j;break;case %:num = i % j;break;default:break;return num;public double count(double i,double j,char operator,JsqView jv)double num = 0;switch (operator) case +:num = i + j;break;case -:num = i - j;break;case *:num = i * j;break;case /:if(j=0.0)try throw new MyException(); catch (MyException e) JOptionPane.showMessageDialog(jv, “被除数不能为零“,“错误“,JOptionPane.ERROR_MESSAGE);return 0;num = i / j;break;default:break;return num;public double count (double i,JsqView jv)double num;if(i=0.0)try throw new MyException
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年管理体系认证基础考试真题(含答案)
- 摇臂拍摄基础知识培训
- 内蒙古自治区通辽市2024-2025学年八年级下学期期末语文试题(解析版)
- 摄影图像基础知识培训课件
- 热工检测技术试题及答案
- 300万平方米纸质包装技改项目可行性研究报告模板-立项备案
- 2025餐饮劳动的合同范本
- 2025高级工程师标准劳动合同
- 摄制部基础知识培训总结
- 2025年探讨无证房屋的租赁合同效力
- 2024光伏并网柜技术规范
- 梨状窝瘘的临床特征
- 品质异常检讨
- 《公路工程预算定额》(JTGT3832-2018)
- 商业综合体新旧物业交接方案
- 2024年甘肃省公务员录用考试《行测》真题及答案解析
- 人工智能教学实训综合应用平台需求说明
- GB/T 24633.1-2024产品几何技术规范(GPS)圆柱度第1部分:词汇和参数
- (完整版)八年级上物理思维导图
- 2022级数字媒体技术应用专业人才培养方案(中职)
- 外墙保温及真石漆施工方案
评论
0/150
提交评论