Javaで計算機を書く

4731 ワード

//       ,                          java          
package gui.hbut.edu.cn.www;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
	private JTextField display;                            //           
	JPanel jPanel;
	JPanel numbersPanel;
	
	final static int ADD = 1;                              //  , , ,        ,    
	final static int SUB = 2;
	final static int MULT = 3;
	final static int DIV = 4;
	private double number1 = 0.0,number2 = 0.0,result = 0.0;
	private int operation = 0;
	private boolean isCalculating = false;                 //    
	final private String [] buttonText = { "←", "CE", "C", "+", "7", "8", "9", "-", "4", 
			"5", "6", "*", "1", "2", "3", "/", "+/-", "0", ".", "=" };   //    
	public Calculator(){
		jPanel = new JPanel();
		display = new JTextField("0");
		display.setPreferredSize(new Dimension(240, 35));    //     
		display.setFont(new Font("", Font.PLAIN, 20));
		display.setHorizontalAlignment(JTextField.RIGHT);
		display.setEditable(false);
		jPanel.add(display, "North");
		
		numbersPanel = new JPanel();
		numbersPanel.setLayout(new GridLayout(5,4,12,25));
		
		for(int i = 0; i < buttonText.length; i++){
			JButton button = new JButton(buttonText[i]);
			numbersPanel.add(button);
			button.addActionListener(this);
		}
		jPanel.add(numbersPanel, "South");
		getContentPane().add(jPanel);
		setTitle("Calculator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setBounds(100,100,250,320);
		setResizable(false);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e){
		String cmd = e.getActionCommand();             //         
		if(Character.isDigit(cmd.charAt(0)) || cmd.equals(".")){ 
			if(isCalculating){
				display.setText("0");
				isCalculating = false;
			}
			
			if(display.getText().equals("0")){  //   0
				if(cmd.equals("0"))
					return ;
				else if(cmd.equals("."))
					display.setText("0.");
				else
					display.setText(cmd);
			}else{                              //    0
				if(!display.getText().contains("."))
					display.setText(display.getText() + cmd);
				else if(!cmd.equals(".")){
					display.setText(display.getText() + cmd);
				}
			}
			
		}else{
			if(cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")){
				number1 = Double.parseDouble(display.getText());
				display.setText("");
				if(cmd.equals("+"))
					operation = ADD;
				else if(cmd.equals("-"))
					operation = SUB;
				else if(cmd.equals("*"))
					operation = MULT;
				else
					operation = DIV;
			}
			else if(cmd.equals("C")){   //       
				number1 = 0;
				number2 = 0;
				result = 0;
				display.setText("0");
			}
			else if(cmd.equals("CE")){  //        
				if(number2 == 0)
					number1 = 0;
				else
					number2 = 0;
			}
			else if(cmd.equals("←")){   //   
				if(display.getText().length() == 1)
					display.setText("0");
				else
					display.setText(display.getText().substring(0, display.getText().length()-1));
			}
			 else if (cmd.equals("+/-")) {  //          
					if (display.getText().equals("0")) {
						return;
					}
					if (display.getText().contains("-")) {
						display.setText(display.getText().substring(1));
					} else {
						display.setText("-" + display.getText());
					}
			 }
			 else if(cmd.equals("="))
			 {
				 number2 = Double.parseDouble(display.getText());
				 switch (operation) {//           
					case 1:
						result = number1 + number2;
						break;
					case 2:
						result = number1 - number2;
						break;
					case 3:
						result = number1 * number2;
						break;
					case 4:
						result = number1 / number2;
						break;
					default:
						result = number2;
					}
				 	Number resultObject = result;
					if ((resultObject.doubleValue() - resultObject.intValue()) < 1e-10) {
						String resultString = resultObject.intValue() + "";
						display.setText(resultString);
					} 
					else 
						display.setText(result + "");
					result = 0.0;
					number1 = 0.0;
					number2 = 0.0;
			 }
			
		}
	}
	
	
	public static void main(String []args){
		Calculator calculator = new Calculator();
	}
}