import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ShiXi1
{
   JFrame f;
   Container c;
    JTextField t1;
   GridBagLayout gridbag;
   GridBagConstraints g;
      int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
      double weightx,weighty;
      Insets inset;
      boolean clickable;
  //char lastOperator;
   public ShiXi1(){
  f=new JFrame("计算器");
  f.setBounds(200,200,330,250);
  f.setResizable(false);
  c=f.getContentPane();
      ImageIcon icon=new ImageIcon("图片/1.jpg");
      f.setIconImage(icon.getImage());
  addJMenuBar();
  addJPanel();
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public  void  addJMenuBar(){
  
  //添加菜单栏
  JMenuBar  mb=new JMenuBar();
  JMenu  m1=new JMenu("编辑(E)");
  m1.add(new JMenuItem("复制(C) Ctrl+C"));
  m1.add(new JMenuItem("粘贴(P) Ctrl+V"));
  JMenu m2=new JMenu("查看(V)");
  m2.add(new JMenu(" ●   标准型(T)"));
  m2.add(new JMenu("        科学型(S)"));
  m2.addSeparator();
  m2.add(new JMenu("        数字分组(I)"));
  JMenu m3=new JMenu("帮助(H)");
  m3.add(new JMenuItem("帮助主题(H)"));
  m3.addSeparator();
  m3.add(new JMenuItem("关于计算器(A)"));
  mb.add(m1);
  mb.add(m2);
  mb.add(m3);
  f.setJMenuBar(mb);  
   }
   public void addJPanel(){
  gridbag=new GridBagLayout();
      c.setLayout(gridbag);
 
      t1=new JTextField("0.",25);
  t1.setHorizontalAlignment(JTextField.RIGHT);
  t1.setEditable(false);  
      gridx=0;gridy=0; gridwidth=6;gridheight=1; weightx=0;weighty=0;
      anchor=GridBagConstraints.NORTH;
      fill=GridBagConstraints.HORIZONTAL;
      inset=new Insets(5,2,3,2);
      ipadx=0;
      ipady=0;
      g=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
         fill,inset,ipadx,ipady);
      gridbag.setConstraints(t1,g);
      c.add(t1);
 
     
  JLabel l=new JLabel(" ");
  l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
  //l.setPreferredSize(new Dimension(40,50));
      gridx=0;gridy=1; gridwidth=1;gridheight=1; weightx=0;weighty=0;
      anchor=GridBagConstraints.CENTER;
      fill=GridBagConstraints.HORIZONTAL;
      inset=new Insets(2,3,2,5);
      ipadx=0;
      ipady=0;
      g=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
         fill,inset,ipadx,ipady);
      gridbag.setConstraints(l,g);
      c.add(l);
  BHandler handler=new BHandler(); 
  addButton("Backspace",1,1,2,1,Color.RED,handler);
  addButton("CE",3,1,2,1,Color.RED,handler);
  addButton("C",5,1,1,1,Color.RED,handler);
    
        addButton("MC",0,2,1,1,Color.RED,handler);
    addButton("7",1,2,1,1,Color.BLUE,handler); /*定义计算器上的组件*/
    addButton("8",2,2,1,1,Color.BLUE,handler); 
    addButton("9",3,2,1,1,Color.BLUE,handler); 
//addButton("/",4,2,1,1,Color.RED,handler); 
addButton("sqrt",5,2,1,1,Color.BLUE,handler);  addButton("MR",0,3,1,1,Color.RED,handler);
addButton("4",1,3,1,1,Color.BLUE,handler); 
addButton("5",2,3,1,1,Color.BLUE,handler); 
addButton("6",3,3,1,1,Color.BLUE,handler); 
//addButton("*",4,3,1,1,Color.RED,handler); 
addButton("%",5,3,1,1,Color.BLUE,handler); 
addButton("MS",0,4,1,1,Color.RED,handler);
addButton("1",1,4,1,1,Color.BLUE,handler); 
addButton("2",2,4,1,1,Color.BLUE,handler); 
addButton("3",3,4,1,1,Color.BLUE,handler); 
//addButton("-",4,4,1,1,Color.RED,handler); 
addButton("1/x",5,4,1,1,Color.BLUE,handler); 
addButton("M+",0,5,1,1,Color.RED,handler);
addButton("0",1,5,1,1,Color.BLUE,handler); 
addButton("+/-",2,5,1,1,Color.BLUE,handler);
addButton(".",3,5,1,1,Color.BLUE,handler);
//addButton("+",4,5,1,1,Color.RED,handler);
//addButton("=",5,5,1,1,Color.RED,handler);
       

   }
    protected void addButton(String name,int x,int y,int width,int height,Color yanse,ActionListener h) {
       JButton b = new JButton(name);
   b.setForeground(yanse);
      c.setLayout(gridbag);
  gridx=x;
  gridy=y;
  gridwidth=width;
  gridheight=height;   Insets inset = new Insets(2,3,3,4);
  anchor=GridBagConstraints.NORTH;
      fill=GridBagConstraints.HORIZONTAL;
      weightx=0;
      weighty=0;
      ipadx=0;
      ipady=0;
   g=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
           fill,inset,ipadx,ipady);
      gridbag.setConstraints(b,g);
  b.addActionListener(h);
  c.add(b);   }
   
   public static void main(String[] args) 
   {
  new ShiXi1();
   }
   private class BHandler implements ActionListener
   {
   public void actionPerformed(ActionEvent event){
 clickable=true;
  // bl=false; operated=false; equaled=false;
  // storedNumber=0; lastNumber=
   String s=event.getActionCommand();
   if(s.equals("CE")||s.equals("Backspace")||s.equals("c")){
   t1.setText("");}
   else if(s.equals("+/-")){
   double x;
   x=Double.parseDouble(t1.get().trim());
   t1.setText(""+(-x));}
   else 
   if(s==("sqrt")){
   String s1=t1.getText();
   if(s1.charAt(0)=='-'&&clickable=true){
   t1.setText("负数不能开平方");
   clickable=false;}
   else
   t1.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(t1.getText()))));
   }
   else if(s.equals("%"))//单击了%按钮
   {
   double x;
   x=Double.parse(t1.getText().trim());
   t1.setText(""+(0.01*x));
   }
   else if(s.equals("1/x"));//单击了"1/x"按钮
   { 
   double x;
   x=Double.parseDouble(t1.getText().trim());
   if(x==0){ t1.setText("除数不能为零");}
   else {t1.setText(""+(1/x));}
   }
   //按钮‘+/-’按钮时处理
   if(s=="+/-"&& clickable==true){
   boolean isNumber=true;
   String s1=t.getText();
   for (int i=0;i<s.length() ;i++ )
  if(!(s1.charAt(i)>='0'&& s1.charAt(i)<='9'|| s1.charAt(i)=='.'||s1.charAt(i)=='-'))
   {
  isNumber=false;  break;
   }
   if(isNumber==true){
   //如果当前字符串首字母有‘-’,代表现在是一个负数,在按下时将首符号去掉
   if(s.charAt(0)=='-'){
   t1.setText("");
   for(int i=1;i<s1.length();i++){
   char a=s1.charAt(i);
   t1.setText(t1.getText()+a);
   }
   }
   //如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
   else
   t1.setText('-'+s);
   }
   }
   //计算器有关内存的操作
   else 
   if(s=="MC"&& clickable==true){
   memoryd=memoryi=0;
   textMemory.setText("");
   }
   //Ms的操作,将当前数据保存到内存,显示M
   else 
   if(s=="MS"&& clickable==true){
   boolean isDot=false;
   textMemory.setText(" M");
   for(int i=0;i<t1.getText().length();i++)
   if('.'==t1.getText().charAt(i)){
   isDot=true;
   break;
   }
   if(isDot==true){
   memoryd=Double.parseDouble(t1.getText());
   memoryi=0;
   }
   //如果是int,则存入memoryi(int 存储器)
   else {
   memoryi=Integer.parseInt(t1.getText());
   memoryd=0;
   }
   }
   //MR的操作,将存储器的信息输出
   else 
   if(s=="MR"&& clickable==true){
   if(memoryd!=0)
   t1.setText(Double.toString(memoryd));
   if(memoryi!=0)
   t1.setText(Integer.toString(memoryi));
   }
   //M+的功能,将当前文本框里的数据和存储器中数据相加后,再加入存储器
   else 
   if (s=="M+"&& clickable==true)
   {
   boolean isDot=false;
   for(int i=0;i<t1.getText().length();i++)
   if('.'=t1.getText().charAt(i)){
   isDot=true;
   break;
   }
   if(memoryi!=0){
   if(isDot==false)//被加数是一个整数
   memoryi+=Integer.parseInt(t1.getText());
   else{
   //被加数是一个双精度的,则将整形存储器中的数传入双精度存储器中与当前数相加,整形存储器清零
   memoryd=memoryi+Double.parseDouble(t1.getText());
   memoryi=0;
   }
   }
             else
 memoryd+=Double.parseDouble(t1.getText());
   }
   }
   }
   }
   }