import java.awt.*;
import java.awt.event.ActionListener;

import javax.swing.*;

class MainClass{
	public static void main(String[] args) {
		
		// window 창 설정
		JFrame frame = new JFrame("simple calculator");
		frame.setLocation(500, 400);
		frame.setPreferredSize(new Dimension(300, 200));
		Container contentPane = frame.getContentPane();

		JTextField resultField = new JTextField();
		contentPane.add(resultField, BorderLayout.NORTH);

		GridLayout button_layout = new GridLayout(4, 4);
		JPanel panel_button = new JPanel();
		panel_button.setLayout(button_layout);

		
		JButton[] nums = new JButton[10];
		nums[0] = new JButton("0");
		nums[1] = new JButton("1");
		nums[2] = new JButton("2");
		nums[3] = new JButton("3");
		nums[4] = new JButton("4");
		nums[5] = new JButton("5");
		nums[6] = new JButton("6");
		nums[7] = new JButton("7");
		nums[8] = new JButton("8");
		nums[9] = new JButton("9");
		
		JButton clr = new JButton("C"); // clear
		JButton equals = new JButton("="); // result
		JButton add = new JButton("+");
		JButton sub = new JButton("-");
		JButton mul = new JButton("*");
		JButton div = new JButton("/");

		panel_button.add(nums[7]);
		panel_button.add(nums[8]);
		panel_button.add(nums[9]);
		panel_button.add(add);
		panel_button.add(nums[4]);
		panel_button.add(nums[5]);
		panel_button.add(nums[6]);
		panel_button.add(sub);
		panel_button.add(nums[1]);
		panel_button.add(nums[2]);
		panel_button.add(nums[3]);
		panel_button.add(mul);
		panel_button.add(nums[0]);
		panel_button.add(equals);
		panel_button.add(clr);
		panel_button.add(div);

		contentPane.add(panel_button, BorderLayout.CENTER);

		// event
		
		ActionListener numberPressListener = new NumberButtonListener(resultField);
		for(int i = 0; i < 10; i++) {
			nums[i].addActionListener(numberPressListener);
		}

		clr.addActionListener(numberPressListener);
		equals.addActionListener(numberPressListener);
		add.addActionListener(numberPressListener);
		sub.addActionListener(numberPressListener);
		mul.addActionListener(numberPressListener);
		div.addActionListener(numberPressListener);
		
		// window 띄우기
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class NumberButtonListener implements ActionListener {
	JButton btn;
	JTextField result;

	private int total = 0;
	private char op = ' ';

	NumberButtonListener(JTextField result) {
		this.result = result;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String getBut = e.getActionCommand();
		String presentVal = result.getText();

		if (getBut == "0") {
			if(!presentVal.equals("0")) {
				result.setText(presentVal + getBut);
			}
		} else if (getBut == "1") {
			result.setText(presentVal + getBut);
		} else if (getBut == "2") {
			result.setText(presentVal + getBut);
		} else if (getBut == "3") {
			result.setText(presentVal + getBut);
		} else if (getBut == "4") {
			result.setText(presentVal + getBut);
		} else if (getBut == "5") {
			result.setText(presentVal + getBut);
		} else if (getBut == "6") {
			result.setText(presentVal + getBut);
		} else if (getBut == "7") {
			result.setText(presentVal + getBut);
		} else if (getBut == "8") {
			result.setText(presentVal + getBut);
		} else if (getBut == "9") {
			result.setText(presentVal + getBut);
		} else if (getBut == "C") {
			result.setText("");
			total = 0;
		} else if (getBut == "+") {
			int temp =  Integer.parseInt(presentVal);
			switch (op) {
			case '+':
				total += temp;
				break;
			case '-':
				total -= temp;
				break;
			case '*':
				total *= temp;
				break;
			case '/':
				total /= temp;
				break;
			default:
				total = temp;
			}
			op = '+';
			result.setText("");
		} else if (getBut == "-") {
			int temp =  Integer.parseInt(presentVal);
			switch (op) {
			case '+':
				total += temp;
				break;
			case '-':
				total -= temp;
				break;
			case '*':
				total *= temp;
				break;
			case '/':
				total /= temp;
				break;
			default:
				total = temp;
			}
			op = '-';
			result.setText("");
		} else if (getBut == "*") {
			int temp =  Integer.parseInt(presentVal);
			switch (op) {
			case '+':
				total += temp;
				break;
			case '-':
				total -= temp;
				break;
			case '*':
				total *= temp;
				break;
			case '/':
				total /= temp;
				break;
			default:
				total = temp;
			}
			op = '*';
			result.setText("");
		} else if (getBut == "/") {
			int temp =  Integer.parseInt(presentVal);
//			total = total / Integer.parseInt(presentVal);
			switch (op) {
			case '+':
				total += temp;
				break;
			case '-':
				total -= temp;
				break;
			case '*':
				total *= temp;
				break;
			case '/':
				total /= temp;
				break;
			default:
				total = temp;
			}
			op = '/';
			result.setText("");
		} else if (getBut == "=") {
			int temp = Integer.parseInt(presentVal);
			switch (op) {
			case '+':
				total += temp;
				break;
			case '-':
				total -= temp;
				break;
			case '*':
				total *= temp;
				break;
			case '/':
				total /= temp;
				break;
			default:
				total = temp;
			}
			op = ' ';
			result.setText("" + total);
			System.out.println(total);
		}
	}

}

+ Recent posts