科学计算器程序Java代码Word文档格式.docx

上传人:b****3 文档编号:7166004 上传时间:2023-05-08 格式:DOCX 页数:16 大小:50.63KB
下载 相关 举报
科学计算器程序Java代码Word文档格式.docx_第1页
第1页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第2页
第2页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第3页
第3页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第4页
第4页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第5页
第5页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第6页
第6页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第7页
第7页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第8页
第8页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第9页
第9页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第10页
第10页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第11页
第11页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第12页
第12页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第13页
第13页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第14页
第14页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第15页
第15页 / 共16页
科学计算器程序Java代码Word文档格式.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

科学计算器程序Java代码Word文档格式.docx

《科学计算器程序Java代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《科学计算器程序Java代码Word文档格式.docx(16页珍藏版)》请在冰点文库上搜索。

科学计算器程序Java代码Word文档格式.docx

privateButtonGroupbgb;

privateStringback;

privatebooleanIfResult=true,flag=false;

privateStringoper="

="

;

privatedoubleresult=0;

privateNumnumActionListener;

privateDecimalFormatdf;

publicCalucator(){

super("

科学计算器"

);

//设置标题栏

df=newDecimalFormat("

#.####"

//保留四位小数

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

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

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

//5行6列

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

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);

numActionListener=newNum();

//实现数字监听

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

tf=newJTextField();

tf.setEditable(false);

//文本区域不可编辑

tf.setBackground(Color.white);

//文本区域的背景色

tf.setHorizontalAlignment(JTextField.RIGHT);

//文字右对齐

tf.setText("

0"

tf.setBorder(BorderFactory.createLoweredBevelBorder());

init();

//对计算器进行初始化

}

/**

*初始化操作

*添加按钮

*/

privatevoidinit(){

addButton(panel1,"

Backspace"

newClear(),Color.red);

CE"

C"

addButton(panel2,"

1/x"

newSigns(),Color.magenta);

log"

7"

numActionListener,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"

%"

π"

numActionListener,Color.orange);

e"

′″"

newSigns(),Color.orange);

 

JButtonbtns=newJButton("

计算器"

btns.setBorder(BorderFactory.createLoweredBevelBorder());

btns.setEnabled(false);

//按钮不可操作

btns.setPreferredSize(newDimension(20,20));

panel3.add(btns);

//加入按钮

addButton(panel3,"

MC"

null,Color.red);

MR"

MS"

M+"

panel4.add(panel1,BorderLayout.NORTH);

panel4.add(panel2,BorderLayout.CENTER);

this.add(tf,BorderLayout.NORTH);

this.add(panel3,BorderLayout.WEST);

this.add(panel4);

pack();

this.setResizable(false);

//窗口不可改变大小

this.setLocation(300,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

*@parampanel

*@paramname

*@paramaction

*@paramcolor

privatevoidaddButton(JPanelpanel,Stringname,ActionListeneraction,Colorcolor){

JButtonbt=newJButton(name);

panel.add(bt);

//在面板上增加按钮

bt.setForeground(color);

//设置前景(字体)颜色

bt.addActionListener(action);

//增加监听事件

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

÷

*@paramx

privatevoidgetResult(doublex){

if(oper=="

){result+=x;

}

elseif(oper=="

){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());

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(df.format(Double.parseDouble(tf.getText())/100));

/*1/x求倒数*/

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

除数不能为零"

tf.setText(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){

IfResult=false;

if(IfResult){

oper=str;

}else{

getResult(Double.parseDouble(tf.getText()));

IfResult=true;

}

*清除按钮的事件监听

classClearimplementsActionListener{

if(str=="

){

tf.setText("

IfResult=true;

result=0;

}elseif(str=="

doublei=0-Double.parseDouble(tf.getText().trim());

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

if(Double.parseDouble(tf.getText())>

0){

if(tf.getText().length()>

1){

tf.setText(tf.getText().substring(0,tf.getText().length()-1));

//使用退格删除最后一位字符

}else{

tf.setText("

IfResult=true;

}

2){

tf.setText

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

当前位置:首页 > 农林牧渔 > 林学

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

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