欢迎来到课桌文档! | 帮助中心 课桌文档-建筑工程资料库
课桌文档
全部分类
  • 党建之窗>
  • 感悟体会>
  • 百家争鸣>
  • 教育整顿>
  • 文笔提升>
  • 热门分类>
  • 计划总结>
  • 致辞演讲>
  • 在线阅读>
  • ImageVerifierCode 换一换
    首页 课桌文档 > 资源分类 > DOC文档下载  

    用java编写地计算器基本功能可以实现.doc

    • 资源ID:7949       资源大小:60.50KB        全文页数:11页
    • 资源格式: DOC        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    用java编写地计算器基本功能可以实现.doc

    wordJsp.javapackage 练习题;import java.awt.Button;import java.awt.Color;import java.awt.Font;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.accessibility.AccessibleContext;import javax.swing.JFrame;public class Jsp extends JFrame /* * 声明计算器里的各种组件 * */private TextField tf_show;/ 显示框private Button b_0;/ 数字0private Button b_point;/ 点private Button b_add;/ 加法运算符private Button b_1;/ 数字1private Button b_2;/ 数字2private Button b_3;/ 数字3private Button b_sub;/ 减号private Button b_equal;/ 等号运算符private Button b_4;/ 数字4private Button b_5;/ 数字5private Button b_6;/ 数字6private Button b_mul;/ 乘号private Button b_fra;/ 分数private Button b_7;/ 数字7private Button b_8;/ 数字8private Button b_9;/ 数字9private Button b_div;/ 除号private Button b_mod;/ 取余private Button b_back;/ 退格private Button b_ce;/ 去除private Button b_c;/private Button b_sign;/ 加减号private Button b_sqrt;/ 根号private Button b_mc;/ MCprivate Button b_mr;/ MRprivate Button b_ms;/ MSprivate Button b_madd;/ M+private Button b_msub;/ M-private static final Color C_BUTTON = new Color(0, 0, 255);private String opt;/ 记录用户点击的操作private double num1;/ 用来记录被操作数当点击任何一个操作符时private double num2;/ 用来记录操作数/ 标识量:标志显示框是追加模式还是替换模式/ 值为true:替换 false:追加private boolean b1 = true;/private int i = 1;private Font f_show = new Font("黑体", Font.BOLD, 18);/ 设置文本框加粗public Jsp(String title) super(title);this.setBounds(300, 300, 242, 320);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLayout(null);this.setResizable(false);/ 禁用最大化/ this.init();/ this.setVisible(true);Color c=new Color(255,255,255);ne().setBackground(c.cyan);/* * 初始化计算器 */public void init() /*-组件初始化-*/tf_show = new TextField("0");b_0 = new Button("0");b_point = new Button(".");b_add = new Button("+");b_1 = new Button("1");b_2 = new Button("2");b_3 = new Button("3");b_sub = new Button("-");b_equal = new Button("=");b_4 = new Button("4");b_5 = new Button("5");b_6 = new Button("6");b_mul = new Button("*");b_fra = new Button("1/x");b_7 = new Button("7");b_8 = new Button("8");b_9 = new Button("9");b_div = new Button("/");b_mod = new Button("%");b_back = new Button("");b_ce = new Button("CE");b_c = new Button("C");b_sign = new Button("±");b_sqrt= new Button("");b_mc = new Button("MC");b_mr = new Button("MR");b_ms = new Button("MS");b_madd = new Button("M+");b_msub = new Button("M-");tf_show.setSelectionStart(tf_show.getText().length();/ 光标放到0的右边/*-组件自定义大小位置-*/tf_show.setBounds(10, 25, 215, 50);b_0.setBounds(20, 250, 75, 25);b_point.setBounds(100, 250, 35, 25);b_add.setBounds(140, 250, 35, 25);Bounds(20, 220, 35, 25);b_2.setBounds(60, 220, 35, 25);b_3.setBounds(100, 220, 35, 25);b_sub.setBounds(140, 220, 35, 25);b_equal.setBounds(180, 220, 35, 55);b_4.setBounds(20, 190, 35, 25);b_5.setBounds(60, 190, 35, 25);b_6.setBounds(100, 190, 35, 25);b_mul.setBounds(140, 190, 35, 25);b_fra.setBounds(180, 190, 35, 25);b_7.setBounds(20, 160, 35, 25);b_8.setBounds(60, 160, 35, 25);b_9.setBounds(100, 160, 35, 25);b_div.setBounds(140, 160, 35, 25);b_mod.setBounds(180, 160, 35, 25);b_back.setBounds(20, 130, 35, 25);b_ce.setBounds(60, 130, 35, 25);b_c.setBounds(100, 130, 35, 25);b_sign.setBounds(140, 130, 35, 25);b_sqrt.setBounds(180, 130, 35, 25);b_mc.setBounds(20, 100, 35, 25);b_mr.setBounds(60, 100, 35, 25);b_ms.setBounds(100, 100, 35, 25);b_madd.setBounds(140, 100, 35, 25);b_msub.setBounds(180, 100, 35, 25);/*-组件属性设置-*/b_0.setForeground(C_BUTTON);/ 加前景色b_point.setForeground(C_BUTTON);b_add.setForeground(C_BUTTON);b_1.setForeground(C_BUTTON);b_2.setForeground(C_BUTTON);b_3.setForeground(C_BUTTON);b_sub.setForeground(C_BUTTON);b_equal.setForeground(C_BUTTON);b_4.setForeground(C_BUTTON);b_5.setForeground(C_BUTTON);b_6.setForeground(C_BUTTON);b_mul.setForeground(C_BUTTON);b_fra.setForeground(C_BUTTON);b_7.setForeground(C_BUTTON);b_8.setForeground(C_BUTTON);b_9.setForeground(C_BUTTON);b_div.setForeground(C_BUTTON);b_mod.setForeground(C_BUTTON);b_back.setForeground(C_BUTTON);b_ce.setForeground(C_BUTTON);b_c.setForeground(C_BUTTON);b_sign.setForeground(C_BUTTON);b_sqrt.setForeground(C_BUTTON);b_mc.setForeground(C_BUTTON);b_mr.setForeground(C_BUTTON);b_ms.setForeground(C_BUTTON);b_madd.setForeground(C_BUTTON);b_msub.setForeground(C_BUTTON);tf_show.setFont(f_show);tf_show.setForeground(C_BUTTON);/*-设置组件的事件监听-*/b_1.addActionListener(new ActionLin();/ addActionListener是个接口,不能newb_2.addActionListener(new ActionLin();b_3.addActionListener(new ActionLin();b_sub.addActionListener(new OptListener();b_equal.addActionListener(new equalListener();b_4.addActionListener(new ActionLin();b_5.addActionListener(new ActionLin();b_6.addActionListener(new ActionLin();b_mul.addActionListener(new OptListener();b_fra.addActionListener(new OptListener();b_7.addActionListener(new ActionLin();b_8.addActionListener(new ActionLin();b_9.addActionListener(new ActionLin();b_div.addActionListener(new OptListener();b_mod.addActionListener(new OptListener();b_back.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String str = tf_show.getText();if (!str.equals("0") tf_show.setText(str.substring(0,str.length()-1); elsetf_show.setText("0"););b_ce.addActionListener(new OptListener();b_c.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) tf_show.setText("0"););/*b_sign.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String str = tf_show.getText();for(;i>0;)if(i%2!=0)tf_show.setText("-"+str);elseString str1 = str.substring(1, str.length();tf_show.setText(str1);i+;break;);*/b_sign.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) String str=tf_show.getText();if(str.indexOf("-")=-1)str="-"+str;elsestr=str.substring(1);tf_show.setText(str););r(new OptListener();b_0.addActionListener(new ActionLin();b_point.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 1-获取文本框的值String str = tf_show.getText();if (str.indexOf(".") = -1) / 2-追加一个点str = str + "."/ 3-重新设置回去tf_show.setText(str););b_add.addActionListener(new OptListener();/*-添加组件-*/this.add(tf_show);this.add(b_0);this.add(b_point);this.add(b_add);d(b_1);this.add(b_2);this.add(b_3);this.add(b_sub);this.add(b_equal);this.add(b_4);this.add(b_5);this.add(b_6);this.add(b_mul);this.add(b_fra);this.add(b_7);this.add(b_8);this.add(b_9);this.add(b_div);this.add(b_mod);this.add(b_back);this.add(b_ce);this.add(b_c);this.add(b_sign);this.add(b_sqrt);this.add(b_mc);this.add(b_mr);this.add(b_ms);this.add(b_madd);this.add(b_msub);private void show(String num) tf_show.setText(num);/* * 内部类:定义在一个类里的类 1-不能是public 2-内部类相当于本类的一个方法 3-可以访问外部类的成员属性 */ 操作数的事件监听类class ActionLin implements ActionListener public void actionPerformed(ActionEvent e) / 要执行的事/ 获取事件源Button b = (Button) e.getSource();/ getSource()是事件源String num = tf_show.getText();/ 获取事件源上的文本,将文本设置给显示框if (b1 | num.equals("0") / 替换模式tf_show.setText(b.getLabel();b1 = false;/ 将模式改为追加 else String str = tf_show.getText();tf_show.setText(str + b.getLabel();tf_show.setSelectionStart(tf_show.getText().length();/ 光标放到0的右边/ 操作符的事件监听类class OptListener implements ActionListener public void actionPerformed(ActionEvent e) Button b = (Button) e.getSource();/ getSource()是事件源/* * 1-记录操作符之前先判断在此之前有没有点击过运算符 2-如果有:如此先把前面的运算结果算出来 3-如果没有:如此直接记录 4- * 点过:opt不为null */if (null != opt) /* * 1-拿到现在文本框里的数加数 2-进展运算 3-运算完之后将结果显示在文本框里边 */num2 = Double.parseDouble(tf_show.getText();if (opt.equals("+") double num = num1 + num2;tf_show.setText(String.valueOf(num); else if (opt.equals("-") double num = num1 - num2;tf_show.setText(String.valueOf(num); else if (opt.equals("*") double num = num1 * num2;tf_show.setText(String.valueOf(num); else if (opt.equals("/") double num = num1 / num2;tf_show.setText(String.valueOf(num);/ 记录操作符opt = b.getLabel();/ 记录被加数num1 = Double.parseDouble(tf_show.getText();/ System.out.println(opt);b1 = true;/ 刚点完操作符,将模式改为替换/ 定义等于的事件监听类class equalListener implements ActionListener public void actionPerformed(ActionEvent e) num2 = Double.parseDouble(tf_show.getText();/ 获取操作符/ 计算结果if (opt.equals("+") double add = num1 + num2;if (add = (int) add) tf_show.setText(String.valueOf(int) add); else tf_show.setText(String.valueOf(add); else if (opt.equals("-") double sub = num1 - num2;if (sub = (int) sub) tf_show.setText(String.valueOf(int) sub); else tf_show.setText(String.valueOf(sub); else if (opt.equals("*") double mul = num1 * num2;if (mul = (int) mul) tf_show.setText(String.valueOf(int) mul); else tf_show.setText(String.valueOf(mul); else if (opt.equals("/") if (num2 != 0) double div = num1 / num2;if (div = (int) div) tf_show.setText(String.valueOf(int) div); else tf_show.setText(String.valueOf(div); else tf_show.setText("除数不能为0"); else if (opt.equals("%") if (num2 != 0) double mod = num1 % num2;tf_show.setText(String.valueOf(mod); else tf_show.setText("除数不能为0");/ 结果已经算出,之前的操作符可以清掉opt = null;b1 = true;/ 将追加改为替换/* * 1-去除 2-退格 3-0 4-. 5-+/- 6- 连+ */TestJsq.Javapackage 练习题;public class TestJsq public static void main(String args) Jsp jsp=new Jsp("计算器");jsp.init();jsp.setVisible(true);11 / 11

    注意事项

    本文(用java编写地计算器基本功能可以实现.doc)为本站会员(夺命阿水)主动上传,课桌文档仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知课桌文档(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-1

    经营许可证:宁B2-20210002

    宁公网安备 64010402000986号

    课桌文档
    收起
    展开