基于Java实现的一个简单计算器

迷你计算器

本计算器能够实现基本的加减乘除四则运算。项目基于AWT和Swing,利用Java语言实现。JDK的版本是1.7。

 

运行界面:

基于Java实现的一个简单计算器_第1张图片

 

代码:

import java.awt.BorderLayout;
import java.awt.Font;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Calculator extends JFrame implements ActionListener {
	// actions occur when window is closed
    private class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent we) {
            System.exit(0);
        }
     }
	
    // Strings shown on button
    private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*",
    		"1", "2", "3", "-", ".", "0", "=", "+" };
    // Button in Swing
    private JButton[] buttons = new JButton[str.length];
    // Reset button
    private JButton reset = new JButton("CE");
    
    private JTextField resultToDisplay = new JTextField("0");
    
    private boolean isFirstDigit = true;
    private String operator = "=";
    private double result = 0;
    
    // Constructor
    public Calculator() {
    	super("My Calculator");
    	
    	JPanel panelButton = new JPanel(new GridLayout(4, 4));
    	
    	for (int i = 0; i < str.length; i++) {
    		buttons[i] = new JButton(str[i]);
    		buttons[i].setFont(new Font("宋体", Font.BOLD, 40));
    		panelButton.add(buttons[i]);
    	}
    	// Panel to show result
    	JPanel panelResult = new JPanel(new BorderLayout());
    	// Add TestField of result and reset button
    	panelResult.add("Center", resultToDisplay);
    	panelResult.add("East", reset);
    	// Add two panels to one
    	getContentPane().setLayout(new BorderLayout());
    	getContentPane().add("North", panelResult);
    	getContentPane().add("Center", panelButton);
    	// Add action listener
    	for (int i = 0; i < str.length; i++) {
    		buttons[i].addActionListener(this);
    	}
    	reset.addActionListener(this);
    	resultToDisplay.addActionListener(this);
    	addWindowListener(new WindowCloser());
    	setSize(400, 400);
    	setVisible(true);
    	setResizable(false);
    	//pack();
    }
    
    // Implement method actionPerformed
    public void actionPerformed(ActionEvent e) {
    	Object monitoredObject = e.getSource();
    	String commandString = e.getActionCommand();
    	if (monitoredObject == reset) {
    		handleReset();
    	} else if ("0123456789.".indexOf(commandString) > 0) {
    		handleDigit(commandString);
    	} else {
    		handleOperator(commandString);
    	}
    }
    
    public void handleReset() {
    	resultToDisplay.setText("0");
    	isFirstDigit = true;
    	operator = "=";     	
    }
    
    public void handleDigit(String digit) {
    	if (isFirstDigit) {
    		resultToDisplay.setText(digit);
    	} else if (digit.equals(".") 
    			&& (resultToDisplay.getText().indexOf(".") < 0)) {
    		resultToDisplay.setText(resultToDisplay.getText() + ".");
    	} else if (!digit.equals(".")) {
    		resultToDisplay.setText(resultToDisplay.getText() + digit);
    	}
    	isFirstDigit = false;    	
    }
    
    public void handleOperator(String operator) {
    	if (!operator.equals("=")) {
        	result = Double.valueOf(resultToDisplay.getText());
        	this.operator = operator;
    	}
    	else {
    		if (this.operator.equals("+"))
                result += Double.valueOf(resultToDisplay.getText());
            else if (this.operator.equals("-"))
            	result -= Double.valueOf(resultToDisplay.getText());
            else if (this.operator.equals("*"))
            	result *= Double.valueOf(resultToDisplay.getText());
            else if (this.operator.equals("/"))
            	result /= Double.valueOf(resultToDisplay.getText());
            else {}
    	}
    	resultToDisplay.setText(String.valueOf(result));        
        isFirstDigit = true;    	
    }
    
	public static void main(String[] args) {
		new Calculator();
	}
}


 

你可能感兴趣的:(基于Java实现的一个简单计算器)