JAVA实训总结_1747

上传人:s9****2 文档编号:469861302 上传时间:2023-01-14 格式:DOC 页数:12 大小:421.50KB
返回 下载 相关 举报
JAVA实训总结_1747_第1页
第1页 / 共12页
JAVA实训总结_1747_第2页
第2页 / 共12页
JAVA实训总结_1747_第3页
第3页 / 共12页
JAVA实训总结_1747_第4页
第4页 / 共12页
JAVA实训总结_1747_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《JAVA实训总结_1747》由会员分享,可在线阅读,更多相关《JAVA实训总结_1747(12页珍藏版)》请在金锄头文库上搜索。

1、JAVA程序设计课程实训报告一、实训目的知识目标1了解图形用户界面的编程思路及方法;2了解事件及事件处理机制;3掌握常用的图形用户界面组件;4掌握容器布局的设置方法及组件的添加方法;5掌握常见事件类型及事件处理方法;能力目标1与客户沟通的基本能力;2团队协作的基本能力;3编程的良好习惯;4软件调试和测试的能力;5查阅文献的能力;二、项目设置:1、实训内容利用所学 Java 程序设计语言实现一个图形化的计算器。2、实训要求1程序运行正常无误,系统层次清晰合理;2实训内容里的要求必须要全面实现;3实训过程中要仔细操作,细心观察,勤于思考,注意记录;4实训报告要做到文字通畅、论点正确、论述有据;5要

2、求学生在指导教师的指导下,独立完成实训的全部内容;6在规定时间内完成实训的全部任务,并按规定格式上交实训报告;三、实训设备硬件:个人电脑、软件: JCreator Pro 3.50、四、实训过程以上的图表示实训编写的程序没有错误,可以正常的运行以上的图是运行程序时所显示出来的小小计算机的界面为了实际的运行一下计算机的功能,此时输入1 加 2此时计算机上显示的是数字1 和数字 2 相加的结果, 3.为了实际的运行一下计算机的功能,此时输入数字1乘数字 2此时计算机上显示的是数字1 和数字 2 相乘的结果, 2.因为上述的两个小算数成功的运算出正确的结果所以此时关闭小小简单计算机的界面此时出现的编

3、程图面为:计算机程序的源码:/class CalculatorPanel extends JPanel计算机界面private JLabel display;private JPanel panel;private double result;/用以存放最后的计算结果private String lastCommand;/计算你选择的运算方式private boolean start;public CalculatorPanel()setLayout(new BorderLayout();result=0;lastCommand=;start=true;display=new JLabel(0

4、,SwingConstants.RIGHT);display.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5,5,5,5);/createCompoundBorder意为设置组合边框, createLineBorder意为设置线边框, createEmptyBorder意为设置一个空的边框,因为是空边框在此处是看不见的,目的是为了占位子add(display,BorderLayout.NORTH);

5、ActionListener insert=new InsertAction();ActionListener command=new CommandAction();panel=new JPanel();/计算机按钮的布局panel.setLayout(new GridLayout(4,4)/添加按钮及相应的事件监听addButton(7,insert);addButton(8,insert);addButton(9,insert);addButton(/,command);addButton(4,insert);addButton(5,insert);addButton(6,insert)

6、;addButton(*,command);addButton(1,insert);addButton(2,insert);addButton(3,insert);addButton(-,command);addButton(0,insert);addButton(.,insert);addButton(=,command);addButton(+,command);add(panel,BorderLayout.CENTER);/ 点击按钮会触发 ActionEvent 事件,事件监听器接口 ActionListener, 接口实现的方法 ActionPerformed ,注册事件的监听器 a

7、dd ActionListener.private void addButton(String label,ActionListener listener)JButton button=new JButton(label); button.addActionListener(listener); panel.add(button); /按钮监听器类,用于显示private class InsertAction implements ActionListenerpublic void actionPerformed(ActionEvent event)String input=event.get

8、ActionCommand();if(start)display.setText();start=false;display.setText(display.getText()+input);/ 按钮监听器类,用于监听计算private class CommandAction implements ActionListenerpublic void actionPerformed(ActionEvent event)String command=event.getActionCommand();/ 获取你点击的那个按钮 if(start)if(command.equals(=)display.

9、setText(command);start=false;elselastCommand=command;elsecalculate(Double.parseDouble(display.getText();/ 进行数据计算,输入的是字符串,所以要进行强制类型的转换lastCommand=command;start=true;/ 计算类两个方法: setTest()设置标签中的内容, getTest()获取标签中的方法;public void calculate(double x)if(lastCommand.equals(+)result+=x;else if(lastCommand.equ

10、als(-)result-=x;else if(lastCommand.equals(*)result*=x;else if(lastCommand.equals(/)result/=x;else if(lastCommand.equals(=)result=x;display.setText(+result);class CalculatorFrame extends JFramepublic CalculatorFrame()setTitle(计算机 );Container contentPane=getContentPane();CalculatorPanel panel=new CalculatorPanel();contentPane.add(panel);public class CalculatorExamplepublic static void main(String args)CalculatorFrame frame=new CalculatorFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fra

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 幼儿教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号