Swing计算器

昨天写的,只实现了加减乘除以及取消的功能
o(╯□╰)o,不能发附件额.....
贴代码,仅供参考

我定义了3个类1个接口
1、Calculate接口:定义了计算方法
2、Calculator类 该类实现了Calculate中定义的方法
3、OperatorFactory类:负责方法的调用
4、CalculatorPage类:计算器的主页面

Code:
  1. public class OperatorFactory {   
  2.     public static double createOperator (double numA, double numB, char operate) {   
  3.         Calculator operation = new Calculator(numA, numB);   
  4.         double result = 0;   
  5.         switch (operate) {   
  6.         case '+':   
  7.             result = operation.operatorAdd();   
  8.             break;   
  9.         case '-':   
  10.             result = operation.operatorSub();   
  11.             break;   
  12.         case '*':   
  13.             result = operation.operatorMul();   
  14.             break;   
  15.         case '/':   
  16.             result = operation.operatorDiv();   
  17.             break;   
  18.         default:   
  19.             break;   
  20.         }   
  21.         return result;   
  22.     }   
  23. }  
Code:
  1. public class Calculator implements Calculate {   
  2.     double numA;   
  3.     double numB;   
  4.   
  5.     public Calculator(double numA, double numB) {   
  6.         super();   
  7.         this.numA = numA;   
  8.         this.numB = numB;   
  9.     }   
  10.   
  11.     // 实现加法运算   
  12.     public double operatorAdd() {   
  13.   
  14.         return numA + numB;   
  15.     }   
  16.     // 实现除法运算   
  17.     public double operatorDiv() {   
  18.   
  19.         return numA / numB;   
  20.     }   
  21.     // 实现乘法运算   
  22.     public double operatorMul() {   
  23.   
  24.         return numA * numB;   
  25.     }   
  26.     // 实现减法运算   
  27.     public double operatorSub() {   
  28.   
  29.         return numA - numB;   
  30.     }   
  31. }  
Code:
  1. public interface Calculate {   
  2.     /**  
  3.      * 加法运算  
  4.      * @return  返回计算结果  
  5.      */  
  6.     public double  operatorAdd();   
  7.     /**  
  8.      * 减法运算  
  9.      * @return  返回计算结果  
  10.      */  
  11.     public double  operatorSub();   
  12.     /**  
  13.      * 乘法运算  
  14.      * @return  返回计算结果  
  15.      */  
  16.     public double operatorMul();   
  17.     /**  
  18.      * 除法运算  
  19.      * @return  返回计算结果  
  20.      */  
  21.     public double  operatorDiv();   
  22. }  

 

