




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.awt.List;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class MessageBoard extends Framepublic static void main(String args) final Frame jf = new Frame(计算器); jf.setSize(250,275); /设置窗口长宽jf.setResizable(false); /设置窗体大小不可更改 /*添加文本框*/jf.setLayout(null); /组件不会随窗口的大小而改变final JTextField jt1 = new JTextField();jt1.setHorizontalAlignment(JTextField.RIGHT); /设置文本框右对齐jt1.setEditable(false); /设置文本框不可编辑jt1.setBounds(10,50,233,20);jf.add(jt1);/*添加按钮*/设置0键final JButton b0 = new JButton(0);b0.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+0););/设置1键final JButton b1 = new JButton(1);b1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)jt1.setText(jt1.getText()+1); );/设置2键final JButton b2 = new JButton(2);b2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+2); );/设置3键final JButton b3 = new JButton(3);b3.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /监听器接口定义好的 ActionEvent类型 e是参数名 jt1.setText(jt1.getText()+3); );/设置4键final JButton b4 = new JButton(4);b4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+4); );/设置5键final JButton b5 = new JButton(5);b5.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+5); );/设置6键final JButton b6 = new JButton(6);b6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+6); );/设置7键final JButton b7 = new JButton(7);b7.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+7); );/设置8键final JButton b8 = new JButton(8);b8.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+8); );/设置9键final JButton b9 = new JButton(9);b9.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+9); );/设置加号键final JButton badd = new JButton(+);badd.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+); );/设置减号键final JButton bminus = new JButton(-);bminus.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+-); );/设置乘号键final JButton bmulti = new JButton(*);bmulti.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+*); );/设置除号键final JButton bdivide = new JButton(/);bdivide.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+/); );/设置等号键final JButton bequal = new JButton(=);bequal.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName(js);tryString reslut = se.eval(jt1.getText().toString();jt1.setText(reslut); catch (ScriptException evt)JOptionPane.showMessageDialog(null,表达式不合法!); );/设置小数点号键final JButton bpoint = new JButton(.);bpoint.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+.); );/设置左括号键final JButton bleft = new JButton();bleft.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+(); );/设置右括号键final JButton bright = new JButton();bright.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+); );/设置负号键final JButton bfuhao = new JButton(+-);bfuhao.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(jt1.getText()+-); );/设置清0键final JButton bce = new JButton(CE);bce.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) jt1.setText(); );/设置退格键final JButton bback = new JButton(Backspace);bback.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String message;int leng=jt1.getText().length();message=jt1.getText();if(leng=0)JOptionPane.showMessageDialog(null, 以无输入内容); /当退格到没有输入是弹出消息框提示elsejt1.setText(message.substring(0,leng-1); /设置文本框的内容没点一次退格减一个数 );/按钮布局 b0.setBounds(10,228,45,35);b1.setBounds(10,190,45,35); /前2个代表x,y坐标 后面的是 宽和高b2.setBounds(57,190,45,35);b3.setBounds(104,190,45,35);b4.setBounds(10,151,45,35);b5.setBounds(57,151,45,35);b6.setBounds(104,151,45,35);b7.setBounds(10,112,45,35);b8.setBounds(57,112,45,35);b9.setBounds(104,112,45,35);badd.setBounds(151,228,45,35);bminus.setBounds(151,190,45,35);bmulti.setBounds(151,151,45,35);bdivide.setBounds(151,112,92,35);bequal.setBounds(198,228,45,35);bpoint.setBounds(104,228,45,35);bleft.setBounds(198,151,45,35);bright.setBounds(198,190,45,35);bfuhao.setBounds(57,228,45,35);bce.setBounds(128,75,115,35);bback.setBounds(10,75,115,35);/向jf中添加控件jf.add(b0);jf.add(b1);jf.add(b2);jf.add(b3);jf.add(b4);jf.add(b5);jf.add(b6);jf.add(b7);jf.add(b8);jf.add(b9);jf.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 离婚财产分割与共同债务处理补充协议样本
- 租赁别墅退房协议范本及环境恢复要求
- 琴行专业教师团队聘用及教学成果分享协议
- 离婚协议中子女抚养权变更及监护权调整执行细节合同
- 互联网科技公司股权转让与用户数据共享合同
- 课件制作大赛开场
- 汽车测试技术与试验试题及答案
- 辅警安全知识培训心得
- 工商银行2025眉山市小语种岗笔试题及答案
- 工商银行2025柳州市小语种岗笔试题及答案
- (教科2024版)科学三年级上册2.1 水到哪里去了 课件(新教材)
- 2025国家能源集团招聘笔试历年参考题库附带答案详解
- 编织课件教学课件
- 认证机构保密管理办法
- 土建类安全员C2模拟试题及参考答案
- 公司财务报表分析技巧与方法
- 新课标(水平三)体育与健康《篮球》大单元教学计划及配套教案(18课时)
- 建筑工人临时用工协议书
- 2022年《上海市初中语文课程终结性评价指南》中规定的个文言实词
- 苏教版四年级上册科学全册课件
- 北京中考英语词汇表1600词汇+词组
评论
0/150
提交评论