科学计算器程序Java代码.docx
- 文档编号:26022151
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:15
- 大小:18.39KB
科学计算器程序Java代码.docx
《科学计算器程序Java代码.docx》由会员分享,可在线阅读,更多相关《科学计算器程序Java代码.docx(15页珍藏版)》请在冰豆网上搜索。
科学计算器程序Java代码
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.text.DecimalFormat;
importjavax.swing.BorderFactory;
importjavax.swing.ButtonGroup;
importjavax.swing.JButton;
importjavax.swing.JCheckBoxMenuItem;
importjavax.swing.JFrame;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JPanel;
importjavax.swing.JRadioButtonMenuItem;
importjavax.swing.JTextField;
publicclassCalucatorextendsJFrame{
privateJTextFieldtf;
privateJPanelpanel1,panel2,panel3,panel4;
privateJMenuBarmyBar;
privateJMenumenu1,menu2,menu3;
privateJMenuItemeditItem1,editItem2,help1,help2,help3;
privateJRadioButtonMenuItemseeItem1,seeItem2;//单选框
privateJCheckBoxMenuItemseeItem3;//复选框
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("宋体",Font.PLAIN,12));
menu3.setFont(newFont("宋体",Font.PLAIN,12));
/**
*编辑栏
*/
editItem1=newJMenuItem("复制(C)Ctrl+C");
editItem2=newJMenuItem("粘贴(P)Ctrl+V");
editItem1.setFont(newFont("宋体",Font.PLAIN,12));
editItem2.setFont(newFont("宋体",Font.PLAIN,12));
/**
*查看栏
*/
seeItem1=newJRadioButtonMenuItem("科学型(T)");
seeItem2=newJRadioButtonMenuItem("标准型(S)");
seeItem3=newJCheckBoxMenuItem("数字分组(I)");
seeItem1.setFont(newFont("宋体",Font.PLAIN,12));
seeItem2.setFont(newFont("宋体",Font.PLAIN,12));
seeItem3.setFont(newFont("宋体",Font.PLAIN,12));
/**
*帮助栏
*/
help1=newJMenuItem("帮助主题(H)");
help2=newJMenuItem("关于计算器(A)");
help1.setFont(newFont("宋体",Font.PLAIN,12));
help2.setFont(newFont("宋体",Font.PLAIN,12));
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);
addButton(panel1,"CE",newClear(),Color.red);
addButton(panel1,"C",newClear(),Color.red);
addButton(panel2,"1/x",newSigns(),Color.magenta);
addButton(panel2,"log",newSigns(),Color.magenta);
addButton(panel2,"7",numActionListener,Color.blue);
addButton(panel2,"8",numActionListener,Color.blue);
addButton(panel2,"9",numActionListener,Color.blue);
addButton(panel2,"÷",newSigns(),Color.red);
addButton(panel2,"n!
",newSigns(),Color.magenta);
addButton(panel2,"sqrt",newSigns(),Color.magenta);
addButton(panel2,"4",numActionListener,Color.blue);
addButton(panel2,"5",numActionListener,Color.blue);
addButton(panel2,"6",numActionListener,Color.blue);
addButton(panel2,"×",newSigns(),Color.red);
addButton(panel2,"sin",newSigns(),Color.magenta);
addButton(panel2,"x^2",newSigns(),Color.magenta);
addButton(panel2,"1",numActionListener,Color.blue);
addButton(panel2,"2",numActionListener,Color.blue);
addButton(panel2,"3",numActionListener,Color.blue);
addButton(panel2,"-",newSigns(),Color.red);
addButton(panel2,"cos",newSigns(),Color.magenta);
addButton(panel2,"x^3",newSigns(),Color.magenta);
addButton(panel2,"0",numActionListener,Color.blue);
addButton(panel2,"-/+",newClear(),Color.blue);
addButton(panel2,".",newDot(),Color.blue);
addButton(panel2,"+",newSigns(),Color.red);
addButton(panel2,"tan",newSigns(),Color.magenta);
addButton(panel2,"%",newSigns(),Color.magenta);
addButton(panel2,"π",numActionListener,Color.orange);
addButton(panel2,"e",numActionListener,Color.orange);
addButton(panel2,"′″",newSigns(),Color.orange);
addButton(panel2,"=",newSigns(),Color.red);
JButtonbtns=newJButton("计算器");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);//按钮不可操作
btns.setPreferredSize(newDimension(20,20));
panel3.add(btns);//加入按钮
addButton(panel3,"MC",null,Color.red);
addButton(panel3,"MR",null,Color.red);
addButton(panel3,"MS",null,Color.red);
addButton(panel3,"M+",null,Color.red);
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;}
elseif(oper=="×"){result*=x;}
elseif(oper=="÷"){result/=x;}
elseif(oper=="="){result=x;}
tf.setText(df.format(result));
}
/**
*运算符号的事件监听
*/
classSignsimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
/*
*用ActionEvent对象的getActionCommand()方法
*取得与引发事件对象相关的字符串
*/
Stringstr=e.getActionCommand();
/*sqrt求平方根*/
if(str.equals("sqrt")){
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("log")){
doublei=Double.parseDouble(tf.getText());
if(i>0){
tf.setText(String.valueOf(df.format(Math.log(i))));
}else{
tf.setText("负数不能求对数");
}
}
/*%求百分比*/
elseif(str.equals("%")){
tf.setText(df.format(Double.parseDouble(tf.getText())/100));
}
/*1/x求倒数*/
elseif(str.equals("1/x")){
if(Double.parseDouble(tf.getText())==0){
tf.setText("除数不能为零");
}else{
tf.setText(df.format(1/Double.parseDouble(tf.getText())));
}
}
/*sin求正弦函数*/
elseif(str.equals("sin")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.sin(i))));
}
/*cos求余弦函数*/
elseif(str.equals("cos")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.cos(i))));
}
/*tan求正切函数*/
elseif(str.equals("tan")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.tan(i))));
}
/*n!
求阶乘*/
elseif(str.equals("n!
")){
doublei=Double.parseDouble(tf.getText());
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
{
tf.setText("无法进行阶乘");
}
}
/*x^2求平方*/
elseif(str.equals("x^2")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i)));
}
/*x^3求立方*/
elseif(str.equals("x^3")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i*i)));
}
/*′″角度转换*/
/**
*将角度值转换成弧度值,方便三角函数的计算
*/
elseif(str.equals("′″")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(i/180*Math.PI));
}
else{
if(flag){
IfResult=false;
}
if(IfResult){
oper=str;
}else{
getResult(Double.parseDouble(tf.getText()));
oper=str;
IfResult=true;
}
}
}
}
/**
*清除按钮的事件监听
*/
classClearimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
/*
*用ActionEvent对象的getActionCommand()方法
*取得与引发事件对象相关的字符串
*/
Stringstr=e.getActionCommand();
if(str=="C"){
tf.setText("0");
IfResult=true;
result=0;
}elseif(str=="-/+"){
doublei=0-Double.parseDouble(tf.getText().trim());
tf.setText(df.format(i));
}elseif(str=="Backspace"){
if(Double.parseDouble(tf.getText())>0){
if(tf.getText().length()>1){
tf.setText(tf.getText().substring(0,tf.getText().length()-1));
//使用退格删除最后一位字符
}else{
tf.setText("0");
IfResult=true;
}
}else{
if(tf.getText().length()>2){
tf.setText(tf.getText().sub
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 科学 计算器 程序 Java 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)