Repository

계산기 (Java Swing)

dev_zephyr 2021. 1. 27. 18:58

처음으로 만든 GUI 프로그램이었다.

 

지금 다시 코드를 리뷰해 보니,

엄청나게 치열했던 기억이 새록새록 나고

또, 고치고 싶은 코드도 많이 보인다.

 

그때의 그 풋풋함을 나중에도 기억하게 

그대로 남겨놔야겠다.

 

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.StringTokenizer;

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

public class MyLayout02 extends JFrame{
	
	MyFrame02 frame;
	
	ArrayList<String> list = new ArrayList<>(); // 화면에 보여지는 식을 저장.
	ArrayList<String> tmpNumList = new ArrayList<>(); // 계산에 쓸 숫자를 스트링으로 저장.
	ArrayList<Integer> numList = new ArrayList<>();	// 계산에 쓸 숫자를 저장.
	ArrayList<String> operList = new ArrayList<>();	// 계산에 쓸 연산자를 저장.
	
	String showNum = "";
	int result;
	
	JTextField showFormula = null;
	JTextField showNumber = null;		

	public MyLayout02() {
		
		frame = new MyFrame02();
		run();
		
	}
	
	
	public void run() {
		frame.setLayout(null);
		
		showFormula = new JTextField();
		showFormula.setFont(new Font("Consolas", Font.ITALIC, 25));
		showFormula.setHorizontalAlignment(JTextField.RIGHT);
		showFormula.setBounds(0, 0, 480, 40);
		frame.getContentPane().add(showFormula);
		
		showNumber = new JTextField();
		showNumber.setFont(new Font("Consolas", Font.ITALIC, 50));
		showNumber.setHorizontalAlignment(JTextField.RIGHT);
		showNumber.setBounds(0, 40, 480, 120);
		frame.getContentPane().add(showNumber);
		
		JButton b_MC = new JButton("MC");
		b_MC.setBounds(0, 160, 96, 80); 
		b_MC.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(b_MC);
		
		JButton b_MR = new JButton("MR");
		b_MR.setBounds(96, 160, 96, 80);
		b_MR.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(b_MR);
		
		JButton b_MS = new JButton("MS");
		b_MS.setBounds(192, 160, 96, 80);
		b_MS.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(b_MS);
		
		JButton b_MPlus = new JButton("M+");
		b_MPlus.setBounds(288, 160, 96, 80);
		b_MPlus.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(b_MPlus);
		
		JButton b_Mminus = new JButton("M-");
		b_Mminus.setBounds(384, 160, 96, 80);
		b_Mminus.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(b_Mminus);
		//
		
		JButton backSpace = new JButton("<-");
		backSpace.setBounds(0, 240, 96, 80);
		backSpace.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(backSpace);
		
		JButton ce = new JButton("CE");
		ce.setBounds(96, 240, 96, 80);
		ce.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(ce);
		
		JButton reset = new JButton("C");
		reset.setBounds(192, 240, 96, 80);
		reset.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(reset);
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				list.clear();
				numList.clear();
				operList.clear();
				tmpNumList.clear();
				showFormula.setText("");
				showNumber.setText("0");
			}
		});
		
		JButton plusMinus = new JButton("±");
		plusMinus.setBounds(288, 240, 96, 80);
		plusMinus.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(plusMinus);
		
		JButton root = new JButton("√");
		root.setBounds(384, 240, 96, 80);
		root.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(root);
		//
		
		JButton seven = new JButton("7");
		seven.setBounds(0, 320, 96, 80);
		seven.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(seven);
		seven.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("7");
				showNumber.setText(showNum);
			}
		});
		
		JButton eight = new JButton("8");
		eight.setBounds(96, 320, 96, 80);
		eight.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(eight);
		eight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("8");
				showNumber.setText(showNum);
			}
		});
		
		JButton nine = new JButton("9");
		nine.setBounds(192, 320, 96, 80);
		nine.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(nine);
		nine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("9");
				showNumber.setText(showNum);
			}
		});
		
		JButton div = new JButton("/");
		div.setBounds(288, 320, 96, 80);
		div.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(div);
		div.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showFormula.setText(inputOper("/"));;
			}
		});

		JButton remain = new JButton("%");
		remain.setBounds(384, 320, 96, 80);
		remain.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(remain);
		//
		
		JButton four = new JButton("4");
		four.setBounds(0, 400, 96, 80);
		four.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(four);
		four.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("4");
				showNumber.setText(showNum);
			}
		});
		
		JButton five = new JButton("5");
		five.setBounds(96, 400, 96, 80);
		five.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(five);
		five.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("5");
				showNumber.setText(showNum);
			}
		});
		
		JButton six = new JButton("6");
		six.setBounds(192, 400, 96, 80);
		six.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(six);
		six.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("6");
				showNumber.setText(showNum);
			}
		});
		
		JButton mul = new JButton("*");
		mul.setBounds(288, 400, 96, 80);
		mul.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(mul);
		mul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showFormula.setText(inputOper("*"));;
			}
		});
		
		JButton oneX = new JButton("1/x");
		oneX.setBounds(384, 400, 96, 80);
		oneX.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(oneX);
		//
		
		JButton one = new JButton("1");
		one.setBounds(0, 480, 96, 80);
		one.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(one);
		one.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("1");
				showNumber.setText(showNum);
			}
		});
		
		JButton two = new JButton("2");
		two.setBounds(96, 480, 96, 80);
		two.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(two);
		two.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("2");
				showNumber.setText(showNum);
			}
		});
		
		JButton three = new JButton("3");
		three.setBounds(192, 480, 96, 80);
		three.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(three);
		three.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("3");
				showNumber.setText(showNum);
			}
		});
		
		JButton sub = new JButton("-");
		sub.setBounds(288, 480, 96, 80);
		sub.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(sub);
		sub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showFormula.setText(inputOper("-"));;
			}
		});
		
		JButton equal = new JButton("=");
		equal.setBounds(384, 480, 96, 160);
		equal.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(equal);
		equal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				showFormula.setText(inputOper("="));;
				showNumber.setText(Integer.toString(calc()));
			}
		});
