正文

计算器(个位四则运算成立)2006-04-26 08:53:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/13175.html

分享到:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame{
 static int count=0,all=0,but;
 static String fuhao;
 private JLabel promptLabel;
 private JTextField inputField;
 private JTextArea outputArea;
 private JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,x1,x2,y1,y2,s1,s2;
 private int jiahao=0;
 static int paixu[]=new int[50];
 
 
 
 
  public test(){
   super("ceshi StringTokenizer");
   Container container=getContentPane();
   container.setLayout(new FlowLayout());
  
   promptLabel=new JLabel("计算器");
   container.add(promptLabel);
   inputField=new JTextField(20);
  
  
  
  
   inputField.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     StringTokenizer tokens=new StringTokenizer(event.getActionCommand());
    
     outputArea.setText("Number of elements"+tokens.countTokens()+"\nThe tokens are:\n");
    
     while(tokens.hasMoreTokens())
     outputArea.append(tokens.nextToken()+"\n");
    }
   });
  
   container.add(inputField);
  
   j1=new JButton("1");
   j2=new JButton("2");
   j3=new JButton("3");
  
   x1=new JButton("*");
   x2=new JButton("/");
  
   j4=new JButton("4");
   j5=new JButton("5");
   j6=new JButton("6");
  
   y1=new JButton("+");
   y2=new JButton("-");
   j7=new JButton("7");
   j8=new JButton("8");
   j9=new JButton("9");
   s1=new JButton("=");
   j0=new JButton("0");
 //1-------------- 
   j1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
    
     but=1;count++;
     paixu(1);
    
     inputField.setText("\t\t            1\n");
  
    
    
    }
   });
 //2-------------- 
   j2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(2);
      inputField.setText("\t\t            2\n");
   }
   }
   );
 //3-------------- 
   j3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(3);
      inputField.setText("\t\t            3\n");
   }
   }
   );
//4-----------  
   j4.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(4);
      inputField.setText("\t\t            4\n");
   }
   }
   );
 //5--------------
 j5.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(5);
      inputField.setText("\t\t            5\n");
   }
   }
   );
  
   //6---------------------
   j6.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(6);
      inputField.setText("\t\t            6\n");
   }
   }
   );
  
   //7----------------------
   j7.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(7);
      inputField.setText("\t\t            7\n");
   }
   }
   );
  
   //8------------
   j8.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(8);
      inputField.setText("\t\t            8\n");
   }
   }
   );
  
   //9-------------------
   j9.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(9);
      inputField.setText("\t\t            9\n");
   }
   }
   );
  
   //0-----------------------
   j0.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
     but=2;count++;
     paixu(0);
      inputField.setText("\t\t            0\n");
   }
   }
   );
  
 //+----------- 
   y1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
 
    fuhao="+";
        
    }
   });
//- ---------------  
   y2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
 
    fuhao="-";
        
    }
   });
  
// *---------------  
   x1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
 
    fuhao="*";
        
    }
   });
  
// / ---------------  
   x2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
 
    fuhao="/";
        
    }
   });     

//= --------------------  
   s1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
   
     if(fuhao=="+")//+
  {   all=paixu[1]+paixu[2];
      String All="\t\t         ";
      All+=all;
     inputField.setText(All);
     } 
      
    else    if(fuhao=="-")//-
  {   all=paixu[1]-paixu[2];
      String All="\t\t         ";
      All+=all;
     inputField.setText(All);
     } 
  else   if(fuhao=="*")//*
  {   all=paixu[1]*paixu[2];
      String All="\t\t         ";
      All+=all;
     inputField.setText(All);
     }   
  else   if(fuhao=="/")// /
  {   all=paixu[1]/paixu[2];
      String All="\t\t         ";
      All+=all;
     inputField.setText(All);
     }  
     count=0;  
   }
   });
  
   container.add(j9);
   container.add(j8);
   container.add(j7);
  
   container.add(x1);
   container.add(x2);
  
   container.add(j6);
   container.add(j5);
   container.add(j4);
  
   container.add(y1);
   container.add(y2);
  
   container.add(j3);
   container.add(j2);
   container.add(j1);
   container.add(j0);
  
   container.add(s1);
  
  
  
  
   outputArea=new JTextArea(10,18);
   outputArea.setEditable(false);
  
   container.add(new JScrollPane(outputArea));
  
  
  
   setSize(275,500);
   setVisible(true);
  
  }
 
  //堆栈-------------------------
  private void paixu(int button){
   
   paixu[count]=button;
   
  }
 
  public static void main(String args[]){
   test application=new test();
   application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

 

 

阅读(4463) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册