JAVA课程设计报告心得体会计算器文本编辑器.docx

上传人:b****2 文档编号:1818232 上传时间:2023-05-01 格式:DOCX 页数:36 大小:180.24KB
下载 相关 举报
JAVA课程设计报告心得体会计算器文本编辑器.docx_第1页
第1页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第2页
第2页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第3页
第3页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第4页
第4页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第5页
第5页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第6页
第6页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第7页
第7页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第8页
第8页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第9页
第9页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第10页
第10页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第11页
第11页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第12页
第12页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第13页
第13页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第14页
第14页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第15页
第15页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第16页
第16页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第17页
第17页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第18页
第18页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第19页
第19页 / 共36页
JAVA课程设计报告心得体会计算器文本编辑器.docx_第20页
第20页 / 共36页
亲,该文档总共36页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

JAVA课程设计报告心得体会计算器文本编辑器.docx

《JAVA课程设计报告心得体会计算器文本编辑器.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计报告心得体会计算器文本编辑器.docx(36页珍藏版)》请在冰点文库上搜索。

JAVA课程设计报告心得体会计算器文本编辑器.docx

JAVA课程设计报告心得体会计算器文本编辑器

用Java设计计算器calculator

内容提要:

在本文构造实现了一个计算器擦亮calculator,主要内容包括:

calculator的功能需求分析;calculator的基本设计思路和类的划分;calculator的具体实现。

关键字:

Java、计算器calculator

引言:

设计实现一个Java应用程序的过程如下:

(1)功能需求分析;

(2)设计和类划分;

(3)代码编写实现。

本文就按照这个步骤来实现计算器calculator的制作。

正文:

1.calculator功能需求分析

作为计算器,至少应该具备以下几点功能:

(1)计算器要有GUI界面。

(2)能运算小数,并且不会产生精度损失。

(3)用户可以输入所需计算的数值,可以进行加、减、乘、除四种最基本的运算和混合运算。

(4)允许正负数间的运算。

(5)可以求一个数值的平方及倒数,可以进行阶乘运算。

(6)有菜单栏选项。

具体的功能需求和界面我们可以模仿微软公司出品的windowsXP中自带的计算器。

如图一:

图一windowsXP中的计算器界面图

2.calculator基本设计思路和类划分

基于第1节中提出对于calculator功能需求的分析,对这个应用程序设计划分类如下:

(1)calculator:

这个类的功能是实现一个框架

(2)calculatorFrame:

这个类作为主类,实现主要功能:

运算,按钮排布,菜单,颜色设置,文本显示

3.calculator的具体实现

3.1calculator类的设计

calculator用来定义一个计算器的框架

1.主要方法

下面以表格的形式列出calculator类至少应该具有的方法和功能描述(如表一所示)。

表一calculator类的主要方法

方法

功能描述

staticvoidmain(Stringargs[])

calculator应用程序的入口,是主方法

3.2calculatorFrame类的设计

calculatorFrame类实现整体功能,包括窗体的初始化、各种用户事件监听和响应(菜单、运算、结果显示等等)。

1.父类和主要接口

设计calculator整体窗口特性继承自JFrame类。

为了对用户命令做出响应(如帮助菜单栏弹出窗口),calculatorFrame类必须能够监听到用户的命令,因此设计calculatorFrame类实现ActionListener接口。

2.主要方法

下面以表格的形式列出calculatorFrame类至少应该具有的方法和功能描述(如表二所示)。

表二calculatorFrame类的主要方法

方法

功能描述

voidactionPerformed(ActionEvente)

重载ActionListener接口中的方法,用于对用户命令进行响应,用户命令包括“帮助”“版权”“说明”以及各个按钮等

3.基本效果

图二为calculator的基本效果图。

4.代码分析

calculator.java代码如下:

//calculator.java

/*

*文件名:

calculator.java

*说明:

calculatorFrame主类,实现主要功能

*/

//导入AWT包

importjava.awt.*;

importjava.awt.event.*;

//导入SWING包

importjavax.swing.*;

importjavax.swing.event.*;

classcalculator

{

publicstaticvoidmain(String[]args)

{

calculatorFrameframe=newcalculatorFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.show();

}

}

//主类calculatorFrame

classcalculatorFrameextendsJFrame

implementsActionListener