//
		
		JButton zero = new JButton("0");
		zero.setBounds(0, 560, 192, 80);
		zero.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(zero);
		zero.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				input("0");
				showNumber.setText(showNum);
			}
		});
		
		JButton point = new JButton(".");
		point.setBounds(192, 560, 96, 80);
		point.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(point);
		
		JButton add = new JButton("+");
		add.setBounds(288, 560, 96, 80);
		add.setFont(new Font("Consolas", Font.ITALIC, 30));
		frame.getContentPane().add(add);
		add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showFormula.setText(inputOper("+"));;
			}
		});
	}
	
	
	public void input(String input) {
		list.add(input);					// 화면에 보여지는 식에 저장하고
		showNum = showNum.concat(input);	// 화면에 보여지는 숫자에 저장
		
	}	
	
	public String inputOper(String oper) {	// 연산자 버튼을 누르면 식을 보여주는 화면에 여태까지 입력받은 숫자+연산자를 리턴해서 출력한다.
		list.add(oper);
		operList.add(oper);
		tmpNumList.add(showNum);			
		
		String tmp = "";
		for(int i=0; i<list.size(); i++) {	// 입력받은 모든것은 list에 저장되어 있으므로,
			if(i == 0) {
				tmp = list.get(i);			// 리스트 전체에 저장된 값을 하나의 스트링으로 
			} else {
				tmp = tmp.concat(list.get(i));	// 합친다.
			}
		}
		
		showNum = "";						// 화면에 보여질 숫자는 다시 처음부터 보여져야 하기 때문에 초기화한다.
		return tmp;
	}
	
	public int calc() {
		
		int num;
		
		for(int i=0; i<tmpNumList.size(); i++) {	// 숫자는 <String>배열에 담겨있기때문에,
			num = Integer.parseInt(tmpNumList.get(i));	// 계산을 위해 Int로 바꿔서 <Integer>배열에 담는다.
			numList.add(num);
		}
		
		for(int i=0; i<operList.size(); i++) {	// 계산한다.
			
			if(i == 0) {					
				switch(operList.get(i)) {
				case "+" : result = numList.get(i) + numList.get(i+1); break;
				case "-" : result = numList.get(i) - numList.get(i+1); break;
				case "*" : result = numList.get(i) * numList.get(i+1); break;
				case "/" : result = numList.get(i) / numList.get(i+1); break;
				}
			} else {
				switch(operList.get(i)) {
				case "+" : result += numList.get(i+1); break;
				case "-" : result -= numList.get(i+1); break;
				case "*" : result *= numList.get(i+1); break;
				case "/" : result /= numList.get(i+1); break;
			}
			
			}
		}
		
		return result;
	}
	
	
	public static void main(String[] args) {
		new MyLayout02();

	}

}

 

import javax.swing.JFrame;

public class MyFrame02 extends JFrame{

	public MyFrame02() {
		this("계산기 by zephyr", 490, 680);
	}
	
	public MyFrame02(String title, int width, int height) {
		setTitle(title);
		setSize(width, height);
		
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	// X버튼 누르면 종료
		
	}

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

	}

}