计算器完美代码.docx
《计算器完美代码.docx》由会员分享,可在线阅读,更多相关《计算器完美代码.docx(23页珍藏版)》请在冰豆网上搜索。
计算器完美代码
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
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.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JPanel;
importjavax.swing.JRadioButtonMenuItem;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassCalucatorextendsJFrameimplementsActionListener{
privateJTextFieldtf;
privateJPanelpanel1,panel2,panel3;
privateJMenuBarmyBar;
privateJMenumenu1,menu2,menu3;
privateJMenuItemeditItem1,editItem2,help1,help2;
privateJRadioButtonMenuItemseeItem1,seeItem2;//单选框
privatebooleanIfResult=true,flag=false;
privateStringoper="=";
privatedoubleresult=0;
privateNumnumActionListener;
privateDecimalFormatdf;
publicCalucator(){
super("计算器-软件092班熊治景");//设置标题栏
df=newDecimalFormat("#.####");//保留四位小数
this.setLayout(newBorderLayout(5,5));
panel1=newJPanel(newGridLayout(1,3,10,10));
panel2=newJPanel(newGridLayout(5,6,10,10));//5行6列
panel3=newJPanel(newBorderLayout(10,10));
/**
*菜单栏
*/
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");
editItem1.addActionListener(this);
editItem2=newJMenuItem("粘贴(P)Ctrl+V");
editItem2.addActionListener(this);
editItem1.setFont(newFont("宋体",Font.PLAIN,12));
editItem2.setFont(newFont("宋体",Font.PLAIN,12));
/**
*查看栏
*/
seeItem1=newJRadioButtonMenuItem("科学型(T)");
seeItem2=newJRadioButtonMenuItem("标准型(S)");
seeItem1.setFont(newFont("宋体",Font.PLAIN,12));
seeItem2.setFont(newFont("宋体",Font.PLAIN,12));
/**
*帮助栏
*/
help1=newJMenuItem("帮助主题(H)");
help1.addActionListener(this);
help2=newJMenuItem("关于计算器(A)");
help2.addActionListener(this);
help1.setFont(newFont("宋体",Font.PLAIN,12));
help2.setFont(newFont("宋体",Font.PLAIN,12));
ButtonGroupbgb=newButtonGroup();//选项组
menu1.add(editItem1);
menu1.add(editItem2);
menu2.add(seeItem1);
menu2.add(seeItem2);
menu3.add(help1);
menu3.addSeparator();//添加一条分割线
menu3.add(help2);
myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);
this.setJMenuBar(myBar);
numActionListener=newNum();//实现数字监听
/**
*文本域,即为计算器的屏幕显示区域
*/
tf=newJTextField();
Fontf1=newFont("黑体",1,40);//显示屏数字字体
tf.setFont(f1);
tf.setBounds(15,20,410,55);
tf.setForeground(Color.GREEN);
tf.setBackground(Color.RED);//文本区域的背景色
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐
tf.setText("");
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,"ln",newSigns(),Color.magenta);
addButton(panel2,"7",numActionListener,Color.green);
addButton(panel2,"8",numActionListener,Color.green);
addButton(panel2,"9",numActionListener,Color.green);
addButton(panel2,"÷",newSigns(),Color.red);
addButton(panel2,"n!
",newSigns(),Color.magenta);
addButton(panel2,"sqrt",newSigns(),Color.magenta);
addButton(panel2,"4",numActionListener,Color.green);
addButton(panel2,"5",numActionListener,Color.green);
addButton(panel2,"6",numActionListener,Color.green);
addButton(panel2,"×",newSigns(),Color.red);
addButton(panel2,"sin",newSigns(),Color.magenta);
addButton(panel2,"x^2",newSigns(),Color.magenta);
addButton(panel2,"1",numActionListener,Color.green);
addButton(panel2,"2",numActionListener,Color.green);
addButton(panel2,"3",numActionListener,Color.green);
addButton(panel2,"-",newSigns(),Color.red);
addButton(panel2,"cos",newSigns(),Color.magenta);
addButton(panel2,"x^3",newSigns(),Color.magenta);
addButton(panel2,"0",numActionListener,Color.green);
addButton(panel2,"-/+",newClear(),Color.green);
addButton(panel2,".",newDot(),Color.green);
addButton(panel2,"+",newSigns(),Color.red);
addButton(panel2,"tan",newSigns(),Color.magenta);
addButton(panel2,"%",newSigns(),Color.magenta);
addButton(panel2,"π",numActionListener,Color.yellow);
addButton(panel2,"e",numActionListener,Color.yellow);
addButton(panel2,"′″",newSigns(),Color.yellow);
addButton(panel2,"=",newSigns(),Color.red);
panel3.add(panel1,BorderLayout.NORTH);
panel3.add(panel2,BorderLayout.CENTER);
this.add(tf,BorderLayout.NORTH);
this.add(panel3);
pack();
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);//在面板上增加按钮
Fontf3=newFont("黑体",1,20);//按钮字体
bt.setFont(f3);
bt.setForeground(color);//设置前景(字体)颜色
bt.setBackground(newColor(26,29,34));
bt.setFocusPainted(false);//去掉焦点框
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("负数不能开平方根");
}
}
/*ln求常用对数*/
elseif(str.equals("ln")){
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().substring(0,tf.getText().length()-1));
}else{
tf.setText("0");
IfResult=true;
}
}
}elseif(str=="CE"){
tf.setText("0");
If