简单的计算器

来源:互联网 发布:软件开发计划模板 编辑:程序博客网 时间:2024/05/12 02:09
 

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

import javax.swing.*;
public class MU extends JFrame implements ActionListener{
     
      JTextField jt= new JTextField();
     
      String old ;
      String s;
 public static void main(String[] args) {
        
  MU jj= new MU();
  jj.setVisible(true);
     jj.Mu("自己写的计算器");
 }
 
 public void Mu(String str){
       
    this.setBounds(300, 200, 300, 300);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
   
   
   
    Container c= this.getContentPane();
    c.setLayout(new GridLayout(4,1));
   
    JPanel jp1= new JPanel();
    JPanel jp2= new JPanel();
    JPanel jp3= new JPanel();
    JPanel jp4= new JPanel();
   
    jp1.setLayout(new GridLayout(1,1));
    jp2.setLayout(new GridLayout(3,3));
    jp3.setLayout(new GridLayout(2,2));
   
    jt.setHorizontalAlignment(JTextField.RIGHT);
    jp1.add(jt);
   
    JButton  jb1= new JButton("0");
    JButton  jb2= new JButton("1");
    JButton  jb3= new JButton("2");
    JButton  jb4= new JButton("3");
    JButton  jb5= new JButton("4");
    JButton  jb6= new JButton("5");
    JButton  jb7= new JButton("6");
    JButton  jb8= new JButton("7");
    JButton  jb9= new JButton("8");
    JButton  jb10= new JButton("9");
   
    jp2.add(jb1);
    jp2.add(jb2);
    jp2.add(jb3);
    jp2.add(jb4);
    jp2.add(jb5);
    jp2.add(jb6);
    jp2.add(jb7);
    jp2.add(jb8);
    jp2.add(jb9);
    jp2.add(jb10);
   
    JButton jbb=  new JButton("退出");
    JButton jbb1= new JButton("取消");
   
   
    jp3.add(jbb);
    jp3.add(jbb1);
   
    JButton jby = new JButton("+");
    JButton jby2= new JButton("-");
    JButton jby3= new JButton("*");
    JButton jby4= new JButton("/");
    JButton jby5= new JButton("=");
   
    jp4.add(jby);
    jp4.add(jby2);
    jp4.add(jby3);
    jp4.add(jby4);
    jp4.add(jby5);
   
    c.add(jp1);
    c.add(jp2);
    c.add(jp4);
    c.add(jp3);
 
   
    jb1.addActionListener(this);
    jb2.addActionListener(this);
    jb3.addActionListener(this);
    jb4.addActionListener(this);
    jb5.addActionListener(this);
    jb6.addActionListener(this);
    jb7.addActionListener(this);
    jb8.addActionListener(this);
   
   
    jb9.addActionListener(this);
    jb10.addActionListener(this);
    jby.addActionListener(this);
    jby2.addActionListener(this);
    jby3.addActionListener(this);
    jby4.addActionListener(this);
    jby5.addActionListener(this);
    jbb.addActionListener(this);
    jbb1.addActionListener(this);
   
  
 }

 public void actionPerformed(ActionEvent e) {
   
     System.out.println(e.getActionCommand());
   
   
     double d =0;
     if(e.getActionCommand()=="+"){
       s= jt.getText();
       jt.setText("");
       old ="+";
      
     }else if(e.getActionCommand()=="-"){
      s= jt.getText();
      jt.setText("");
      old="-";
     }else if(e.getActionCommand()=="*"){
      s= jt.getText();
      jt.setText("");
      old="*";
     
     }else if(e.getActionCommand()=="/"){
      s= jt.getText();
      jt.setText("");
      old ="/" ;
     
     }else if(e.getActionCommand()=="="){
      System.out.println(old);
      String ss= jt.getText();
         double dd= Double.parseDouble(ss);
         d = Double.parseDouble(s);
         double sum=0;
        if(old=="+"){
           sum =dd+d ;
        String s3= String.valueOf(sum);
        jt.setText(s3);
        }else if(old=="-"){
           sum =d-dd ;
         String s3= String.valueOf(sum);
         jt.setText(s3);
        }else if(old=="*"){
           sum =dd*d ;
         String s3= String.valueOf(sum);
         jt.setText(s3);
        }else if(old=="/"){
           sum =d/dd ;
         String s3= String.valueOf(sum);
         jt.setText(s3);
        }
       
     }else if(e.getActionCommand()=="取消"){
       jt.setText("");
       old=null;
     }else if(e.getActionCommand()=="退出"){
      System.exit(0);
     }
     else {
      jt.setText(jt.getText()+e.getActionCommand());
     }
  
 }

}

原创粉丝点击