⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 一个简单的计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MainFrame
{ private Frame fr; 
  private Panel p1;
  private Panel p2;
  private Panel p3;
  static JTextField text;
  static Button []number;
  static Button Bzf;
  static Button Bpoint;
  static Button []rbutton;
  
  static double answear=0;  //储存中间运算结果
  static boolean start=true; //标志是不是新的一轮计算开始
  static boolean point=false; //标志是否已经存在小数点,避免一个数中出现多个小数点
  static boolean fnumber=false; //标志0前面是否有非0数或小数点,避免“00000”现象的出现
  static boolean negtive=false; //判断是不是负数
  static boolean computable=true;//判断当前所显示的是否能用于计算
  static boolean error=false;//记录有无除数为0错误
  static boolean error1=false;//记录有无函数调用无效错误
  static boolean error2=false;//记录有无数据溢出
  static String ysf="=";
  
  public static void main(String []args)
  { MainFrame jiemian=new MainFrame();
    jiemian.showjiemian();
  }
  
  public void showjiemian()  //显示界面
  { fr=new Frame("赵长松的计算器");
    fr.setLayout(null);//使局部管理器失效
    fr.setBackground(Color.lightGray);
    fr.setLocation(350,200);  //设置窗口出现时在屏幕的位置
    fr.setResizable(false);  //不允许用户自己改变窗口大小
    fr.addWindowListener(new WindowAction());//关闭窗口
    text=new JTextField("0",25);
    p1=new Panel();
    p2=new Panel();
    p3=new Panel();
    number=new Button[10];
    Bzf=new Button("+/-");
    Bpoint=new Button(".");
    text.setHorizontalAlignment(SwingConstants.RIGHT); //将输入的数字或得到的结果在文本右侧显示
    text.setEditable(false); //不允许修改文本框里的内容
    p1.setSize(285,35);
    p1.setBackground(Color.gray);
    p1.add(text);
    p2.setLayout(new GridLayout(4,3,3,3));
    number[9]=new Button(((Integer)0).toString());
    { int i;
     for(i=0;i<3;i++)
     { number[i]=new Button(((Integer)(i+7)).toString()); }
     for(i=3;i<6;i++)
     { number[i]=new Button(((Integer)(i+1)).toString()); }
     for(i=6;i<9;i++)
     { number[i]=new Button(((Integer)(i-5)).toString()); }
    }
    p2.setSize(125,130);
    for(int i=0;i<10;i++)
    { number[i].setForeground(Color.blue);//使按钮上的字的颜色为蓝色
      number[i].addActionListener(new ButtonAction());//加按钮监听器
      number[i].addKeyListener(new ButtonAction());//加键盘监听器
      p2.add(number[i]); 
    }
    Bzf.setForeground(Color.blue);
    Bpoint.setForeground(Color.blue);
    Bzf.addActionListener(new ButtonAction());
    Bpoint.addActionListener(new ButtonAction());
    p2.add(Bzf);
    p2.add(Bpoint);
    
    p3.setSize(150,130);
    p3.setLayout(new GridLayout(4,4,3,3));
    rbutton=new Button[16];
    rbutton[0]=new Button("+");
    rbutton[0].setForeground(Color.red);//使按钮上的字的颜色变为红色
    rbutton[1]=new Button("sin");
    rbutton[1].setForeground(Color.blue);
    rbutton[2]=new Button("sqrt");
    rbutton[2].setForeground(Color.blue);
    rbutton[3]=new Button("ln");
    rbutton[3].setForeground(Color.blue);
    rbutton[4]=new Button("-");
    rbutton[4].setForeground(Color.red);
    rbutton[5]=new Button("cos");
    rbutton[5].setForeground(Color.blue);
    rbutton[6]=new Button("x^y");
    rbutton[6].setForeground(Color.blue);
    rbutton[7]=new Button("%");
    rbutton[7].setForeground(Color.blue);
    rbutton[8]=new Button("*"); 
    rbutton[8].setForeground(Color.red);
    rbutton[9]=new Button("tan");
    rbutton[9].setForeground(Color.blue);
    rbutton[10]=new Button("cot");
    rbutton[10].setForeground(Color.blue);
    rbutton[11]=new Button("1/x");
    rbutton[11].setForeground(Color.blue);
    rbutton[12]=new Button("/");
    rbutton[12].setForeground(Color.red);
    rbutton[13]=new Button("退格");
    rbutton[13].setForeground(Color.red);
    rbutton[14]=new Button("CE");
    rbutton[14].setForeground(Color.red);    
    rbutton[15]=new Button("="); 
    rbutton[15].setForeground(Color.red);
     
    for(int i=0;i<16;i++)
    { rbutton[i].addActionListener(new ButtonAction());
      p3.add(rbutton[i]); }

    p1.setLocation(20,35);
    fr.add(p1);
    p2.setLocation(20,80);
    fr.add(p2);
    p3.setLocation(155,80);
    fr.add(p3);
    fr.setSize(325,230);
    fr.setVisible(true);
  }
  class WindowAction extends WindowAdapter //内部类,处理窗口关闭事件
  { public void windowClosing(WindowEvent e)
    { System.exit(0); }
  }
}