Code:
  1. public class CalculatorPage extends JFrame {   
  2.     private static final long serialVersionUID = 3909257112490231420L;   
  3.   
  4.     public CalculatorPage() {   
  5.         super("我的计算器");   
  6.         initComponents();   
  7.     }   
  8.     private void initComponents() {   
  9.         initJplMain();   
  10.         setBounds(450150, WIDTH, HEIGTH);   
  11.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  12.         setResizable(false);   
  13.         add(jplMain);   
  14.     }   
  15.     public void initJplMain() {   
  16.         initJplKey();   
  17.         jtfRes = new JTextField();   
  18.         jplMain = new JPanel();   
  19.         GroupLayout layout = new GroupLayout(jplMain);   
  20.         jplMain.setLayout(layout);   
  21.         layout.setAutoCreateGaps(true);   
  22.         layout.setAutoCreateContainerGaps(true);   
  23.         SequentialGroup hGroup = layout.createSequentialGroup();   
  24.         hGroup.addGroup(layout.createParallelGroup(Alignment.LEADING)   
  25.                 .addGap(50).addComponent(jtfRes).addComponent(jplKey));   
  26.         layout.setHorizontalGroup(hGroup);   
  27.         SequentialGroup vGroup = layout.createSequentialGroup();   
  28.         vGroup.addGap(50).addGroup(   
  29.                 layout.createParallelGroup().addComponent(jtfRes));   
  30.         vGroup.addGroup(layout.createParallelGroup().addComponent(jplKey))   
  31.                 .addGap(30);   
  32.         layout.setVerticalGroup(vGroup);   
  33.     }   
  34.   
  35.     /**  
  36.      * 初始化按键界面  
  37.      */  
  38.     private void initJplKey() {   
  39.         jplKey = new JPanel();   
  40.         initTbnVec();   
  41.         jplKey.setLayout(new GridLayout(5433));   
  42.         for (int i = 0; i < 20; i++)   
  43.             jplKey.add(tbn_Vec.get(i));   
  44.         setSize(232300);   
  45.     }   
  46.     /**  
  47.      * 初始化按键;并为它们添加事件监听器  
  48.      */  
  49.     private void initTbnVec() {   
  50.         tbn_Vec = new Vector<JButton>();   
  51.         for (int i = 0; i < 20; i++)   
  52.             tbn_Vec.add(i, new JButton());   
  53.         tbn_Vec.get(3).setText("C");   
  54.         tbn_Vec.get(4).setText("7");   
  55.         tbn_Vec.get(5).setText("8");   
  56.         tbn_Vec.get(6).setText("9");   
  57.         tbn_Vec.get(7).setText("+");   
  58.         tbn_Vec.get(8).setText("4");   
  59.         tbn_Vec.get(9).setText("5");   
  60.         tbn_Vec.get(10).setText("6");   
  61.         tbn_Vec.get(11).setText("-");   
  62.         tbn_Vec.get(12).setText("1");   
  63.         tbn_Vec.get(13).setText("2");   
  64.         tbn_Vec.get(14).setText("3");   
  65.         tbn_Vec.get(15).setText("*");   
  66.         tbn_Vec.get(16).setText("0");   
  67.         tbn_Vec.get(17).setText(".");   
  68.         tbn_Vec.get(18).setText("=");   
  69.         tbn_Vec.get(19).setText("/");   
  70.         for (int i = 3; i < 20; i++) {   
  71.             if (i == 4 | i == 5 | i == 6 | i == 8 | i == 9 | i == 10 | i == 12  
  72.                     | i == 13 | i == 14 | i == 16 | i == 17) {   
  73.                 tbn_Vec.get(i).addActionListener(new ActionListener() {   
  74.                     public void actionPerformed(ActionEvent evt) {   
  75.                         digitActionPerformed(evt);   
  76.                     }   
  77.                 });   
  78.             } else if (i == 7 | i == 11 | i == 15 | i == 19) {   
  79.                 tbn_Vec.get(i).addActionListener(new ActionListener() {   
  80.                     public void actionPerformed(ActionEvent evt) {   
  81.                         operatorActionPerformed(evt);   
  82.                     }   
  83.                 });   
  84.             } else if (i == 18) {   
  85.                 tbn_Vec.get(i).addActionListener(new ActionListener() {   
  86.                     public void actionPerformed(ActionEvent evt) {   
  87.                         calculatorActionPerformed(evt);   
  88.                     }   
  89.                 });   
  90.             } else {   
  91.                 tbn_Vec.get(i).addActionListener(new ActionListener() {   
  92.                     public void actionPerformed(ActionEvent evt) {   
  93.                         resetActionPerformed(evt);   
  94.                     }   
  95.                 });   
  96.             }   
  97.         }   
  98.   
  99.     }   
  100.     protected void calculatorActionPerformed(ActionEvent evt) {   
  101.         if ("" != strNumA & "" != strNum) {   
  102.             numA = Double.parseDouble(strNumA);   
  103.             numB = Double.parseDouble(strNum);   
  104.         }   
  105.         if ("+".equals(operatorStr)) {   
  106.             operate = '+';   
  107.         } else if ("-".equals(operatorStr)) {   
  108.             operate = '-';   
  109.         } else if ("*".equals(operatorStr)) {   
  110.             operate = '*';   
  111.         } else {   
  112.             operate = '/';   
  113.         }   
  114.         if (numB == 0 & operate == '/') {   
  115.             JOptionPane.showMessageDialog(this"除数不能为零!");   
  116.             return;   
  117.         }   
  118.         double result = OperatorFactory.createOperator(numA, numB, operate);   
  119.         jtfRes.setText(result + "");   
  120.         strNumA = "";   
  121.         strNum = "";   
  122.         numA = result;   
  123.     }   
  124.   
  125.     protected void resetActionPerformed(ActionEvent evt) {   
  126.         // TODO Auto-generated method stub   
  127.         strNum = "";   
  128.         jtfRes.setText("");   
  129.         numA=0;   
  130.         numB=0;   
  131.     }   
  132.   
  133.     private void operatorActionPerformed(ActionEvent evt) {   
  134.         // TODO Auto-generated method stub   
  135.         strNumA = strNum;   
  136.         strNum = "";   
  137.         operatorStr = ((JButton) evt.getSource()).getText();   
  138.     }   
  139.   
  140.     private void digitActionPerformed(ActionEvent evt) {   
  141.         // TODO Auto-generated method stub   
  142.         strNum += ((JButton) evt.getSource()).getText();   
  143.         jtfRes.setText(strNum);   
  144.     }   
  145.   
  146.     public static void main(String args[]) {   
  147.         java.awt.EventQueue.invokeLater(new Runnable() {   
  148.             public void run() {   
  149.                 new CalculatorPage().setVisible(true);   
  150.             }   
  151.         });   
  152.     }   
  153.     private double numA;   
  154.     private double numB;   
  155.     private String strNum = "";   
  156.     private String strNumA;   
  157.     private String operatorStr;   
  158.     private char operate;//操作符   
  159.     private JPanel jplMain;//主界面   
  160.     private JPanel jplKey;// 用来显示按键   
  161.     private JTextField jtfRes;;// 用来显示结果   
  162.     private Vector<JButton> tbn_Vec;//用来存储按键   
  163.     private final int WIDTH = 250;//初始化宽   
  164.     private final int HEIGTH = 350;//初始化高   
  165. }  

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值