计算器源码-java

    import javax.swing.*;
   
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
   
    public class awtQi extends JFrame implements ActionListener
    {
   
     private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;
     private JTextField resultField;
     private Panel pNorth,pCenter,pSouth;
     private boolean end,add,sub,mul,div;
     private String str;
      private double num1,num2;
       JLabel lblStatusbar;
      
      public awtQi()
       {
           pSouth=new Panel();
           pNorth=new Panel();                    //面板布局
           pCenter=new Panel();
           lblStatusbar=new JLabel(“【小项目系列作品-计算器】     编写者:朱峰      班级:06计信1班 “);
           resultField=new JTextField(“0″);
           JButtonListener();
           add(pNorth,BorderLayout.NORTH);            
           add(pCenter,BorderLayout.CENTER);
           add(pSouth,BorderLayout.SOUTH);
           pNorth.setLayout(new GridLayout(1,1));
           pNorth.add(resultField);
           pSouth.add(lblStatusbar);
           pCenter.setLayout(new GridLayout(4,4));
           addButton();
          
       }
 
      private void addButton()             //改方法在jPanel上添加按钮组
       {                                    
          pCenter.add(s1);
          pCenter.add(s2);
          pCenter.add(s3);
          pCenter.add(b1);
          pCenter.add(s4);
          pCenter.add(s5);
          pCenter.add(s6);
          pCenter.add(b2);
          pCenter.add(s7);
          pCenter.add(s8);
          pCenter.add(s9);
          pCenter.add(b3);
          pCenter.add(s0);  
          pCenter.add(f1);
          pCenter.add(f2);
          pCenter.add(b4);
       }
 
      private void JButtonListener()             //该方法创建按钮,并且注册监听事件到listenerQi类中
       {            
          s1=new JButton(“   1   “);   s1.addActionListener(this);
          s2=new JButton(“   2   “);   s2.addActionListener(this);
          s3=new JButton(“   3   “);   s3.addActionListener(this);   
          s4=new JButton(“   4   “);   s4.addActionListener(this);
          s5=new JButton(“   5   “);   s5.addActionListener(this);
          s6=new JButton(“   6   “);   s6.addActionListener(this);
          s7=new JButton(“   7   “);   s7.addActionListener(this);
          s8=new JButton(“   8   “);   s8.addActionListener(this);
          s9=new JButton(“   9   “);   s9.addActionListener(this);
          s0=new JButton(“   0   “);   s0.addActionListener(this);
          b1=new JButton(“   +   “);   b1.addActionListener(this);
          b2=new JButton(“   -   “);   b2.addActionListener(this);
          b3=new JButton(“   *   “);   b3.addActionListener(this);
          b4=new JButton(“   /   “);   b4.addActionListener(this);
          f1=new JButton(“   .   “);   f1.addActionListener(this);
          f2=new JButton(“   =   “);   f2.addActionListener(this);
       }
      
      public static void main(String[] args)
       {
           awtQi test=new awtQi();
           test.setSize(400,300);
           test.setVisible(true);
       }
      
      
      
      
      public void num(int i)                //决定显示的数值
       {
           String s=null;
           s=String.valueOf(i);
          if(end)
           {
               resultField.setText(“0″);
               end=false;
           }
          if((resultField.getText()).equals(“0″))
               resultField.setText(s);
          else
           {
               s=resultField.getText()+s;
               resultField.setText(s);
           }
       }
      
      public void sign(int s)
       {
          if     (s==1)
           {
               add=true;
               sub=false;
               mul=false;
               div=false;
           }
          else if (s==2)
           {
               add=false;
               sub=true;
               mul=false;
               div=false;
           }
          else if (s==3)
           {
               add=false;
               sub=false;
               mul=true;
               div=false;
           }
          else if (s==4)
           {
               add=false;
               sub=false;
               mul=false;
               div=true;
           }
           num1=Double.parseDouble(resultField.getText());
           System.out.println(“第一个数字已产生:”+num1);
           end=true;
          
       }
      public void actionPerformed(ActionEvent e) {
          if (e.getSource()==s1)                //以下为0-9的输入
               num(1);
          else if (e.getSource()==s2)
               num(2);
          else if (e.getSource()==s3)
               num(3);
          else if (e.getSource()==s4)
               num(4);
          else if (e.getSource()==s5)
               num(5);
          else if (e.getSource()==s6)
               num(6);
          else if (e.getSource()==s7)
               num(7);
          else if (e.getSource()==s8)
               num(8);
          else if (e.getSource()==s9)
               num(9);
          else if (e.getSource()==s0)
               num(0);
          
          
          else if(e.getSource()==b1)            //以下为+,-,*,/的输入
               sign(1);
          else if(e.getSource()==b2)
               sign(2);        
          else if(e.getSource()==b3)
               sign(3);            
          else if(e.getSource()==b4)
               sign(4);
          
          
          else if(e.getSource()==f1)            //判断.的输入
           {
               str=resultField.getText();
              if(str.indexOf(“.”)<=1)
                   str=str+“.”;
               resultField.setText(str);
      
           }
          else if(e.getSource()==f2)                //按下=号时候的计算
               {
                   num2=Double.parseDouble(resultField.getText());        
                   System.out.println(“第二个数字已产生:”+num2);
                  
                  if(add)
                       num1=num1+num2;
                  else if(sub)
                       num1=num1-num2;
                  else if(mul)
                       num1=num1*num2;
                  else if(div)
                       num1=num1/num2;
                  
                          
                   System.out.println(“结果为:”+num1);
                   printQi();
               }                                    
       }
 
      private void printQi() {
           resultField.setText(String.valueOf(num1));            //显示最终的结果
           end=true;
       }
 
  }

评论关闭。