zoukankan      html  css  js  c++  java
  • 基于简单工厂模式的计算器程序

    这个计算器是我学Java时写的第一个Swing程序,后来我读《大话设计模式》的第一章简单工厂模式恰好也是计算器,于是就改进了之前这个。

    源代码下载:https://github.com/myCodingTrip/Calculator

    运算类Operation.java

    public abstract class Operation {
        protected double numberA = 0;
        protected double numberB = 0;
        protected double result = 0;
        
        public double getNumberA() {
            return numberA;
        }
        public void setNumberA(double numberA) {
            this.numberA = numberA;
        }
        public double getNumberB() {
            return numberB;
        }
        public void setNumberB(double numberB) {
            this.numberB = numberB;
        }
        public abstract double getResult();
    }
    
    class OperationAdd extends Operation{
        @Override
        public double getResult() {
            double result = 0;
            result = numberA + numberB;
            return result;
        }
    }
    class OperationSub extends Operation{
        @Override
        public double getResult() {
            result = numberA - numberB;
            return result;
        }
    }
    class OperationMul extends Operation{
        @Override
        public double getResult() {
            result = numberA * numberB;
            return result;
        }
    }
    class OperationDiv extends Operation{
        @Override
        public double getResult(){
            try {
                result = numberA / numberB;
            } catch (Exception e) {
                System.out.println("除数不能为0");
                e.printStackTrace();
            }
            return result;
        }
    }
    工厂类 OperationFactory.java
    //简单工厂模式
    //只需要传入运算符号,工厂就实例化出合适的对象,用过多态,返回父类的方式实现计算器的结果
    public class OperationFactory {
        public static Operation createOperate(char sign) {
            Operation operation = null;
            switch (sign) {
            case '+':
                operation = new OperationAdd();
                break;
            case '-':
                operation = new OperationSub();
                break;
            case '*':
                operation = new OperationMul();
                break;
            case '/':
                operation = new OperationDiv();
                break;
            default:
                break;
            }
            return operation;
        }
    }
    计算器界面CalculateFrame.java
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    @SuppressWarnings("serial")
    public class CalculateFrame extends JFrame{
        
        private JTextField textData1, textData2, textResult;
        private JLabel labelNum1, labelNum2, labelResult;
        private JRadioButton radioAdd, radioSub, radioMul, radioDiv;
        private JButton btnCalculate;
        private Operation oper;
        //进行运算的两个数字和结果
        private String data1, data2, operation;
        private double resultNum;
        public CalculateFrame(){        
            //设置窗口基本属性
            this.setTitle("简单计算器");
            this.setSize(300, 200);
            //本机分辨率为1366*768
            this.setLocation(1366/2-300/2, 768/2-200/2);
            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
            this.addWindowListener(new WindowAdapter(){
                @Override
                public void windowClosing(WindowEvent e) {
                    setVisible (false);
                    System.exit(0);
                }
            });
            
            initView();
            initLayout();
            this.setVisible(true);//显示窗口
            btnCalculate.addActionListener(new ButtonListener());    
        }
        
        //设置一些功能区域
        public void initView() {
            textData1 = new JTextField();
            textData2 = new JTextField();
            textResult = new JTextField();
            textResult.setEnabled(false);
            labelNum1 = new JLabel("第一个数:");
            labelNum2 = new JLabel("第二个数:");
            labelResult = new JLabel("结果:");
            btnCalculate = new JButton("计算");
            radioAdd = new JRadioButton("+");
            radioSub = new JRadioButton("-");
            radioMul = new JRadioButton("*");
            radioDiv = new JRadioButton("/");
        }
        
        //将控件加入到布局中
        public void initLayout() {
            JPanel p1 = new JPanel(new GridLayout(1,2));
            p1.add(labelNum1);
            p1.add(textData1);
            JPanel p2 = new JPanel(new GridLayout(1,4));
            p2.add(radioAdd);
            p2.add(radioSub);
            p2.add(radioMul);
            p2.add(radioDiv);    
            JPanel p3 = new JPanel(new GridLayout(1,2));
            p3.add(labelNum2);
            p3.add(textData2);
            JPanel p4 = new JPanel(new GridLayout(1,2));
            p4.add(labelResult);
            p4.add(textResult);
            JPanel p5 = new JPanel(new GridLayout(1,1));
            p5.add(btnCalculate);    
            this.setLayout(new GridLayout(5,1));        
            this.add(p1);
            this.add(p2);
            this.add(p3);
            this.add(p4);
            this.add(p5);        
            ButtonGroup group = new ButtonGroup();
            group.add(radioAdd);
            group.add(radioSub);
            group.add(radioMul);
            group.add(radioDiv);
        }
        class ButtonListener implements ActionListener{
            //从程序中读入数据
            public void actionPerformed(ActionEvent e) {
                data1 = textData1.getText();
                data2 = textData2.getText();
                
                operation = "";
                if(radioAdd.isSelected())        operation = radioAdd.getText();
                else if(radioSub.isSelected())    operation = radioSub.getText();                
                else if(radioMul.isSelected())    operation = radioMul.getText();                
                else if(radioDiv.isSelected())    operation = radioDiv.getText();
                
                //将运算符号传入工厂中进行实例化
                oper = OperationFactory.createOperate(operation.charAt(0));
                oper.numberA = Double.parseDouble(data1);
                oper.numberB = Double.parseDouble(data2);
                resultNum = oper.getResult();
                textResult.setText(String.valueOf(resultNum));
            }
        }    
    }
    主程序 Main.java
    public class Main {
        public static void main(String[] args) {
            new CalculateFrame();
        }
    }

    程序界面:

  • 相关阅读:
    洛谷—— P1196 银河英雄传说
    MySQL练习题参考答案
    January 16 2017 Week 3 Monday
    January 15 2017 Week 3 Sunday
    January 14 2017 Week 2nd Saturday
    January 13 2017 Week 2 Friday
    January 12 2017 Week 2 Thursday
    January 11 2017 Week 2nd Wednesday
    January 10 2017 Week 2nd Tuesday
    January 09 2017 Week 2nd Monday
  • 原文地址:https://www.cnblogs.com/mycd/p/5716328.html
Copyright ? 2011-2022 开发猿


http://www.vxiaotou.com