Java Calculator

Java Calculator

package Calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CalculatorDome extends JFrame implements ActionListener {
    //全局变量
    private final String[] components = {"7","8","9","+","4","5","6","-","1","2","3","*",".","0","=","/"};
    private boolean flag_firstNumber = true;
    String operator = "=";
    double digital = 0.0;
    String first_number = "";
    String second_number = "";
    JButton []numbers;
    JButton CE;

    JTextField message;
    //constructor
    CalculatorDome(){
        super("Calculator");
        //initialize
        numbers = new JButton[components.length];
        CE = new JButton("CE");
        message = new JTextField("");
        //create container
        Container container = this.getContentPane();
        JPanel upperpanel = new JPanel();
        upperpanel.setLayout(new BorderLayout());
        upperpanel.add("Center",message);
        upperpanel.add("East",CE);
        JPanel lowerpanel = new JPanel();
        lowerpanel.setLayout(new GridLayout(4,4));
        for(int i = 0;i < components.length; i++)
        {
            numbers[i] = new JButton(components[i]);
            lowerpanel.add(numbers[i]);
            numbers[i].addActionListener(this);
        }

        container.setLayout(new BorderLayout());
        container.add("North",upperpanel);
        container.add("Center",lowerpanel);

        CE.addActionListener(this);
        message.addActionListener(this);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800,800);
        this.setVisible(true);
        this.setBackground(Color.GRAY);
    }

    public static void main(String[] args){ new CalculatorDome();   }

    @Override
    public void actionPerformed(ActionEvent e){
        Object target = e.getSource();
        String label = e.getActionCommand();

        if(target == CE){   handleCE();    }
        else if("0123456789.".indexOf(label) >= 0){
            if(flag_firstNumber)        {   first_number +=label;   }
            else if (!flag_firstNumber) {   second_number +=label;  }
            handleNumber(label);
        }
        else{
            handleOperator(label);
            flag_firstNumber = false;
        }
    }

    public void handleCE(){
        message.setText("");
        flag_firstNumber = true;
        digital = 0.0;
        first_number = "";
        second_number = "";
    }

    public void handleNumber(String key){   message.setText(message.getText() + key);  }

    public void handleOperator(String key){
        message.setText(message.getText() + key);

        if(flag_firstNumber) {  operator = key; }
        else {
    //          System.out.println(first_number);
    //          System.out.println(second_number);
                double a = Double.parseDouble(first_number);
                double b = Double.parseDouble(second_number);
                if(operator.equals("+"))        {   digital = a+b;  }
                else if(operator.equals("-"))   {   digital = a-b;  }
                else if(operator.equals("/"))   {   digital = a/b;  }
                else if(operator.equals("*"))   {   digital = a*b;  }
                message.setText("" + digital);
        }
    }
}

你可能感兴趣的:(Java)