




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验名称:使用java窗体工具做一个简单的计算器一、 实验目的学会使用java窗体控件。主要使用JButton以及JLaber空间。使用监听器会设置各种控件参数学会处理各种数据之间的转换软件微小问题的调试与优化二、 实验内容使用窗体控件来制作一个简易计算器,要能够正确的实现加减乘除等相关功能,并且能优化字符串与整型,浮点型之间的关系,使之输出整数结果末尾不带有零,并且进行一些其他优化,如无限小数,以及二进制与十进制转换而导致的误差,需要进行相关优化。三、 实验步骤importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JTextField;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.font.TextMeasurer;importjava.awt.event.ActionEvent;importjavax.swing.SwingConstants;importjava.awt.Font;publicclassCalculate{StringoldString;intoperate;privateJFrameframe;privateJTextFieldtextField;/***Launchtheapplication.*/publicstaticvoidmain(String[]args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{Calculatewindow=newCalculate();window.frame.setVisible(true);}catch(Exceptione){e.printStackTrace();}}});/***Createtheapplication.*/publicCalculate(){initialize();}/***Initializethecontentsoftheframe.*/privatevoidinitialize(){frame=newJFrame();frame.setTitle("\u8BA1\u7B97\u5668");frame.setBounds(750,300,339,352);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);textField=newJTextField();textField.setEditable(false);textField.setFont(newFont(”宋体",Font.PLAIN,30));textField.setHorizontalAlignment(SwingConstants.RIGHT);textField.setText("0");textField.setBounds(14,13,285,66);frame.getContentPane().add(textField);textField.setColumns(10);JButtonbtnNewButton=newJButton("4");btnNewButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("4");elsetextField.setText(s+"4");}});btnNewButton.setBounds(24,169,59,27);frame.getContentPane().add(btnNewButton);JButtonbutton=newJButton("7");button.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("7");elsetextField.setText(s+"7");});button.setBounds(24,127,59,27);frame.getContentPane().add(button);JButtonbutton_1=newJButton("1");button_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("1");elsetextField.setText(s+"1");}});button_1.setBounds(24,209,59,27);frame.getContentPane().add(button_1);JButtonbutton_2=newJButton("2");button_2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("2");elsetextField.setText(s+"2");}});button_2.setBounds(98,209,59,27);frame.getContentPane().add(button_2);JButtonbutton_3=newJButton("3");button_3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("3");elsetextField.setText(s+"3");}});button_3.setBounds(171,209,59,27);frame.getContentPane().add(button_3);JButtonbutton_4=newJButton("5");button_4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("5");elsetextField.setText(s+"5");}});button_4.setBounds(98,169,59,27);frame.getContentPane().add(button_4);JButtonbutton_5=newJButton("6");button_5.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("6");elsetextField.setText(s+"6");}});button_5.setBounds(171,169,59,27);frame.getContentPane().add(button_5);JButtonbutton_6=newJButton("8");button_6.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("8");elsetextField.setText(s+"8");}});button_6.setBounds(97,127,59,27);frame.getContentPane().add(button_6);JButtonbutton_7=newJButton("9");button_7.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("9");elsetextField.setText(s+"9");}});button_7.setBounds(171,127,59,27);frame.getContentPane().add(button_7);JButtonbutton_8=newJButton("0");button_8.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(s.equals("0"))textField.setText("0");elsetextField.setText(s+"0");}});button_8.setBounds(24,248,59,27);frame.getContentPane().add(button_8);JButtonbutton_9=newJButton(".");button_9.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=textField.getText();if(!s.contains("."))textField.setText(s+".");}});button_9.setBounds(98,249,59,27);frame.getContentPane().add(button_9);JButtonbutton_10=newJButton("\u00B1");button_10.setBounds(171,249,59,27);frame.getContentPane().add(button_10);JButtonbutton_11=newJButton("+");button_11.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){oldString=textField.getText();textField.setText("0");operate=1;}});button_11.setBounds(240,87,59,27);frame.getContentPane().add(button_11);JButtonbutton_12=newJButton("-");button_12.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){oldString=textField.getText();textField.setText("0");operate=2;}});button_12.setBounds(240,127,59,27);frame.getContentPane().add(button_12);JButtonbutton_13=newJButton("*");button_13.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){oldString=textField.getText();textField.setText("0");operate=3;}});button_13.setBounds(240,169,59,27);frame.getContentPane().add(button_13);JButtonbutton_14=newJButton("/");button_14.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){oldString=textField.getText();textField.setText("0");operate=4;}});button_14.setBounds(240,209,59,27);frame.getContentPane().add(button_14);JButtonbtnNewButton_1=newJButton("C");btnNewButton_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){textField.setText("0");oldString="0";}});btnNewButton_1.setBounds(24,87,59,27);frame.getContentPane().add(btnNewButton_1);JButtonbtnCe=newJButton("CE");btnCe.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){}});btnCe.setBounds(98,87,59,27);frame.getContentPane().add(btnCe);JButtonbutton_16=newJButton("<-");button_16.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Stringss=textField.getText();if(ss.length()==1)textField.setText("0");elsetextField.setText(ss.substring(0,ss.length()-1));}});button_16.setBounds(171,87,59,27);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 45211.3-2025小麦抗病虫性评价技术规程第3部分:秆锈病
- 工程采购合同(31篇)
- 项目执行过程中遇到问题的解析与应对方案
- 电信行业网络优化与数据安全保障方案
- 塑料产品买卖合同书
- 股份制企业合同审查与管理文书
- 物流运输承包合同
- 房地产合作销售开发协议书
- 管桩施工劳务合同
- 能源行业资源整合合作协议
- 《不一样的物体作业设计方案-2023-2024学年科学大象版》
- (2024年)发生输液反应时应急预案及处理流程
- 能源经济学导论
- 《社区康复》课件-第七章 脑瘫患儿的社区康复实践
- 白酒包装盒工艺
- 水痘预防课件
- 《管理统计学》教学课件
- 新人教版小学二年级下册美术电子教案(全)
- 公司人事招聘面试技巧培训完整版课件两篇
- 第1课《立足时代+志存高远》第1框《时代为我搭舞台》【中职专用】《心理健康与职业生涯》(高教版2023基础模块)
- 出国劳务派遣合同(专业版)电子版正规范本(通用版)
评论
0/150
提交评论