JAVA

[동현] 계산기 만들기_2

양상추상츄 2021. 10. 25. 16:55

https://www.youtube.com/watch?v=Z_heBwSwPn0&t=1s 

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; // 버튼입력값 받기
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame {
	
	private JTextField inputSpace;
	private String num ="";
	private ArrayList<String> equation = new ArrayList<String>();
	
	public Calculator() {
		setLayout(null);
		
		//메인 패널
		inputSpace = new JTextField();
		inputSpace.setEditable(false); // 편집 불가능하게 설정
		inputSpace.setBackground(Color.white);
		inputSpace.setHorizontalAlignment(JTextField.RIGHT);
		inputSpace.setFont(new Font("Arial", Font.BOLD, 50));
		inputSpace.setBounds(8, 10, 270, 70); //x, y 사이즈
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(4, 4, 10, 10)); 
		//가로 칸수, 세로 칸수, 좌우 간격, 상하간격
		buttonPanel.setBounds(8, 90, 270, 235);
		
		String button_names[] = {"C", "÷", "*", "=", "7", "8", "9", "+",
				"4", "5", "6", "-", "1", "2", "3", "0"};
		JButton buttons[] = new JButton[button_names.length];
		
		//계산기 외형
		for(int i = 0; i<button_names.length; i++) {
			buttons[i] = new JButton(button_names[i]);
			buttons[i].setFont(new Font("Arial", Font.BOLD, 20));
			if (button_names[i] == "C") 
				buttons[i].setBackground(Color.RED);
			else if ((i >= 4 && i <= 6 )||(i >= 8 && i <= 10 )||(i >= 8 && i <= 10 )||
					(i >= 12 && i <= 14 )) buttons[i].setBackground(Color.BLACK);
			else buttons[i].setBackground(Color.GRAY);
			buttons[i].setForeground(Color.WHITE);
			buttons[i].setBorderPainted(false); // 테두리 없애기
			buttons[i].addActionListener(new PadActionListener());
			buttonPanel.add(buttons[i]); //버튼을 버튼패널에 추가
		}
		
		add(inputSpace);
		add(buttonPanel);//버튼패널
		
		setTitle("계산기");
		setVisible(true);
		setSize(300, 370);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	
	//액션리스너,키보드 누른값 받아오기
	class PadActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String operation = e.getActionCommand();
			
			if(operation.equals("C")) {
				inputSpace.setText("");
			}else if (operation.equals("=")) {// = 버튼에 계산기능 추가
				String result = Double.toString(calculate(inputSpace.getText()));
				inputSpace.setText("" + result);
				num = "";
			}else {
				inputSpace.setText(inputSpace.getText() + e.getActionCommand());
			}
		}
	}
	
	private void fullTextParsing(String inputText) {
		equation.clear();
		
		for(int i =0; i<inputText.length(); i++){
			char ch = inputText.charAt(i);
			
			if (ch == '-' || ch == '+' || ch == '*' || ch == '÷') {
				equation.add(num);
				num = "";
				equation.add(ch+"");
			}else {
				num = num + ch;
			}
		}
		equation.add(num);
	}
	
	public double calculate(String inputText) {
		fullTextParsing(inputText);
		
		double prev = 0;
		double current = 0;
		String mode = "";
		
		for(String s : equation) {
			if(s.equals("+")) {
				mode = "add";
			}else if (s.equals("-")) {
				mode = "sub";
			}else if (s.equals("*")) {
				mode = "mul";
			}else if (s.equals("÷")) {
				mode = "div";
			}else {
				current = Double.parseDouble(s);//문자열로 들어오는 숫자를 소수로바꾸기
				if(mode =="add") {
					prev += current;
				}else if(mode == "sub") {
					prev -= current;
				}else if(mode == "mul") {
					prev *= current;
				}else if(mode == "div") {
					prev /= current;
				}else {
					prev = current;
				}
			}
		}
		return prev;
	}
	
	public static void main(String[] args) {
		new Calculator();

	}

}

*동현님 계산기만들기