用java编写一个计算器swing_利用Java Swing 实现一个计算器

56e452ce1dcf74ec629cb6798e40082a.pngimport javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.math.BigDecimal;

//除零异常处理

class Divzero extends Exception {

Divzero(String msg) {

super(msg);

}

}

abstract class Operation { // 运算类

public abstract double operation(double x, double y);

}

class Add extends Operation { // 子类加法

public double operation(double x, double y) {

return x + y;

}

}

class Sub extends Operation { // 子类减法

public double operation(double x, double y) {

return x - y;

}

}

class Mul extends Operation { // 子类乘法

public double operation(double x, double y) {

return x * y;

}

}

class Div extends Operation { // 子类除法

public double operation(double x, double y) {

return x / y;

}

}

// 绑定运算符号对象

class Factory {

public static Operation factory(String operation) { //根据运算符创建相应的操作对象

switch (operation) {

case "+":

return new Add();

case "-":

return new Sub();

case "×":

return new Mul();

case "÷":

return new Div();

default: return null;

}

}

}

public class calculator {

// 创建按钮

private JPanel JPanel;

private JButton B8;

private JButton B9;

private JButton B7;

private JButton B5;

private JButton B6;

private JButton B4;

private JButton B2;

private JButton B3;

private JButton B1;

private JButton B0;

private JButton Bpoint;

private JButton Bsign;

private JButton Bc;

private JButton Bbc;

private JButton Bdiv;

private JButton Bsub;

private JButton Badd;

private J

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值