자바 GUI 는 간단 한 계산 기 를 만든다.

26052 단어 자바
논리 에 문제 가 있 는 것 같 습 니 다. 인터페이스 가 아직 쓸 수 있 으 니 아 쉬 운 대로 보 세 요.시간 나 면 고 쳐.
package calculator;

/*   :calculator
  :     
  :1.0      Bill        2015 11 28     
                ,      

        2.0      Bill        2015 12 2
                ,      

        2.1      Bill        2015 12 5
                        ,    ,       
*/


import java.awt.*;         //    
import java.awt.event.*;   //      

import javax.swing.*;      //gui  

import java.io.*;          //           

public class calculator                                     //implements ActionListener
 implements ActionListener
{   
    JFrame frame;
    JButton btn[]=new JButton[12];                                       //12      
    JButton add,minus,plus,divide,clean,back,sqrt,equal;    //   +,-,*,/,  ,  ,  ,  
    double x,y;                                             //     
    int z;                                                  //      
    int z2;
    JTextField field;
    StringBuffer str;

    public static void main(String[] args)
    {
        calculator cal=new calculator();
    }
    public calculator()
    {   
        frame=new JFrame("        ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            //  gui    
        frame.getContentPane();                                          //      


        JPanel shangBufen=new JPanel();                                 //       
        JPanel xiaBufen=new JPanel();                                   //       

        //           
        str=new StringBuffer();

       //           
        field=new JTextField(30);                    //20          (            )
        field.setText("0");
        field.setHorizontalAlignment(JTextField.RIGHT);        //      
        field.setEditable(false);
        shangBufen.add(field);
        shangBufen.setLayout(new GridLayout(2,1));            //    

        //0~9      

        String s;
        for(int i=0;i<10;i++)
        {
            s=""+i;
            btn[i]=new JButton(s);
            btn[i].addActionListener(this);
        }
         btn[10]=new JButton("-/+");
         btn[10].addActionListener(this);
         btn[11]=new JButton(".");
         btn[11].addActionListener(this);

         add=new JButton("+");minus=new JButton("-");
         add.addActionListener(this);minus.addActionListener(this);
         plus=new JButton("*");divide=new JButton("/");
         plus.addActionListener(this);divide.addActionListener(this);
         clean=new JButton("C");back=new JButton("Back");
         clean.addActionListener(this);back.addActionListener(this);
         sqrt=new JButton("Sqrt");equal=new JButton("=");
         sqrt.addActionListener(this);equal.addActionListener(this);



        xiaBufen.add(btn[7]); xiaBufen.add(btn[8]); xiaBufen.add(btn[9]); xiaBufen.add(add); xiaBufen.add(minus);  
        xiaBufen.add(btn[4]); xiaBufen.add(btn[5]); xiaBufen.add(btn[6]); xiaBufen.add(plus); xiaBufen.add(divide);  
        xiaBufen.add(btn[1]); xiaBufen.add(btn[2]);xiaBufen.add(btn[3]); xiaBufen.add(clean); xiaBufen.add(back);  
        xiaBufen.add(btn[0]); xiaBufen.add(btn[10]); xiaBufen.add(btn[11]);xiaBufen.add(sqrt);xiaBufen.add(equal);  
        xiaBufen.setLayout(new GridLayout(4,5,5,5));                          //    ,  5  

        frame.add(shangBufen);
        frame.add(xiaBufen);
        frame.setLayout(new FlowLayout());            //    

        frame.setSize(400,300);
        frame.setResizable(false);           //        
        frame.setVisible(true);         
    }

        public void actionPerformed(ActionEvent event)                   //       
        {
          try
          {
              if(event.getSource()==clean)                              //       
              {
                field.setText("0");                                     //    
                field.setHorizontalAlignment(JTextField.RIGHT);         //      
                str.setLength(0);                                       //                      
                z=0;z2=0;
              }
              else if(event.getSource()==back)                          //         
              {
                  if(!field.getText().trim().equals("0"))    //       0,trim()             
                  {  
                       if(str.length()!=1)                
                       {
                       field.setText(str.delete(str.length()-1,str.length()).toString());//        ,           
                       field.setHorizontalAlignment(JTextField.RIGHT);
                       }
                       else
                       {
                          field.setText("0");
                          field.setHorizontalAlignment(JTextField.RIGHT);
                          str.setLength(0);
                       }
                 }
              }
              else if(event.getSource()==btn[10])                                        //     
              {
                  x=Double.parseDouble(field.getText().trim());
                  x=-x;
                  field.setText(""+x);
                  field.setHorizontalAlignment(JTextField.RIGHT);
              }
              else if(event.getSource()==btn[11])                                        //      
              {
                  //                
                  if(field.getText().trim().indexOf(".")==-1)          //       -1
                  {   
                      field.setText(str.append(event.getActionCommand()).toString());            //getActionCommmand()          
                      field.setHorizontalAlignment(JTextField.RIGHT);
                  }
              }else if(event.getSource()==sqrt)                                          //       
              {
                  x=Double.parseDouble(field.getText().trim());
                  if(x>=0)
                  {   
                      x=Math.sqrt(x);
                      field.setText(""+x);
                      field.setHorizontalAlignment(JTextField.RIGHT);
                  }
                  y=0d;
                  str.setLength(0);
                  str.append(""+x);
              }else if(event.getSource()==add)                                          //"+"   
              {


                  if(z!=0)
                  {
                      y=Double.parseDouble(field.getText().trim());
                      str.setLength(0);
                      switch(z)
                      {case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 0:break;
                      }  
                  }else
                  {field.setText(""+0);}
                  y=0d;
                  z=1;z2=1;
              }else if(event.getSource()==minus)                                       //“-”   
              {


                  if(z!=0)
                  {
                      y=Double.parseDouble(field.getText().trim());
                      str.setLength(0);
                      switch(z)
                      {case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 0:break;
                      }  
                  }else
                  {field.setText(""+0);}
                  y=0d;
                  z=2;z2=1;
              }else if(event.getSource()==plus)                                        //“*”   
              {


                  if(z!=0)
                  {
                      y=Double.parseDouble(field.getText().trim());
                      str.setLength(0);
                      switch(z)
                      {case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 0:break;
                      }  
                  }else
                  {field.setText(""+0);}
                  y=0d;
                  z=3;z2=1;
              }else if(event.getSource()==divide)                                      //“/”   
              {

                  if(z!=0)
                  {
                      y=Double.parseDouble(field.getText().trim());
                      str.setLength(0);
                      switch(z)
                      {case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                       case 0:break;
                      }  
                  }else
                  {field.setText(""+0);}
                  y=0d;
                  z=4;z2=1;
              }else if(event.getSource()==equal)  
              {
                  y=Double.parseDouble(field.getText().trim());
                  str.setLength(0);
                  switch(z)
                  {case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                   case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                   case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                   case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
                   case 0:break;
                  }
                  z=0;z2=0;

              }else if(event.getSource()==btn[0])
              {
                  if(!field.getText().trim().equals("0"))
                  {
                      field.setText(str.append(event.getActionCommand()).toString());            //getActionCommmand()          
                      field.setHorizontalAlignment(JTextField.RIGHT);
                  }
              }else                                                                          //        
              {   if(z!=0)
                 {
                  if(z2==1)
                  {
                      str.setLength(0);
                      field.setText(str.append(event.getActionCommand()).toString());            //getActionCommmand()          
                      field.setHorizontalAlignment(JTextField.RIGHT);
                      x=Double.parseDouble(field.getText().trim());
                      z2=0;
                  }else
                  {
                      field.setText(str.append(event.getActionCommand()).toString());            //getActionCommmand()          
                      field.setHorizontalAlignment(JTextField.RIGHT);  
                      x=Double.parseDouble(field.getText().trim());               
                  } 
                 }else
                 {
                     field.setText(str.append(event.getActionCommand()).toString());            //getActionCommmand()          
                     field.setHorizontalAlignment(JTextField.RIGHT); 
                     x=Double.parseDouble(field.getText().trim());
                 }
              }
          }catch(NumberFormatException e1)           //    
          {e1.printStackTrace();}
           catch(StringIndexOutOfBoundsException e2)       //         
          {e2.printStackTrace();}
        }

}


좋은 웹페이지 즐겨찾기