




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java计算器程序代码以下为代码:import avax.swmg.*;impoitjava.awt.*;impoitj ava.awt.event.*;public class Calculator extends JFrame unplements ActionListener private booleandotExist, operated, equaled; / 帮助运算的布尔变量private double stoiedNumbei; / 目前的结果 private chai lastOpeiator; / 表示上一 运算符 private JTextField opeiation
2、; / 结果栏private JButton dot plus, minus, multi, div, sqit, equal, changePN, clear; 运算符private JButton numbeis; / 数字/构造者public Calculatoi(setTitle(flCalculatorM;初始化变量dotExist = false; /表示当前的数是否有小数点operated = false; /表示任意运算符是否被按下equaled = false; /表示等号是否 被按下 stoiedNumbei = 0;lastOpeiator =初始化窗口变量operati
3、on = new JTextField(uOn;opeiation.setEditable(false;numbers = new JButton10;for (mti = 0; i< 10; i+numbersi = new JButton(,n 十 i;dot = newplus = new JButton(H+H;nuiius = new JButton(M-n;multi = new JButton(H*M;div = new JButton(17M;sqrt = new JButton(W”;equal = new JButton(M=n;changePN = new JBut
4、ton(M±M;cleai = new JButton(nACu;将窗口物体放入窗口GndBagLayout layout = new GndBagLayout(; getConteiitPane(.setLayout(layout;addComponent(layout, opeiation. 0, 0, 4, 1; addComponent(layout, numbeisl, 1, 0, 1,1; addComponent(layout, numbeis2, 1, 1, 1, 1; addComponent(layout, numbers3, 1, 2, 1, 1; addCom
5、ponent(layout, numbeis4, 2, 0, 1, 1; addComponent(layout, numbers5, 2, 1, 1, 1; addComponent(layout, numbeis65 2, 2, 1, 1; addComponent(layout, numbeis7, 3, 0, 1, 1; addComponent(layout, numbeis8, 3, 1, 1, 1;addComponent(layout, numbeis9, 3, 2, 1, 1; addComponent(layout, dot, 4, 0, 1, 1;addComponent
6、(layout, numbers05 4, 1, 1, 1; addComponent(layout, sqrt, 4, 2, 1, 1; addComponent(layout, plus, 1,3, 1, 1; addComponent(layout, minus, 2, 3, 1, 1;addComponent(layout, multi, 3, 3 J, 1; addComponent(layout, div, 4, 3, 1, 1; addComponent(layout, equal, 5, 0, 2, 1; addComponent(layout, changePN, 5, 2,
7、 1, 1; addComponent(layout, cleai; 5, 3, 1, 1; 对按钮进行反应的方法public void actioiiPeifdimed(ActionEvent e JButtonbtn = (JButtone.getSource(; if (btn = clearopeiation.setText(n0H;dotExist = false;stoiedNumbei = 0;lastOpeiator = ?;)else if (btn = equalopeiate(,=,;equaled = tine;)else if (btn = plusoperate(,
8、+,;equaled = false;)else if (btn = niuiusopeiate(1-1;equaled = false; else if (btn = multi operatef*1;equaled = false; else if (btn = div opeiate(V,;else if (btn = chaiigePN operate(,pl;operate(,=l;equaled = true; else if (btn = sqrt operate(,sl;operate(,=l; equaled = tme; else if (equaled stoiedNum
9、ber = 0; fbr (inti = 0; i< 10; i+ if (btn = numbeisi if (opeiation.getText(.equals(nOM opeiation.setText(,M,+ i; else i“! operated operation.setText(opeiation.getText(+ i; else opeiation.setText(IH,+ i;operated = false; if (btn = dot && ! dotExist operation.setText(operation.getText(十”;do
10、tExist = tine; / 进行运算的方法 private void opeiate(chai operator doublecunentNumber Double.valueOf(opeiatioii.getText(.doubleValue(; if (lastOpeiatoi = stoiedNumber = cunentNumbei-; else if (lastOpeiatoi = "V stoiedNumbei +=cunentNumber; else if (lastOpeiatoi = stoiedNumber -= cunentNumber; else if
11、(lastOpeiatoi' = stoiedNumbei *= cunentNumber; else if (lastOpeiatoi = 7' stoiedNumber /= cunentNumbef; else if (lastOpeiatoi = 'p' stoiedNumbei *= -1;=else if (lastOpeiatoi = stoiedNumbei = Math. sqrt(cunentNumbei-; else if (lastOpeiatoi =&& equaled stoiedNumbei = cunentNumb
12、ef; operation.setText(Mn +stoiedNumber; operated = tine; lastOpeiatoi = opeiator; / 快捷使用 GndBagLayout 的方法 private void addComponent(GiidBagLayout layout. Component component, int row, mt col, mt width, mt height GndBagConstiaints constiamts=new GndBagConstiaints(; constiaints.fill = GndBagConstiaints.BOTH; constiamts.insets = new Iiisets(10, 2, 10, 2; constiauits.weightx = 100; constiaints.weighty = 100;constiauits.gndx = col; coiistiaints.gfidy = row; constiamts.giidwidth = width;constiauits.gndheight = height; layout.setConstiaints(component, constiaints;if (component instanceofJButton
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年在线教育平台教学质量提升策略:学习分析技术应用
- 2025年深海矿产资源勘探技术国际合作动态报告
- 2025农村金融服务风险管理策略与案例研究
- 新型页岩气开采技术2025年环境风险评估与生态保护红线
- 2025年基层医疗机构信息化建设与基层卫生人才队伍培养策略
- 2025年金融科技企业估值模型构建与投资策略创新报告
- 2025年碳捕获与封存(CCS)在农业产业应用案例分析报告
- 2025年生态修复工程中生态系统服务功能评估与生态系统服务价值评估模型构建报告
- K2教育领域人工智能个性化学习系统在促进教育信息化与实现教育个性化中的应用报告
- 统编版中考必背重点古诗文61篇
- 全国职业院校技能大赛高职组(商务数据分析赛项)备赛试题库(含答案)
- 《中华人民共和国职业分类大典》(2022年版)各行业职业表格统计版(含数字职业)
- 数字孪生数据映射机制-深度研究
- 《药学综合知识与技能》课件-过敏性鼻炎的自我药疗与用药指导
- 加温毯预防术中低体温
- 检验科实验室生物安全风险评估
- 石材维修合同范本(2025年)
- 【MOOC】全新版大学进阶英语综合教程I-内蒙古大学 中国大学慕课MOOC答案
- 《南方航空公司汇率风险管理策略案例分析》
- 2025年山东省春季高考模拟考试英语试卷(含答案解析)
- 《气瓶使用安全培训》课件
评论
0/150
提交评论