




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java编程作业年级:2010 班级:计科六班 姓名:李世建 学号:3110006093一、 作业题目:用Swing组件创建Windows系统中的计算器界面。二、 实现界面的代码import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.text.*;import java.lang.StringBuffer.*;class Calculator extends JFrame implements ActionListener /实现事件监听接口/Panel的使用:使用多个Panel和Panel的嵌套设置界面虽然关系较复杂,/但却更有效的控制界面,更人性化;整个窗口配置也会更容易JFrame mainFrame;/定义窗口对象JTextField text; /定义文本框对象JPanel p0=new JPanel(); /是文本框的加载面板JPanel p=new JPanel();/是p1,p2的加载面板JPanel p1=new JPanel(); /定义左边面板,放置5个内存功能键按钮,加载到p中JPanel p2=new JPanel(); /定义右边面板,放置符号、数字,并加载到p中JPanel p21=new JPanel(); /定义中间上面的面板,设置3个按钮,加载到p2中JPanel p22=new JPanel(); /定义中间下面的面板,设置为数字、运算符号区,加载到p2中JMenuBar menubar; /定义菜单条对象JMenu editM; /定义菜单对象JMenu viewM;JMenu helpM;JMenuItem editM1; /定义菜单项对象JMenuItem editM2;JMenuItem viewM1;JMenuItem viewM2;JMenuItem viewM3;JMenuItem helpM1;JMenuItem helpM2;JButton bShow; /定义显示按钮对象JButton bMemory; /定义缓存按钮对象JButton bFunction; /定义功能按钮对象JButton bNumber; /定义数字按钮对象JButton bOperator; /定义数字按钮对象double opNum1=0; /定义操作数字,用于运算时存取待运算的数据double opNum2=0; /定义字符串为字符串数组类型,允许增删取改,字符串类型没有相应方法StringBuffer copy=new StringBuffer(20);StringBuffer str=new StringBuffer(); /记录文本区信息StringBuffer memory=new StringBuffer(20); /记录缓存区char currentOp= ; /定义当前次的操作符(+-*/)/* 这是Calculator对象的构造函数*/public Calculator() /*构造窗口和窗口布局*/mainFrame=new JFrame(计算器); /实例化一个窗口Container c=mainFrame.getContentPane(); /该方法返回JFrame的对象/其值是Container类型:不能直接在JFrame对象中加载组件,而应该在/JRootPane对象中加载,通过该方法获取对象实例/*设置菜单栏*/menubar=new JMenuBar(); /创建菜单条mainFrame.setJMenuBar(menubar);/把菜单条加载到窗口editM=new JMenu(编辑(E); viewM=new JMenu(查看(s);helpM=new JMenu(帮助(H);menubar.add(editM);/增加菜单到菜单条editM.setMnemonic(KeyEvent.VK_E); /设置菜单快捷键menubar.add(viewM);viewM.setMnemonic(KeyEvent.VK_V);menubar.add(helpM);helpM.setMnemonic(KeyEvent.VK_H);editM1=new JMenuItem(复制(C) Ctrl+C);/创建菜单项editM2=new JMenuItem(粘贴(P) Ctrl+V);editM.add(editM1); /增加菜单项到菜单editM1.setMnemonic(KeyEvent.VK_C); /设置快捷键editM1.addActionListener(this); /增加菜单项响应editM.add(editM2);editM2.setMnemonic(KeyEvent.VK_P); editM2.addActionListener(this);editM2.setEnabled(false); /设置按键不能用viewM1=new JMenuItem(自然数计算(N);viewM2=new JMenuItem(复数计算(C);viewM3=new JMenuItem(数字分组(I);viewM.add(viewM1);viewM1.setMnemonic(KeyEvent.VK_N); viewM1.addActionListener(this);viewM.add(viewM2);viewM2.setMnemonic(KeyEvent.VK_C); viewM2.addActionListener(this);viewM.addSeparator(); /增加分割线viewM.add(viewM3);viewM3.setMnemonic(KeyEvent.VK_I); viewM3.addActionListener(this);helpM1=new JMenuItem(帮助主题(H);helpM2=new JMenuItem(关于 计算器(A);helpM.add(helpM1);helpM1.setMnemonic(KeyEvent.VK_H); helpM1.addActionListener(this);helpM.addSeparator(); /增加分割线helpM.add(helpM2);helpM2.setMnemonic(KeyEvent.VK_A); helpM2.addActionListener(this);/*设置文本框*/text=new JTextField(0.,24); /初始化显示text.setHorizontalAlignment(JTextField.RIGHT); /设置字段显示方式text.setEditable(false);/设置文本数据是否可被改变p0.add(text); /把文本框加载到面板,可以显示出文本框的长度c.add(p0,BorderLayout.NORTH); /加载到窗口显示/*设置内存键*/c.add(p,BorderLayout.SOUTH);/加载该面板到窗口显示p.setLayout(new BorderLayout(5,3);/设置SOUTH.Panel布局管理器p.add(p1,BorderLayout.WEST); /加载该面板到SOUTH面板的WEST方向p.add(p2,BorderLayout.EAST);/加载该面板到SOUTH面板的EAST方向p1.setLayout(new GridLayout(5,1,3,3); /设置WEST.Panel的布局管理器bShow=new JButton();/设置显示按钮 bShow.setEnabled(false); /设置按钮不响应事件p1.add(bShow);bMemory=new JButton(MC); bMemory.addActionListener(this);/增加按钮的监听器响应事件bMemory.setForeground(Color.RED);/设置按钮文本颜色bMemory.setMargin(new Insets(3,2,3,2);/设置组件周围预留空间p1.add(bMemory); /增加组件到Panel面板bMemory=new JButton(MR); bMemory.addActionListener(this);bMemory.setForeground(Color.RED);bMemory.setMargin(new Insets(3,2,3,2);p1.add(bMemory); bMemory=new JButton(MS); bMemory.addActionListener(this);bMemory.setForeground(Color.RED);bMemory.setMargin(new Insets(3,2,3,2);p1.add(bMemory); bMemory=new JButton(M+); bMemory.addActionListener(this);bMemory.setForeground(Color.RED);bMemory.setMargin(new Insets(3,2,3,2);p1.add(bMemory); /*设置功能键*/p2.setLayout(new BorderLayout(5,1);/设置EAST.Panel的布局管理器p21.setLayout(new GridLayout(1,3,3,0);/定义该面板的布局管理器p2.add(p21,BorderLayout.NORTH); /增加该面板到EAST面板的NORTH方向bFunction=new JButton(BackSpace); bFunction.addActionListener(this);bFunction.setForeground(Color.RED);bFunction.setMargin(new Insets(3,0,3,5);bFunction.setFont(new Font(null,Font.PLAIN,12);p21.add(bFunction); bFunction=new JButton(CE); bFunction.addActionListener(this);bFunction.setForeground(Color.RED);bFunction.setMargin(new Insets(3,2,3,2);p21.add(bFunction); bFunction=new JButton(C); bFunction.addActionListener(this);bFunction.setForeground(Color.RED);bFunction.setMargin(new Insets(3,2,3,2);p21.add(bFunction); /*设置数字运算符号键*/p22.setLayout(new GridLayout(4,5,3,3); /定义该面板的布局管理器p2.add(p22,BorderLayout.SOUTH);/把该面板加载到EAST面板的SOUTH方向bNumber=new JButton(7); /增加右下边面板的按钮bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(8); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(9); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bOperator=new JButton(/); bOperator.addActionListener(this);bOperator.setForeground(Color.RED);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bOperator=new JButton(sqrt); bOperator.addActionListener(this);bOperator.setForeground(Color.BLUE);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bNumber=new JButton(4); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(5); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(6); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bOperator=new JButton(*); bOperator.addActionListener(this);bOperator.setForeground(Color.RED);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bOperator=new JButton(%); bOperator.addActionListener(this);bOperator.setForeground(Color.BLUE);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bNumber=new JButton(1); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(2); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(3); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bOperator=new JButton(-); bOperator.addActionListener(this);bOperator.setForeground(Color.RED);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bOperator=new JButton(1/x); bOperator.addActionListener(this);bOperator.setForeground(Color.BLUE);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bNumber=new JButton(0); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(+/-); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bNumber=new JButton(.); bNumber.addActionListener(this);bNumber.setForeground(Color.BLUE);bNumber.setMargin(new Insets(3,2,3,2);p22.add(bNumber);bOperator=new JButton(+); bOperator.addActionListener(this);bOperator.setForeground(Color.RED);bOperator.setMargin(new Insets(3,2,3,2);p22.add(bOperator);bOperator=new JButton(=); bOperator.addActionListener(this);bOperator.setForeground(Color.RED);bOperat
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年青岛科技大学公开招聘人员(17人)考前自测高频考点模拟试题及答案详解(必刷)
- 2025年杭州市上城区小营街道办事处编外招聘1人考前自测高频考点模拟试题及答案详解(新)
- 2025黑龙江绥化市北林区劳动就业服务中心招聘公益性岗位100人模拟试卷及答案详解(历年真题)
- 2025北京按摩医院部分岗位招聘模拟试卷及答案详解一套
- 2025广东广州市增城区遴选储备村级后备干部考前自测高频考点模拟试题(含答案详解)
- 2025年福建海峡企业管理服务有限公司招聘模拟试卷附答案详解(完整版)
- 2025北京市延庆区教育委员会第二批招聘教师87人模拟试卷及答案详解(网校专用)
- 2025广东广州医学院第一附属医院住院医师规范化培训招生33人(第二批)考前自测高频考点模拟试题带答案详解
- 江苏省盐城市四校2024-2025学年高三上学期10月月考地理试题(解析版)
- 笔尖流出的故事六年级作文600字(5篇)
- 钢制防火门维修合同范本
- 渝22TS02 市政排水管道附属设施标准图集 DJBT50-159
- 幼儿园干冰课件
- pbl教学课件模板
- 凉皮店开业活动方案
- 2025自考行政管理模拟考试试题及答案
- 《胸外心脏按压操作》课件
- 2024-2025学年天津市河西区八年级上学期期中数学试题及答案
- 居家陪护免责合同协议
- 承台大体积砼浇筑方案
- 宣传片管理制度
评论
0/150
提交评论