{

publiccalculatorFrame()

{

//设置框架主题及大小

setTitle("计算器");

setSize(300,235);

//将面板置于框架中

ContainercontentPane=getContentPane();

setResizable(false);

//创建按钮面板

JPanelbuttonPanel=newJPanel();

//创建数字键“1”

b11=newJButton("1");

//设置颜色

b11.setForeground(Color.BLUE);

//创建事件监听器

b11.addActionListener(this);

b12=newJButton("2");

b12.setForeground(Color.BLUE);

b12.addActionListener(this);

b13=newJButton("3");

b13.setForeground(Color.BLUE);

b13.addActionListener(this);

b6=newJButton("4");

b6.setForeground(Color.BLUE);

b6.addActionListener(this);

b7=newJButton("5");

b7.setForeground(Color.BLUE);

b7.addActionListener(this);

b8=newJButton("6");

b8.setForeground(Color.BLUE);

b8.addActionListener(this);

b1=newJButton("7");

b1.setForeground(Color.BLUE);

b1.addActionListener(this);

b2=newJButton("8");

b2.setForeground(Color.BLUE);

b2.addActionListener(this);

b3=newJButton("9");

b3.setForeground(Color.BLUE);

b3.addActionListener(this);

b16=newJButton("0");

b16.setForeground(Color.BLUE);

b16.addActionListener(this);

b17=newJButton("+/-");

b17.setForeground(Color.BLUE);

b17.addActionListener(this);

b4=newJButton("+");

b4.addActionListener(this);

b9=newJButton("-");

b9.addActionListener(this);

b14=newJButton("*");

b14.addActionListener(this);

b19=newJButton("/");

b19.addActionListener(this);

b5=newJButton("1/x");

b5.setForeground(Color.YELLOW);

b5.addActionListener(this);

b20=newJButton("=");

b20.setForeground(Color.YELLOW);

b20.addActionListener(this);

//”.”显示不清晰,故采用“。

”代替

b18=newJButton("。

");

b18.setForeground(Color.BLUE);

b18.addActionListener(this);

b10=newJButton("x^2");

b10.setForeground(Color.YELLOW);

b10.addActionListener(this);

b15=newJButton("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(newGridLayout(4,5));

//建立容纳文本框的面板

JPaneltextPanel=newJPanel();

addText=newJTextField("0",20);

addText.addActionListener(this);

//文本框从右端开始显示

addText.setHorizontalAlignment(JTextField.RIGHT);

textPanel.add(addText);

//创建按钮面板2

JPanelbuttonPanel2=newJPanel();

b21=newJButton("DEL");

b21.addActionListener(this);

b21.setForeground(Color.RED);

b22=newJButton("CE");

b22.addActionListener(this);

b22.setForeground(Color.RED);

b23=newJButton("C");

b23.addActionListener(this);

b23.setForeground(Color.RED);

buttonPanel2.add(b21);

buttonPanel2.add(b22);

buttonPanel2.add(b23);

buttonPanel2.setLayout(newGridLayout(1,5));

//创建菜单栏,并将菜单栏加入到框架中

JMenuBarmenuBar=newJMenuBar();

setJMenuBar(menuBar);

//创建“帮助(H)”菜单

JMenuhelpMenu=newJMenu("帮助(H)");

helpMenu.setForeground(Color.BLUE);

//设置菜单的快捷键

helpMenu.setMnemonic('H');

menuBar.add(helpMenu);

JMenuItemcopyrightItem=newJMenuItem("版权",'b');

copyrightItem.addActionListener(this);

JMenuItemexplainItem=newJMenuItem("说明",'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);

}

//设置用户点击菜单项和按钮时的响应动作

publicvoidactionPerformed(ActionEvente)

{

if(e.getActionCommand()=="版权")

{

intselection=JOptionPane.showConfirmDialog(

calculatorFrame.this,

"制作人:

20095409金华日,20095403章旭,20095397李伏俊","版权",

JOptionPane.DEFAULT_OPTION);

}

if(e.getActionCommand()=="说明")

{

intselection=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)

{

booleanclickable=true;

for(inti=0;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(intn=1;n<=nu;n++)

{

num=num*n;

addText.setText(String.valueOf(num));

}

}

//响应“+/-”按钮

if(e.getSource()==b17)

{Strings=addText.getText();

if(addText.getText().charAt(0)=='-')

{

addText.setText("");

for(inti=1;i

{charq=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)

{

Stringg=addText.getText();

addText.setText("");

for(inti=0;i

{

charc=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="";}

}

}

//定义各个变量类型

publicJTextFieldaddText;

publicJButtonb1;

publicJButtonb2;

publicJButtonb3;

publicJButtonb4;

publicJButtonb5;

publicJButtonb6;

publicJButtonb7;

publicJButtonb8;

publicJButtonb9;

publicJButtonb10;

publicJButtonb11;

publicJButtonb12;

publicJButtonb13;

publicJButtonb14;

publicJButtonb15;

publicJButtonb16;

publicJButtonb17;

publicJButtonb18;

publicJButtonb19;

publicJButtonb20;

publicJButtonb21;

publicJButtonb22;

publicJButtonb23;

publicdoublenum,nu,nu2;

publiccharf;

Stringc="";

}

5.心得体会

Java监听器是Java功能实现的重要组成部分

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2