【java】计算器1


 

  
  
  
  
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import javax.swing.border.*;  
  5. import java.util.LinkedList;  
  6. import java.text.NumberFormat;  
  7. public class ComputerPad extends Frame implements ActionListener   
  8. {  
  9.    NumberButton numberButton[];                    
  10.    OperationButton oprationButton[];               
  11.    Button 小数点按钮,正负号按钮,退格按钮,  
  12.           求倒数按钮,等号按钮,清零按钮;  
  13.    Panel panel;                                   
  14.    JTextField resultShow;                          
  15.    String 运算符号[]={"+","-","*","/"};  
  16.    LinkedList 链表;                                
  17.    boolean  是否按下等号=false;                    
  18.     
  19.  public ComputerPad()  
  20.    {  
  21.      super("计算器");  
  22.      链表=new LinkedList();  
  23.      numberButton=new NumberButton[10];            
  24.      for(int i=0;i<=9;i++)  
  25.         {  
  26.           numberButton[i]=new NumberButton(i);  
  27.           numberButton[i].addActionListener(this);  
  28.         }  
  29.      oprationButton=new OperationButton[4];      
  30.      for(int i=0;i<4;i++)  
  31.         {  
  32.           oprationButton[i]=new OperationButton(运算符号[i]);  
  33.           oprationButton[i].addActionListener(this);  
  34.         }  
  35.      小数点按钮=new Button(".");  
  36.      正负号按钮  =new Button("+/-");   
  37.      等号按钮=new Button("=");  
  38.      求倒数按钮=new Button("1/x");  
  39.      退格按钮=new Button("退格");  
  40.      清零按钮=new Button("C");  
  41.      清零按钮.setForeground(Color.red);  
  42.      退格按钮.setForeground(Color.red);  
  43.      等号按钮.setForeground(Color.red);  
  44.      求倒数按钮.setForeground(Color.blue);  
  45.      正负号按钮.setForeground(Color.blue);  
  46.      小数点按钮.setForeground(Color.blue);  
  47.      退格按钮.addActionListener(this);  
  48.      清零按钮.addActionListener(this);  
  49.      等号按钮.addActionListener(this);  
  50.      小数点按钮.addActionListener(this);  
  51.      正负号按钮.addActionListener(this);  
  52.      求倒数按钮.addActionListener(this);  
  53.      resultShow=new JTextField(10);  
  54.      resultShow.setHorizontalAlignment(JTextField.RIGHT);  
  55.      resultShow.setForeground(Color.blue);  
  56.      resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14));  
  57.      resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));  
  58.      resultShow.setBackground(Color.white);   
  59.      resultShow.setEditable(false);  
  60.      panel=new Panel();                                               
  61.      panel.setLayout(new GridLayout(4,5));   
  62.        
  63.      panel.add(numberButton[1]);  
  64.      panel.add(numberButton[2]);  
  65.      panel.add(numberButton[3]);  
  66.      panel.add(oprationButton[0]);  
  67.      panel.add(清零按钮);  
  68.        
  69.      panel.add(numberButton[4]);  
  70.      panel.add(numberButton[5]);  
  71.      panel.add(numberButton[6]);  
  72.      panel.add(oprationButton[1]);  
  73.      panel.add(退格按钮);  
  74.        
  75.      panel.add(numberButton[7]);  
  76.      panel.add(numberButton[8]);  
  77.      panel.add(numberButton[9]);  
  78.      panel.add(oprationButton[2]);  
  79.      panel.add(求倒数按钮);  
  80.        
  81.      panel.add(numberButton[0]);  
  82.      panel.add(正负号按钮);  
  83.      panel.add(小数点按钮);  
  84.      panel.add(oprationButton[3]);  
  85.      panel.add(等号按钮);  
  86.      add(panel,BorderLayout.CENTER);  
  87.      add(resultShow,BorderLayout.NORTH);  
  88.       
  89.      addWindowListener(new WindowAdapter()  
  90.                     { public void windowClosing(WindowEvent e)  
  91.                        {  
  92.                          System.exit(0);  
  93.                        }  
  94.                     });  
  95.     setVisible(true);  
  96.     setBounds(100,50,240,180);  
  97.     setResizable(false);  
  98.     validate();  
  99.    }  
  100.  public void actionPerformed(ActionEvent e)  
  101.    {  
  102.      
  103.     if(e.getSource() instanceof NumberButton)     
  104.      {  
  105.        NumberButton b=(NumberButton)e.getSource();  
  106.         if(链表.size()==0)   
  107.          {  
  108.            int number=b.getNumber();            
  109.            链表.add(""+number);                 
  110.            resultShow.setText(""+number);  
  111.            是否按下等号=false;  
  112.          }  
  113.           
  114.         else if(链表.size()==1&&是否按下等号==false)  
  115.          {  
  116.            int number=b.getNumber();  
  117.            String num=(String)链表.getFirst();  
  118.            String s=num.concat(""+number);       
  119.            链表.set(0,s);  
  120.            resultShow.setText(s);  
  121.          }  
  122.         else if(链表.size()==1&&是否按下等号==true)  
  123.          {  
  124.            int number=b.getNumber();  
  125.             链表.removeFirst();  
  126.            链表.add(""+number);                   
  127.            是否按下等号=false;  
  128.            resultShow.setText(""+number);  
  129.          }  
  130.         else if(链表.size()==2)                  
  131.          {  
  132.            int number=b.getNumber();             
  133.            链表.add(""+number);                 
  134.                                                 
  135.            resultShow.setText(""+number);  
  136.          }  
  137.         else if(链表.size()==3)                  
  138.          {  
  139.            int number=b.getNumber();  
  140.            String num=(String)链表.getLast();  
  141.            String s=num.concat(""+number);       
  142.            链表.set(2,s);  
  143.            resultShow.setText(s);  
  144.          }  
  145.      }  
  146.    else if(e.getSource() instanceof OperationButton)   
  147.      {  
  148.        OperationButton b=(OperationButton)e.getSource();  
  149.        if(链表.size()==1)  
  150.          {  
  151.            String fuhao=b.get运算符号();  
  152.              
  153.            链表.add(fuhao);                  
  154.          }  
  155.        else if(链表.size()==2)  
  156.          {  
  157.            String fuhao=b.get运算符号();  
  158.            链表.set(1,fuhao);               
  159.          }  
  160.        else if(链表.size()==3)  
  161.          {  
  162.            String fuhao=b.get运算符号();  
  163.            String number1=(String)链表.getFirst();  
  164.            String number2=(String)链表.getLast();  
  165.            String 运算符号=(String)链表.get(1);  
  166.            try 
  167.             {  
  168.                double n1=Double.parseDouble(number1);  
  169.                double n2=Double.parseDouble(number2);  
  170.                double n=0;  
  171.                if(运算符号.equals("+"))  
  172.                  {  
  173.                    n=n1+n2;  
  174.                  }  
  175.                else if(运算符号.equals("-"))  
  176.                  {  
  177.                    n=n1-n2;  
  178.                  }  
  179.                else if(运算符号.equals("*"))  
  180.                  {  
  181.                    n=n1*n2;  
  182.                  }  
  183.                else if(运算符号.equals("/"))  
  184.                  {  
  185.                    n=n1/n2;  
  186.                  }  
  187.                链表.clear();  
  188.                链表.add(""+n);                     
  189.                链表.add(fuhao);                    
  190.                resultShow.setText(""+n);  
  191.             }  
  192.           catch(Exception ee)  
  193.             {  
  194.             }  
  195.          }  
  196.      }  
  197.    else if(e.getSource()==等号按钮)   
  198.      {  
  199.         是否按下等号=true;  
  200.         if(链表.size()==1||链表.size()==2)                         
  201.          {  
  202.            String num=(String)链表.getFirst();  
  203.            resultShow.setText(""+num);  
  204.          }  
  205.         else if(链表.size()==3)  
  206.          {  
  207.            String number1=(String)链表.getFirst();  
  208.            String number2=(String)链表.getLast();  
  209.            String 运算符号=(String)链表.get(1);  
  210.            try 
  211.             {  
  212.                double n1=Double.parseDouble(number1);  
  213.                double n2=Double.parseDouble(number2);  
  214.                double n=0;  
  215.                if(运算符号.equals("+"))  
  216.                  {  
  217.                    n=n1+n2;  
  218.                  }  
  219.                else if(运算符号.equals("-"))  
  220.                  {  
  221.                    n=n1-n2;  
  222.                  }  
  223.                else if(运算符号.equals("*"))  
  224.                  {  
  225.                    n=n1*n2;  
  226.                  }  
  227.                else if(运算符号.equals("/"))  
  228.                  {  
  229.                    n=n1/n2;  
  230.                  }  
  231.                resultShow.setText(""+n);  
  232.                链表.set(0,""+n);  
  233.                链表.removeLast();    
  234.                链表.removeLast();    
  235.               
  236.             }  
  237.           catch(Exception ee)  
  238.             {  
  239.             }  
  240.          }  
  241.      }  
  242.    else if(e.getSource()==小数点按钮)  
  243.      {  
  244.         if(链表.size()==0)   
  245.          {  
  246.             是否按下等号=false;  
  247.          }  
  248.        else if(链表.size()==1)                                 
  249.         {  
  250.            String dot=小数点按钮.getLabel();               
  251.            String num=(String)链表.getFirst();  
  252.            String s=null;  
  253.            if(num.indexOf(dot)==-1)                       
  254.                {  
  255.                  s=num.concat(dot);   
  256.                  链表.set(0,s);                    
  257.                }  
  258.            else 
  259.                {  
  260.                  s=num;    
  261.                }  
  262.            链表.set(0,s);  
  263.            resultShow.setText(s);  
  264.         }  
  265.          
  266.        else if(链表.size()==3)  
  267.         {  
  268.            String dot=小数点按钮.getLabel();               
  269.            String num=(String)链表.getLast();  
  270.            String s=null;  
  271.            if(num.indexOf(dot)==-1)  
  272.               {  
  273.                 s=num.concat(dot);   
  274.                 链表.set(2,s);  
  275.               }  
  276.            else 
  277.               {  
  278.                 s=num;  
  279.               }  
  280.            resultShow.setText(s);   
  281.         }   
  282.      }  
  283.      else if(e.getSource()==退格按钮)  
  284.      {  
  285.        if(链表.size()==1)                                 
  286.         {  
  287.            String num=(String)链表.getFirst();  
  288.            if(num.length()>=1)  
  289.             {  
  290.              num=num.substring(0,num.length()-1);  
  291.              链表.set(0,num);                    
  292.              resultShow.setText(num);  
  293.             }  
  294.           else 
  295.             {  
  296.               链表.removeLast();                    
  297.               resultShow.setText("0");  
  298.             }  
  299.         }  
  300.       else if(链表.size()==3)  
  301.         {  
  302.            String num=(String)链表.getLast();  
  303.            if(num.length()>=1)  
  304.             { num=num.substring(0,num.length()-1);  
  305.               链表.set(2,num);                    
  306.               resultShow.setText(num);  
  307.             }  
  308.            else 
  309.             {  
  310.               链表.removeLast();                    
  311.               resultShow.setText("0");  
  312.             }  
  313.         }     
  314.      }  
  315.    else if(e.getSource()==正负号按钮)  
  316.      {  
  317.        if(链表.size()==1)  
  318.         {  
  319.           String number1=(String)链表.getFirst();  
  320.            try 
  321.               {  
  322.                 double d=Double.parseDouble(number1);  
  323.                 d=-1*d;  
  324.                 String str=String.valueOf(d);  
  325.                 链表.set(0,str);  
  326.                 resultShow.setText(str);   
  327.               }  
  328.            catch(Exception ee)  
  329.               {  
  330.               }  
  331.         }   
  332.       else if(链表.size()==3)  
  333.         {  
  334.           String number2=(String)链表.getLast();  
  335.            try 
  336.               {  
  337.                 double d=Double.parseDouble(number2);  
  338.                 d=-1*d;  
  339.                 String str=String.valueOf(d);  
  340.                 链表.set(2,str);  
  341.                 resultShow.setText(str);  
  342.               }  
  343.            catch(Exception ee)  
  344.               {  
  345.               }  
  346.         }   
  347.      }  
  348.    else if(e.getSource()==求倒数按钮)  
  349.      {  
  350.        if(链表.size()==1||链表.size()==2)  
  351.         {  
  352.           String number1=(String)链表.getFirst();  
  353.            try 
  354.               {  
  355.                 double d=Double.parseDouble(number1);  
  356.                 d=1.0/d;  
  357.                 String str=String.valueOf(d);  
  358.                 链表.set(0,str);  
  359.                 resultShow.setText(str);   
  360.               }  
  361.            catch(Exception ee)  
  362.               {  
  363.               }  
  364.         }   
  365.        else if(链表.size()==3)  
  366.         {  
  367.           String number2=(String)链表.getLast();  
  368.            try 
  369.               {  
  370.                 double d=Double.parseDouble(number2);  
  371.                 d=1.0/d;  
  372.                 String str=String.valueOf(d);  
  373.                 链表.set(0,str);  
  374.                 resultShow.setText(str);   
  375.               }  
  376.            catch(Exception ee)  
  377.               {  
  378.               }  
  379.         }  
  380.      }   
  381.    else if(e.getSource()==清零按钮)  
  382.      {   
  383.         是否按下等号=false;  
  384.         resultShow.setText("0");  
  385.         链表.clear();  
  386.      }   
  387.     }  
  388.  public  static void main(String args[])  
  389.    {  
  390.       new ComputerPad();  
  391.    }    
  392. }  

 

你可能感兴趣的:(java,计算器)