1. 程式人生 > >java 自寫的計算器

java 自寫的計算器


import java.awt.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class jsq extends JFrame implements ActionListener{
 public static  int x;
 public static  int x1;
 public static  int x2;
 public static  int x3;  public static  int y;
 String s = null;
 String s1 = null;
 String s2 = null;
 String ss = null;  static JTextField t=new JTextField(10);
 JButton b1=new JButton("1");  JButton b2=new JButton("2");
 JButton b3=new JButton("3");
 JButton b4=new JButton("4");
 JButton b5=new JButton("5");
 JButton b6=new JButton("6");
 JButton b7=new JButton("7");
 JButton b8=new JButton("8");
 JButton b9=new JButton("9");
 JButton b0=new JButton("0");
 JButton c=new JButton("C");
 JButton del=new JButton("Del");
 JButton cf =new JButton("/");
 JButton cf1 =new JButton("*");
 JButton jf1 =new  JButton("+");
 JButton jf=new JButton("-");
 JButton dy=new JButton("=");
 JPanel p1;
 JPanel p2;
 public jsq(){   b0.setBackground(Color.CYAN);
  b1.setBackground(Color.CYAN);
  b2.setBackground(Color.CYAN);
  b3.setBackground(Color.CYAN);
  b4.setBackground(Color.CYAN);
  b5.setBackground(Color.CYAN);
  b6.setBackground(Color.CYAN);
  b7.setBackground(Color.CYAN);
  b8.setBackground(Color.CYAN);
  b9.setBackground(Color.CYAN);
  p1=new JPanel();
  p1.setLayout(new GridLayout(4,4));
  p1.add(c);p1.add(del);p1.add(cf);p1.add(cf1);
  p1.add(b7);p1.add(b8);p1.add(b9);p1.add(jf1);
  p1.add(b4);p1.add(b5);p1.add(b6);p1.add(jf);
  p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b0);
  p2=new JPanel();   p2.setLayout(new BorderLayout());
  p2.add("North",t);p2.add(p1);p2.add("South",dy);
  //p2.setBackground(Color.ORANGE);
  this.add(p2);   b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  b4.addActionListener(this);
  b5.addActionListener(this);   b6.addActionListener(this);
  b7.addActionListener(this);
  b8.addActionListener(this);
  b9.addActionListener(this);
  b0.addActionListener(this);
  cf.addActionListener(this);
  cf1.addActionListener(this);
  jf1.addActionListener(this);
  jf.addActionListener(this);
  dy.addActionListener(this);
  c.addActionListener(this);
  del.addActionListener(this);
  this.setTitle("計算器");
  this.setLocation(300, 250);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(300,250);
  this.setVisible(true);  }
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub   if(e.getSource() == b1){    x = 1;
   s = t.getText();
   clear();
   t.setText(s+x);
   //System.out.println(x);
  }
  if(e.getSource() == b2){    x = 2;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b3){    x = 3;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b4){    x = 4;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b5){    x = 5;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b6){    x = 6;
   s = t.getText();
   clear();
   t.setText(s+x);   }
  if(e.getSource() == b7){    x = 7;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b8){    x = 8;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b9){    x = 9;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == b0){    x = 0;
   s = t.getText();
   clear();
   t.setText(s+x);
  }
  if(e.getSource() == cf1){
   y = 1;
   x1 =  Integer.parseInt(t.getText());
   System.out.println(x1);
   add1(x1);
   t.setText("*");   }   if(e.getSource() == cf){
   y = 2;
   x1 =  Integer.parseInt(t.getText());
   System.out.println(x1);
   add1(x1);
   t.setText("/");   }   if(e.getSource() == jf1){
   y = 3;
   x1 =  Integer.parseInt(t.getText());
   System.out.println(x1);
   add1(x1);
   t.setText("+");   }   if(e.getSource() == jf){
   y = 4;
   x1 =  Integer.parseInt(t.getText());
   System.out.println(x1);
   add1(x1);
   t.setText("-");   }   if(e.getSource() == dy){
   //t.setText("=");
   x1 =  Integer.parseInt(t.getText());
   System.out.println(x1);
   jgsc(x1);
  }   if(e.getSource() == c){
   t.setText(""); 
  }
  if(e.getSource() == del){    clear();  
   x1 =  Integer.parseInt(t.getText());    delet(x1);   }
 }  void clear(){
  if(s.equals("*")||s.equals("/")||s.equals("+")||s.equals("-")){
   s="";   
  }  
 }//清楚加減乘除的符號  void add1(int qq){
  x2 = qq; 
 }  void jgsc(int qq1){
  if(y==1){
   x3 = qq1;
   String s3 = String.valueOf(x2*x3);
   t.setText(x2+"*"+x3+"="+s3);
   System.out.println((x2*x3)); }   if(y==2){
   x3 = qq1;
   String s3 = String.valueOf(x2/x3);
   t.setText(x2+"/"+x3+"="+s3);
   System.out.println((x2/x3)); }    if(y==3){
   x3 = qq1;
   String s3 = String.valueOf(x2+x3);
   t.setText(x2+"+"+x3+"="+s3);
   System.out.println((x2+x3)); }   if(y==4){
   x3 = qq1;
   String s3 = String.valueOf(x2-x3);
   t.setText(x2+"-"+x3+"="+s3);
   System.out.println((x2-x3)); } 
 }
 void delet(int qq2){
  int a = qq2;   int  q,i=0,yy;   yy=a;
  System.out.println(yy);
  while(yy>0)
  {
   yy=yy/10;
   i++;
  }
  int []as = new int[i];   System.out.println("這是一個"+i+"位數,各個位上(從低位到高位)分別為:");
  for(yy=i-1;yy>=0;yy--)
  {
   q=a%10;
   System.out.println(q);
   a=a/10;
   as[yy] = q;   }
  i--;
  String as1="";
  for(int i1 = 0;i1 < i;i1++){
  System.out.print(as[i1]);
  as1 += String.valueOf(as[i1]);
  }
  t.setText(as1);
 }  public static void main(String[] args) {
  new jsq();
 }
}