计算器

 

package hxl.jsj;

import java.awt.*;//导入AWT包
import java.awt.event.*;//导入事件处理包
import javax.swing.*;

//继承一个类
public class JsqClass extends JFrame {
 //声明
 JTextField t;
 JPanel p1,p2,p3,p4,p5;
 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf,bg,bh,bi,bj,bk,bl,bm,bn;
 String s1,s2,s3;
 double u1,u2,u3;
 char op;
 
 // 设置各种方法参数
 public JsqClass() {
  t = new JTextField(" ");
  //tt = new JTextField(" ");
  //Label l = new Label(" 我的计算器");
  //p = new JPanel();
  p1 = new JPanel();
  p2 = new JPanel();
  p3 = new JPanel();
  p4 = new JPanel();
  p5 = new JPanel();
  //p6 = new JPanel();
  //十个数字按钮
  b0 = new JButton("0");
  b1 = new JButton("1");
  b2 = new JButton("2");
  b3 = new JButton("3");
  b4 = new JButton("4");
  b5 = new JButton("5");
  b6 = new JButton("6");
  b7 = new JButton("7");
  b8 = new JButton("8");
  b9 = new JButton("9"); 
  //bbb=new JButton("exit");
  //6个
  ba = new JButton("+");
  bb = new JButton("-");
  bc = new JButton("*");
  bd = new JButton("/");
  be = new JButton(".");
  bf = new JButton("=");
  bg = new JButton("sin");
  bh = new JButton("cos");
  bi = new JButton("tan");
  bj = new JButton("sqrt");
  bk = new JButton("CLEAR");
  bl = new JButton("exp");
  bm = new JButton("log");
  bn = new JButton("abs");

  this.setSize(450, 600);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("我的计算器");
  this.setLayout(new GridLayout(5, 1, 3, 3));
  this.setBackground(Color.blue);
  t.setFont(new java.awt.Font("Dialog", 0, 20));//名称,样式,大小
  //l.setFont(new java.awt.Font("Dialog", 0, 30));
  //tt.setFont(new java.awt.Font("Dialog", 0, 20));
  // 为按钮和面板添加底色
  // 添加面板
  //this.add(p);
  this.add(p1);
  this.add(p2);
  this.add(p3);
  this.add(p4);
  this.add(p5);
  //this.add(p6);
  // 设置面板
  //p.setLayout(new BorderLayout());
  //p.add(l);
  p1.setLayout(new BorderLayout());
  p1.add(t,BorderLayout.NORTH);
  p2.setLayout(new GridLayout(1, 6));
  p3.setLayout(new GridLayout(1, 6));
  p4.setLayout(new GridLayout(1, 6));
  p5.setLayout(new GridLayout(1, 6));
  //p6.setLayout(new GridLayout(1, 6, 3, 3));
  // 添加按钮
  p2.add(b0);
  p2.add(b1);
  p2.add(b2);
  p2.add(b3);
  p2.add(bg);
  p2.add(bk);
  
  p3.add(b4);
  p3.add(b5);
  p3.add(b6);
  p3.add(b7);
  p3.add(bh);
  p3.add(bl);
  
  p4.add(b8);
  p4.add(b9);
  p4.add(ba);
  p4.add(bb);
  p4.add(bm);
  p4.add(bi);
  
  p5.add(bc);
  p5.add(bd);
  p5.add(be);
  p5.add(bf);
  p5.add(bj);
  p5.add(bn);
  //p6.add(bbb);
  
  // 添加各种监听器
  b0.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "0";
    t.setText(s);
   }
  });
  b1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "1";
    t.setText(s);
   }
  });
  b2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "2";
    t.setText(s);
   }
  });
  b3.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "3";
    t.setText(s);
   }
  });
  b4.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "4";
    t.setText(s);
   }
  });
  b5.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "5";
    t.setText(s);
   }
  });
  b6.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "6";
    t.setText(s);
   }
  });
  b7.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "7";
    t.setText(s);
   }
  });
  b8.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "8";
    t.setText(s);
   }
  });
  b9.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += "9";
    t.setText(s);
   }
  });
  bg.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b *= Math.PI / 180;
    b = Math.sin(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  bh.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b *= Math.PI / 180;
    b = Math.cos(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  bi.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b *= Math.PI / 180;
    b = Math.tan(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  bl.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b = Math.exp(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  bj.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b = Math.sqrt(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  // 开始设置功能键
  bm.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b = Math.log(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  bn.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    double b = Double.parseDouble(s);
    b = Math.abs(b);
    b = b * 10000;
    b = Math.rint(b);
    b = b / 10000;
    t.setText("" + b);
   }
  });
  bk.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    t.setText("");
   }
  });
  ba.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    s1 = t.getText();
    op = '+';
    t.setText("");
   }
  });
  bb.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    s1 = t.getText();
    op = '-';
    t.setText("");
   }
  });
  bc.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    s1 = t.getText();
    op = '*';
    t.setText("");
   }
  });
  bd.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    s1 = t.getText();
    op = '/';
    t.setText("");
   }
  });
  be.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = t.getText();
    s += ".";
    t.setText(s);
   }
  });
  bf.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    s2 = t.getText();
    double u1 = Double.parseDouble(s1);
    double u2 = Double.parseDouble(s2);
    if (op == '+')
     u3 = u1 + u2;
    if (op == '-')
     u3 = u1 - u2;
    if (op == '*')
     u3 = u1 * u2;
    if (op == '/')
     u3 = u1 / u2;
    t.setText("" + u3);
   }
  });
  // 添加退出按钮
 /* bbb.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });*/
 }

 // 设置主函数
 public static void main(String[] args) {
  new JsqClass();
  //w.pack();
  //w.show();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值