class ButtonAction implements ActionListener,KeyListener //处理事件
{ String a="0";//防止空指针
  
  public void actionPerformed(ActionEvent e)//按钮事件
  { String s=e.getActionCommand();  //获得与此动作相关的命令字符串 
    if(('1'<s.charAt(0))&&(s.charAt(0)<='9')) //如果是2-9数字键
    { if(MainFrame.start) 
      { MainFrame.text.setText(s); 
      	MainFrame.fnumber=true; 
      }
       else { MainFrame.text.setText(MainFrame.text.getText()+s); 
       	      MainFrame.fnumber=true;
            }
      MainFrame.start=false;
      MainFrame.computable=true;
    }
    else if(e.getSource()==MainFrame.number[6])//如果是数字键1
    { if(MainFrame.start) 
      { MainFrame.text.setText(s); 
      	MainFrame.fnumber=true;
      }
       else { MainFrame.text.setText(MainFrame.text.getText()+s); 
       	      MainFrame.fnumber=true; 
            }
      MainFrame.start=false;
      MainFrame.computable=true;
    }	
    else if((s.charAt(0)=='.')&&(MainFrame.computable)) //如果是小数点键
    { if(!MainFrame.point)
      { MainFrame.text.setText(MainFrame.text.getText()+s);
       	MainFrame.point=true;
       	MainFrame.fnumber=true;
       }
      MainFrame.start=false;
    }
    else if(s.charAt(0)=='0')//如果是数字键0
    { if(MainFrame.fnumber) MainFrame.text.setText(MainFrame.text.getText()+s);
      if(MainFrame.start) { MainFrame.text.setText("0");}
      MainFrame.computable=true;
    }
    else if((e.getSource()==MainFrame.Bzf)&&(MainFrame.computable))//如果是正负转换键
    { if(!MainFrame.negtive)
      { a=MainFrame.text.getText();
        if(a.charAt(0)==' ') { a=a.substring(1,a.length()); 
        	                   if(a.charAt(0)=='-')
        	                   { a=a.substring(1,a.length());
        	                     MainFrame.negtive=false;
        	                   }
        	                   else {a="-"+a;
        	                         MainFrame.negtive=true;
        	                        }
        }
      	else { a="-"+a;MainFrame.negtive=true;} 
        MainFrame.text.setText(a);
      }
      else { a = MainFrame.text.getText();
             if(a.charAt(0)==' ')
             { if(a.charAt(1)=='-') {a=a.substring(2,a.length()); 
             	                     MainFrame.negtive=false;
             	                    } 
             	else { a=a.substring(1,a.length()); 
             		   a="-"+a;
             		   MainFrame.negtive=true;
             		 }
             }
             else{ a=a.substring(1,a.length());
                   MainFrame.negtive=false; 
                 }
             MainFrame.text.setText(a);
           }
    } 
    else if((e.getSource()==MainFrame.rbutton[0])&&(MainFrame.computable))//如果是“+”
    { comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="+";
      MainFrame.start=true;
      MainFrame.point=false;
      MainFrame.fnumber=false;
      if(MainFrame.error) { MainFrame.text.setText("除数不能为0");
                            MainFrame.computable=false;
                            MainFrame.error=false;
                          }
     else if(MainFrame.error2) 
      { MainFrame.text.setText("数据过大,造成溢出");
        MainFrame.computable=false;
        MainFrame.error2=false;
      }
     else 
     { MainFrame.text.setText(" "+approximation(MainFrame.answear));
       MainFrame.computable=true;
     }
    }
    else if((e.getSource()==MainFrame.rbutton[4])&&(MainFrame.computable))//如果是“-”
    { comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="-";
      MainFrame.start=true;
      MainFrame.point=false;
      MainFrame.fnumber=false;
      if(MainFrame.error) { MainFrame.text.setText("除数不能为0");
                            MainFrame.computable=false;
                            MainFrame.error=false;
                          }
     else if(MainFrame.error2) 
      { MainFrame.text.setText("数据过大,造成溢出");
        MainFrame.computable=false;
        MainFrame.error2=false;
      }
     else 
     { MainFrame.text.setText(" "+approximation(MainFrame.answear));     
       MainFrame.computable=true;
     }
    }
    else if((e.getSource()==MainFrame.rbutton[8])&&(MainFrame.computable))//如果是“*”
    { comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="*";
      MainFrame.start=true;
      MainFrame.point=false;
      MainFrame.fnumber=false;
      if(MainFrame.error) { MainFrame.text.setText("除数不能为0");
                            MainFrame.computable=false;
                            MainFrame.error=false;
                          }
     else if(MainFrame.error2) 
      { MainFrame.text.setText("数据过大,造成溢出");
        MainFrame.computable=false;
        MainFrame.error2=false;
      }
     else 
     { MainFrame.text.setText(" "+approximation(MainFrame.answear));
       MainFrame.computable=true;
     }     
    }
    else if((e.getSource()==MainFrame.rbutton[12])&&(MainFrame.computable))//如果是“/”
    { comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="/";
      MainFrame.start=true;
      MainFrame.point=false;
      MainFrame.fnumber=false;
     if(MainFrame.error) { MainFrame.text.setText("除数不能为0");
                           MainFrame.computable=false;
                           MainFrame.error=false;
                         }
     else if(MainFrame.error2) 
      { MainFrame.text.setText("数据过大,造成溢出");
        MainFrame.computable=false;
        MainFrame.error2=false;
      }
     else 
     { MainFrame.text.setText(" "+approximation(MainFrame.answear));
       MainFrame.computable=true;
     }
    }
    else if((e.getSource()==MainFrame.rbutton[1])&&(MainFrame.computable))//如果是“sin”
    { MainFrame.ysf="sin";
      comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="=";
      MainFrame.start=true;
      MainFrame.text.setText(" "+approximation(MainFrame.answear));
      MainFrame.computable=true;
    }
    else if((e.getSource()==MainFrame.rbutton[5])&&(MainFrame.computable))//如果是“cos”
    { MainFrame.ysf="cos";
      comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="=";
      MainFrame.start=true;
      MainFrame.text.setText(" "+approximation(MainFrame.answear));
      MainFrame.computable=true;
    }
    else if((e.getSource()==MainFrame.rbutton[9])&&(MainFrame.computable))//如果是“tan”
    { MainFrame.ysf="tan";
      comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="=";
      MainFrame.start=true;
      if(MainFrame.error1) { MainFrame.text.setText("函数输入无效");
                             MainFrame.computable=false;
                             MainFrame.error1=false;
                           }
      else 
      { MainFrame.text.setText(" "+approximation(MainFrame.answear));
        MainFrame.computable=true;
      }
    }
    else if((e.getSource()==MainFrame.rbutton[10])&&(MainFrame.computable))//如果是“cot”
    { MainFrame.ysf="cot";
      comput(Double.parseDouble(MainFrame.text.getText()));
      MainFrame.ysf="=";
      MainFrame.start=true;
      if(MainFrame.error1) { MainFrame.text.setText("函数输入无效");
                             MainFrame.computable=false;
                             MainFrame.error1=false;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -