最新JAVA课程设计报告-心得体会——计算器、文本编辑器.doc
-
资源ID:35273186
资源大小:456KB
全文页数:100页
- 资源格式: DOC
下载积分:15金币
快捷下载

会员登录下载
微信登录下载
三方登录下载:
微信扫一扫登录
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
|
最新JAVA课程设计报告-心得体会——计算器、文本编辑器.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateJAVA课程设计报告-心得体会计算器、文本编辑器JAVA课程设计报告-心得体会计算器、文本编辑器用Java设计计算器calculator内容提要:在本文构造实现了一个计算器擦亮calculator,主要内容包括:calculator的功能需求分析;calculator的基本设计思路和类的划分;calculator的具体实现。关键字:Java、计算器calculator引言:设计实现一个Java应用程序的过程如下:(1) 功能需求分析;(2) 设计和类划分;(3) 代码编写实现。本文就按照这个步骤来实现计算器calculator的制作。正文:1. calculator功能需求分析作为计算器,至少应该具备以下几点功能:(1) 计算器要有GUI界面。(2) 能运算小数,并且不会产生精度损失。(3) 用户可以输入所需计算的数值,可以进行加、减、乘、除四种最基本的运算和混合运算。(4) 允许正负数间的运算。(5) 可以求一个数值的平方及倒数,可以进行阶乘运算。(6) 有菜单栏选项。具体的功能需求和界面我们可以模仿微软公司出品的windowsXP中自带的计算器。如图一: 图一 windows XP 中的计算器界面图2. calculator基本设计思路和类划分基于第1节中提出对于calculator功能需求的分析,对这个应用程序设计划分类如下:(1) calculator:这个类的功能是实现一个框架(2) calculatorFrame:这个类作为主类,实现主要功能:运算,按钮排布,菜单,颜色设置,文本显示3. calculator的具体实现3.1 calculator类的设计calculator用来定义一个计算器的框架1.主要方法下面以表格的形式列出calculator类至少应该具有的方法和功能描述(如表一所示)。表一 calculator类的主要方法方法 功能描述static void main (String args)calculator应用程序的入口,是主方法3.2 calculatorFrame类的设计 calculatorFrame类实现整体功能,包括窗体的初始化、各种用户事件监听和响应(菜单、运算、结果显示等等)。1. 父类和主要接口设计calculator整体 窗口特性继承自JFrame类。为了对用户命令做出响应(如帮助菜单栏弹出窗口),calculatorFrame类必须能够监听到用户的命令,因此设计calculatorFrame类实现ActionListener接口。2. 主要方法下面以表格的形式列出calculatorFrame类至少应该具有的方法和功能描述(如表二所示)。表二 calculatorFrame类的主要方法方法功能描述void actionPerformed(ActionEvent e)重载ActionListener接口中的方法,用于对用户命令进行响应,用户命令包括“帮助”“版权”“说明”以及各个按钮等3. 基本效果图二为calculator的基本效果图。4. 代码分析calculator.java代码如下:/calculator.java/*文件名:calculator.java*说明:calculatorFrame主类,实现主要功能*/导入AWT包import java.awt.*;import java.awt.event.*;/导入SWING包import javax.swing.*;import javax.swing.event.*; class calculator public static void main(String args) calculatorFrame frame = new calculatorFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();/主类calculatorFrameclass calculatorFrame extends JFrame implements ActionListener public calculatorFrame()/设置框架主题及大小setTitle("计算器");setSize(300,235); /将面板置于框架中Container contentPane = getContentPane();setResizable(false); /创建按钮面板 JPanel buttonPanel = new JPanel(); /创建数字键“1” b11=new JButton ("1"); /设置颜色 b11.setForeground(Color. BLUE); /创建事件监听器 b11.addActionListener(this); b12=new JButton ("2"); b12.setForeground(Color. BLUE); b12.addActionListener(this); b13=new JButton ("3"); b13.setForeground(Color. BLUE); b13.addActionListener(this); b6=new JButton ("4"); b6.setForeground(Color. BLUE); b6.addActionListener(this); b7=new JButton ("5"); b7.setForeground(Color. BLUE); b7.addActionListener(this); b8=new JButton ("6"); b8.setForeground(Color. BLUE); b8.addActionListener(this); b1=new JButton ("7"); b1.setForeground(Color. BLUE); b1.addActionListener(this); b2=new JButton ("8"); b2.setForeground(Color. BLUE); b2.addActionListener(this); b3=new JButton ("9"); b3.setForeground(Color. BLUE); b3.addActionListener(this); b16=new JButton ("0"); b16.setForeground(Color. BLUE); b16.addActionListener(this); b17=new JButton ("+/-"); b17.setForeground(Color. BLUE); b17.addActionListener(this); b4=new JButton ("+"); b4.addActionListener(this); b9=new JButton ("-"); b9.addActionListener(this); b14=new JButton ("*"); b14.addActionListener(this); b19=new JButton ("/"); b19.addActionListener(this); b5=new JButton ("1/x"); b5.setForeground(Color. YELLOW); b5.addActionListener(this); b20=new JButton ("="); b20.setForeground(Color. YELLOW); b20.addActionListener(this); /”.”显示不清晰,故采用“。”代替b18=new JButton ("。");b18.setForeground(Color. BLUE); b18.addActionListener(this); b10=new JButton ("x2"); b10.setForeground(Color. YELLOW); b10.addActionListener(this); b15=new JButton ("n!"); b15.setForeground(Color. YELLOW); b15.addActionListener(this); buttonPanel.add(b1); buttonPanel.add(b2); buttonPanel.add(b3); buttonPanel.add(b4); buttonPanel.add(b5); buttonPanel.add(b6); buttonPanel.add(b7); buttonPanel.add(b8); buttonPanel.add(b9); buttonPanel.add(b10); buttonPanel.add(b11); buttonPanel.add(b12); buttonPanel.add(b13); buttonPanel.add(b14); buttonPanel.add(b15); buttonPanel.add(b16); buttonPanel.add(b17); buttonPanel.add(b18); buttonPanel.add(b19); buttonPanel.add(b20); /对按钮面板1进行排版,四行五列 buttonPanel.setLayout(new GridLayout(4,5); /建立容纳文本框的面板JPanel textPanel = new JPanel();addText = new JTextField("0" , 20);addText.addActionListener(this); /文本框从右端开始显示addText.setHorizontalAlignment(JTextField.RIGHT);textPanel.add(addText); /创建按钮面板2 JPanel buttonPanel2=new JPanel(); b21=new JButton ("DEL "); b21.addActionListener(this);b21.setForeground(Color. RED);b22=new JButton ("CE");b22.addActionListener(this);b22.setForeground(Color. RED);b23=new JButton ("C");b23.addActionListener(this);b23.setForeground(Color. RED);buttonPanel2.add(b21);buttonPanel2.add(b22);buttonPanel2.add(b23);buttonPanel2.setLayout(new GridLayout(1,5);/创建菜单栏,并将菜单栏加入到框架中JMenuBar menuBar=new JMenuBar();setJMenuBar(menuBar);/创建“帮助(H) ”菜单JMenu helpMenu=new JMenu("帮助(H)"); helpMenu.setForeground(Color. BLUE); /设置菜单的快捷键helpMenu.setMnemonic('H');menuBar.add(helpMenu); JMenuItem copyrightItem = new JMenuItem("版权", 'b'); copyrightItem.addActionListener(this); JMenuItem explainItem=new JMenuItem("说明",'a'); explainItem.addActionListener(this); /将菜单项添加到“帮助”菜单中 helpMenu.add(copyrightItem); helpMenu.add(explainItem); /设置面板布局 contentPane.add(buttonPanel, BorderLayout.SOUTH);contentPane.add(buttonPanel2, BorderLayout.CENTER);contentPane.add(textPanel, BorderLayout.NORTH); /设置用户点击菜单项和按钮时的响应动作 public void actionPerformed(ActionEvent e) if (e.getActionCommand()="版权") int selection=JOptionPane.showConfirmDialog( calculatorFrame.this, "制作人:20095409 金华日,20095403 章旭,20095397 李伏俊","版权", JOptionPane.DEFAULT_OPTION); if(e.getActionCommand()="说明") int selection=JOptionPane.showConfirmDialog( calculatorFrame.this, "此计算器可进行多种常规运算,欢迎大家使用。","帮助", JOptionPane.DEFAULT_OPTION); if(addText.getText().equals("0") addText.setText(" "); if(e.getSource()=b16) c+="0" addText.setText(""+c); if(e.getSource()=b11) c+="1" addText.setText(""+c); if(e.getSource()=b12) c+="2" addText.setText(""+c); if(e.getSource()=b13) c+="3" addText.setText(""+c); if(e.getSource()=b6) c+="4" addText.setText(""+c); if(e.getSource()=b7) c+="5" addText.setText(""+c); if(e.getSource()=b8) c+="6" addText.setText(""+c); if(e.getSource()=b1) c+="7" addText.setText(""+c); if(e.getSource()=b2) c+="8" addText.setText(""+c); if(e.getSource()=b3) c+="9" addText.setText(""+c); if(e.getSource()=b18) boolean clickable=true; for (int i = 0; i < addText.getText().length(); i+) if ('.' = addText.getText().charAt(i) clickable=false; break; if(clickable) c+="." addText.setText(c); /平方 if(e.getSource()=b10) nu =Double.parseDouble(addText.getText(); num=nu*nu; addText.setText(String.valueOf(num); c="" /倒数 if(e.getSource()=b5) nu =Double.parseDouble(addText.getText(); if(addText.getText().charAt(0)='0'&&addText.getText().length()=1) addText.setText("除数不能为0"); else num=1/nu; addText.setText(String.valueOf(num); c="" /阶乘 if(e.getSource()=b15) c=""num=1; nu =Double.parseDouble(addText.getText(); for (int n=1;n<=nu;n+) num=num*n; addText.setText(String.valueOf(num); /响应“+/-”按钮 if(e.getSource()=b17) String s=addText.getText(); if(addText.getText().charAt(0)='-') addText .setText(""); for (int i=1;i<s.length();i+) char q=s.charAt(i); addText.setText(addText.getText()+q); else addText.setText("-"+s); /将文本框清零 if(e.getActionCommand()="CE") addText.setText("0"); c="" if(e.getActionCommand()="C") addText.setText("0"); c="" /删除文本框中的最后一位数 if (e.getSource()=b21) String g=addText.getText(); addText.setText(""); for (int i=0;i<g.length()-1;i+) char c=g.charAt(i); addText.setText(addText.getText()+ c); /响应“+” “-” “*” “/” 按钮 if(e.getSource()=b4) f='+' nu =Double.parseDouble(addText.getText(); c="" if(e.getSource()=b9) f='-' nu =Double.parseDouble(addText.getText(); c="" if(e.getSource()=b14) f='*' nu =Double.parseDouble(addText.getText(); c="" if(e.getSource()=b19) f='/' nu =Double.parseDouble(addText.getText(); c="" /进行运算,并将结果显示在文本框内 if(e.getSource()=b20) nu2 =Double.parseDouble(addText.getText(); if(f='+') num=nu+nu2; addText.setText(String.valueOf(num); c="" if(f='-') num=nu-nu2; addText.setText(String.valueOf(num); c="" if(f='*') num=nu*nu2; addText.setText(String.valueOf(num); c="" if(f='/') num=nu/nu2; addText.setText(String.valueOf(num); c="" /定义各个变量类型 public JTextField addText; public JButton b1; public JButton b2; public JButton b3; public JButton b4; public JButton b5; public JButton b6; public JButton b7; public JButton b8; public JButton b9; public JButton b10; public JButton b11; public JButton b12; public JButton b13; public JButton b14; public JButton b15; public JButton b16; public JButton b17; public JButton b18; public JButton b19; public JButton b20; public JButton b21; public JButton b22; public JButton b23; public double num,nu,nu2; public char f; String c="" 5. 心得体会Java监听器是Java功能实现的重要组成部分,监听器中的接口也是很重要的一个环节。两个类中的两个类似的功能,调用他们的类动态的决定一种实现,那他们提供一个抽象父类,子类分别实现父类所定义的方法。问题的出现:Java是一种单继承的语言,一般情况下,哪个具体类可能已经有了一个超类,解决是给它的父类加父类,或者给它父类的父类加父类,直到移动到类等级结构的最顶端。这样一来,对一个具体类的可插入性的设计,就变成了对整个等级结构中所有类的修改。在本次实验计算器中每个按钮功能的实现都离不开监听器,监听器的接口以及实现将是java程序的主要内容。计算器菜单栏的实现就是监听器的具体表现,当监听器发现“版权”指令时(e.getActionCommand()="版权"),监听器就会做出反应,弹出一个对话框,显示“制作人:20095409 金华日,20095403 章旭,20095397 李伏俊","版权"”,如果监听器接到指令(e.getSource()=b16)时,就会执行下面的功能(c+="0"addText.setText(""+c);),就是在文本框中输出数字“0”。if(e.getActionCommand()="CE")addText.setText("0"); c=""当监听器接到指令“CE”时,就会自动清除文本框中的内容,初始文本框为“0”。当监听器收到指令为“DEL”时(if(e.getSource()=b21)String g=addText.getText();addText.setText("");for(inti=0;i<g.length()-1;i+)char c=g.charAt(i);addText.setText(addText.getText()+ c);),即是清除在文本框中最后输入的数字,.length()的作用就是计算文本框中数字的长度,每按一次“DEL”,就会清除一个刚输入的数字,一直到清除完文本框中所有的数字。在刚开始实验的时候,按键总是没反应,后来才知道是没有安装监听器的原因,加上监听器后又出现问题了,还是没反应,原因是没有让监听器实现其反应,即actionPerformed这个方法的实现。总之,本次实验就是这样一步步摸索出来的,哪里出现问题,就在哪里寻找原因,还好总算把实验做出来了。JAVA编程文本编辑器editor一、实验目的: