java实现简单的计算器word文档良心出品Word文档格式.docx

上传人:b****1 文档编号:4503242 上传时间:2023-05-03 格式:DOCX 页数:23 大小:239.23KB
下载 相关 举报
java实现简单的计算器word文档良心出品Word文档格式.docx_第1页
第1页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第2页
第2页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第3页
第3页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第4页
第4页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第5页
第5页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第6页
第6页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第7页
第7页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第8页
第8页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第9页
第9页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第10页
第10页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第11页
第11页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第12页
第12页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第13页
第13页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第14页
第14页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第15页
第15页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第16页
第16页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第17页
第17页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第18页
第18页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第19页
第19页 / 共23页
java实现简单的计算器word文档良心出品Word文档格式.docx_第20页
第20页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java实现简单的计算器word文档良心出品Word文档格式.docx

《java实现简单的计算器word文档良心出品Word文档格式.docx》由会员分享,可在线阅读,更多相关《java实现简单的计算器word文档良心出品Word文档格式.docx(23页珍藏版)》请在冰点文库上搜索。

java实现简单的计算器word文档良心出品Word文档格式.docx

1.4.1π:

用Math类中的PI来实现

1.4.2自然对数e:

用Math类中的E来实现

2设计

2.1用户界面设计

2.1.1该计算器程序的设计:

用户界面包括Swing组件,不过程序中大都使用的是AWT组件.

2.1.2在AWT组件,

(1)使用了面板和按钮组:

JPanelpanel1,panel2,panel3,panel4;

ButtonGroupbgb;

(2)由于该组件按钮较多,设计一个方法简化按钮的设置

voidaddButton(JPanelpanel,Stringname,ActionListeneraction,Colorcolor){

JButtonbt=newJButton(name);

panel.add(bt);

//在面板上增加按钮

bt.setForeground(color);

//设置字体颜色

bt.addActionListener(action);

//增加监听事件

2.1.3这个界面设计中包含了五个接口,分别控制运算符,数字,清除,存储功能和小数点的输入

classSignsimplementsActionListener

classNumimplementsActionListener

classClearimplementsActionListener

classMemoryimplementsActionListener

classDotimplementsActionListener

2.1.4程序设计中,使用了布局管理:

(1)用边布局管理器(BorderLayout)设置计算器容器各方位组件:

panel4=newJPanel(newBorderLayout(5,5))

panel4.add(panel1,BorderLayout.NORTH);

panel4.add(panel2,BorderLayout.CENTER);

this.add(tf,BorderLayout.NORTH);

this.add(panel3,BorderLayout.WEST);

(2)用网格布局管理器(GridLayout)设置面板

panel1=newJPanel(newGridLayout(1,3,10,10));

panel2=newJPanel(newGridLayout(5,6,5,5));

panel3=newJPanel(newGridLayout(5,1,5,5));

2.2概要设计

计算器的整个程序包括:

一个Calculator类

2.2.1它的功能是使用图形用户来实现计算器的界面设计和运算功能以及一些科学运算方法.

类中包含了Calculator()构造方法,和其他重要的成员方法,最终在main方法中实例化一个ca对象实现计算器的功能。

3实现

packagecalculator;

 

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjava.text.DecimalFormat;

publicclassCalculatorextendsJFrame{

JTextFieldtf;

JPanelpanel1,panel2,panel3,panel4;

JMenuBarmyBar;

JMenumenu1,menu2,menu3;

JMenuItemeditItem1,editItem2,help1,help2,help3;

JRadioButtonMenuItemseeItem1,seeItem2;

//单选框

JCheckBoxMenuItemseeItem3;

//复选框

ButtonGroupbgb;

booleanIfResult=true,flag=false;

Stringoper="

="

;

doubleresult=0,memory=0;

DecimalFormatdf;

Calculator(){

super("

科学计算器"

);

//设置标题栏

df=newDecimalFormat("

#.####"

//保留四位小数

this.setLayout(newBorderLayout(10,5));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

//设置每个JPanel的参数

panel1=newJPanel(newGridLayout(1,3,10,10));

panel4=newJPanel(newBorderLayout(5,5));

/**

*菜单栏

*/

myBar=newJMenuBar();

menu1=newJMenu("

编辑(E)"

menu2=newJMenu("

查看(V)"

menu3=newJMenu("

帮助(H)"

menu1.setFont(newFont("

宋体"

Font.PLAIN,12));

menu2.setFont(newFont("

menu3.setFont(newFont("

*编辑栏

editItem1=newJMenuItem("

复制(C)Ctrl+C"

editItem2=newJMenuItem("

粘贴(P)Ctrl+V"

editItem1.setFont(newFont("

Font.PLAIN,12));

editItem2.setFont(newFont("

*查看栏

seeItem1=newJRadioButtonMenuItem("

科学型(T)"

seeItem2=newJRadioButtonMenuItem("

标准型(S)"

seeItem3=newJCheckBoxMenuItem("

数字分组(I)"

seeItem1.setFont(newFont("

seeItem2.setFont(newFont("

seeItem3.setFont(newFont("

*帮助栏

help1=newJMenuItem("

帮助主题(H)"

help2=newJMenuItem("

关于计算器(A)"

help1.setFont(newFont("

help2.setFont(newFont("

bgb=newButtonGroup();

//选项组

menu1.add(editItem1);

menu1.add(editItem2);

menu2.add(seeItem1);

menu2.add(seeItem2);

menu2.addSeparator();

//添加一条分割线

menu2.add(seeItem3);

menu3.add(help1);

menu3.addSeparator();

menu3.add(help2);

myBar.add(menu1);

myBar.add(menu2);

myBar.add(menu3);

this.setJMenuBar(myBar);

*文本域,即为计算器的屏幕显示区域

tf=newJTextField();

tf.setEditable(false);

//文本区域不可编辑

tf.setBackground(Color.white);

//文本区域的背景色

tf.setHorizontalAlignment(JTextField.RIGHT);

//文字右对齐

tf.setText("

0"

init();

//对计算器进行初始化

}

/**

*初始化操作

*添加按钮

*/

voidinit(){

addButton(panel1,"

Backspace"

newClear(),Color.red);

CE"

C"

addButton(panel2,"

1/x"

newSigns(),Color.magenta);

ln"

7"

newNum(),Color.blue);

8"

9"

÷

"

newSigns(),Color.red);

n!

sqrt"

4"

5"

6"

×

sin"

x^2"

1"

2"

3"

-"

cos"

x^3"

-/+"

newClear(),Color.blue);

."

newDot(),Color.blue);

+"

tan"

%"

π"

newNum(),Color.orange);

e"

′″"

newSigns(),Color.orange);

addButton(panel3,"

MC"

newMemory(),Color.red);

MS"

MR"

M-"

M+"

panel4.add(panel1,BorderLayout.NORTH);

this.add(panel4);

pack();

//调整此窗口的大小,以适合其子组件的首选大小和布局

this.setResizable(false);

//窗口不可改变大小

this.setLocation(300,200);

*统一设置按钮的的使用方式

voidaddButton(JPanelpanel,Stringname,ActionListeneraction,Colorcolor){

bt.addActionListener(action);

*计算器的基础操作(+-×

÷

voidgetResult(doublex){

if(oper.equals("

)){result+=x;

}

elseif(oper.equals("

)){result-=x;

)){result*=x;

)){result/=x;

)){result=x;

tf.setText(df.format(result));

*运算符号的事件监听

classSignsimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

/*

*用ActionEvent对象的getActionCommand()方法取得与引发事件对象的字符串

*/

Stringstr=e.getActionCommand();

/*sqrt求平方根*/

if(str.equals("

)){

doublei=Double.parseDouble(tf.getText());

//将tf文本框中已经输入的数字字符串转化为double类型的变量

if(i>

=0){

/*

*String.valueOf()转换为字符串

*df.format()按要求保留四位小数

*Math.sqrt()求算数平方根

*/

tf.setText(String.valueOf(df.format(Math.sqrt(i))));

}

else{

tf.setText("

负数不能开平方根"

}

/**

*用log函数求自然对数

elseif(str.equals("

0){

tf.setText(String.valueOf(df.format(Math.log(i))));

}else{

负数不能求对数"

/*%求百分比*/

tf.setText(String.valueOf(df.format(Double.parseDouble(tf.getText())/100)));

/*1/x求倒数*/

if(Double.parseDouble(tf.getText())==0){

除数不能为零"

tf.setText(String.valueOf(df.format(1/Double.parseDouble(tf.getText()))));

/*sin求正弦函数*/

tf.setText(String.valueOf(df.format(Math.sin(i))));

/*cos求余弦函数*/

tf.setText(String.valueOf(df.format(Math.cos(i))));

/*tan求正切函数*/

tf.setText(String.valueOf(df.format(Math.tan(i))));

/*n!

求阶乘*/

if((i%2==0)||(i%2==1))//判断为整数放进行阶乘操作

{

intj=(int)i;

//强制类型转换

intresult=1;

for(intk=1;

k<

=j;

k++)

result*=k;

tf.setText(String.valueOf(result));

else

无法进行阶乘"

/*x^2求平方*/

tf.setText(String.valueOf(df.format(i*i)));

/*x^3求立方*/

tf.setText(String.valueOf(df.format(i*i*i)));

/*′″角度转换*/

*将角度值转换成弧度值,方便三角函数的计算

tf.setText(String.valueOf(i/180*Math.PI));

else{

if(flag)

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

当前位置:首页 > 自然科学 > 物理

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

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