一个Java简易计算器

//简易计算器

import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
//import java.lang.Math;
import java.math.BigDecimal;

public class SimpleCalculater extends JFrame{
 
 private String[] names={"+","-","*","/","^","CLEAR"};
 
 private JTextField         
 t1= new JTextField(10),
 t2=new JTextField(10),
 t3=new JTextField(10);
 private JButton[] buttons=new JButton[6];
 
 
 
 public static void main(String[] a){
  new SimpleCalculater("mqslll");
  
  
  
  
 }
 
 public SimpleCalculater(String title){
  super(title);
  
  
  
  Container contentPane=getContentPane();
  //contentPane.setLayout(new GridLayout(3,1));    

  JPanel buttonPanel=new JPanel();
  JPanel textPanel=new JPanel();
  JPanel x=new JPanel();
  textPanel.setLayout(new GridLayout(1,3));
  
  textPanel.add(t1);
  textPanel.add(t2);
  textPanel.add(t3);
  t1.setSize(40,30);
  t2.setSize(40,30);
  t3.setSize(40,30);
  buttonPanel.setLayout(new GridLayout(2,3));
  for(int i=0;i<buttons.length;i++){
   buttons[i]=new JButton(names[i]);
   buttonPanel.add(buttons[i]);
   
  }
  
  
  contentPane.add(buttonPanel,BorderLayout.NORTH);
  contentPane.add(textPanel,BorderLayout.CENTER);
  contentPane.add(buttonPanel,BorderLayout.SOUTH);
  
  buttons[0].addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
   String str1=t1.getText().toString();
   double a =Double.parseDouble(str1);
   String str2=t2.getText().toString();
   double b=Double.parseDouble(str2);
   double c=a+b;                                    //M
   t3.setText(new Double(c).toString());
  }});
  buttons[1].addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    
    String str1=t1.getText().toString();
    double a =Double.parseDouble(str1);
    String str2=t2.getText().toString();
    double b=Double.parseDouble(str2);
    //double c=a-b;
    double c=sub(a,b);
    
    t3.setText(new Double(c).toString());
    
   
   
  }});
  buttons[2].addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    
    String str1=t1.getText().toString();
    double a =Double.parseDouble(str1);
    String str2=t2.getText().toString();
    double b=Double.parseDouble(str2);
    double c=a*b;                                 

    t3.setText(new Double(c).toString());   
   
  }});
  buttons[3].addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    String str1=t1.getText().toString();
    double a =Double.parseDouble(str1);
    String str2=t2.getText().toString();
    double b=Double.parseDouble(str2);
    double c=a/b;
    t3.setText(new Double(c).toString());
   
   
  }});
  buttons[4].addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    String str1=t1.getText().toString();
    double a =Double.parseDouble(str1);
    String str2=t2.getText().toString();
    double b=Double.parseDouble(str2);
    double c=Math.pow(a,b);
    t3.setText(new Double(c).toString());
   
   
  }});
  buttons[5].addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
   t1.setText(null);
   t2.setText(null);
   t3.setText(null);
   
   
  }});
  
  
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  //pack();  

  setSize(300,180);
  setVisible(true);
  
  

   
  
 }
 
 public double sub(double d1,double d2){
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值