본문 바로가기
Project/Study | etc

Team Project - 계산기 프로그램

by 코젼 2022. 7. 1.
728x90
반응형

처음으로 팀끼리 프로젝트를 진행해 보았습니다!!!😀

미완성인점 양해 부탁드립니다😓

 

👬팀원

💬 ActionListener 기능 구현

◾ 황호연(팀장)

◾ 한은진

◾ 이수빈


💬 UI 구현

◾ 이재범

◾ 이동근

◾ 김지연(본인)


🕝 개발 기간

◾ 2022.07.01 ~ 2022.07.01 (1일 단기 프로젝트)


💾 소스코드

2022-07-01 업로드

Calculator.zip
0.01MB


📂 calculate 패키지

📝 Calculate 클래스

package calculate;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Calculator extends JFrame {
	final int MAX_BTN = 20;
	final int SQUARE = 4;
	final int x = 35;
	final int y = 100;
	final int width = 80;
	final int height = 80;
	final int location = 80;
	int number1 = 0;
	int number2 = 0;
	int to1 = 0;
	int to2 = 0;
	String number01 = "0";
	String number02 = "0";
	String rel = "";
	String text_num = "";

	JButton[][] btn = new JButton[SQUARE + 1][SQUARE]; // 버튼
	String[] number = { "←", "CE", "C", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "+/-", "0",
			".", "=" }; // 버튼 안의 문자
	JTextField text = new JTextField();

	public Calculator() {
		// 창의 제목, 사이즈, 보이기 여부 등을 지정해줌
		setTitle("계산기");
		// 화면의 가운데에 띄움
		setLocationRelativeTo(null);
		// 사이즈조절 불가능
		setResizable(false);
		// 창을 닫을 때 실행 중인 프로그램도 같이 종료되도록 함
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Container pane = getContentPane();
		// 배치관리자 제거
		pane.setLayout(null);
		
		setSize(400, 550);
		pane.setBackground(Color.DARK_GRAY);

		text.setEditable(false);
		text.setBackground(new Color(234,234,234));
		text.setBounds(x, y - 90, width * 4, height);
		text.setFont(new Font("휴먼둥근헤드라인", Font.BOLD, 25));
		pane.add(text);

		// k라는 변수를 하나 만들어서, btn배열의 0~19 인덱스에 모두 접근할 수 있도록 한다.
		int k = 0;
		for (int i = 0; i < SQUARE + 1; i++) {
			for (int j = 0; j < SQUARE; j++) {

				// 인덱스에 k++를 해서 btn배열의 0~19에 모두 접근하여 값을 넣을 수 있도록 한다.
//            btn[i][j] = new JButton(number[k++] + "");
				btn[i][j] = new JButton(number[k++]);
				btn[i][j].setBounds(x + (location * j), y + (location * i), width, height);
				btn[i][j].setFont(new Font("휴먼둥근헤드라인", Font.BOLD, 25));
				btn[i][j].setBackground(Color.WHITE);

				if (j == 3) {
					btn[i][3].setForeground(Color.BLUE);
				}

				btn[i][j].addActionListener(new MyActionListener());
				pane.add(btn[i][j]);
			}

			if (i < 3) {
				btn[0][i].setForeground(Color.RED);
			}

		}
		ImageIcon image = new ImageIcon("image/0.png");
		JLabel jLabel = new JLabel(image);
		btn[4][0].setForeground(Color.BLUE);
		btn[4][0].setPreferredSize(new Dimension(30, 30)); // 버튼 크기 지정
		btn[4][0].add(jLabel);
		
		setVisible(true);
	}

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

	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			if (to2 == 0 && rel.equals("")) {
				if (e.getActionCommand() == "1") {
					number01 += "1";
					System.out.print("1");
					text_num += "1";
					text.setText(text_num);
				} else if (e.getActionCommand() == "2") {
					number01 += "2";
					System.out.print("2");
					text_num += "2";
					text.setText(text_num);
				} else if (e.getActionCommand() == "3") {
					number01 += "3";
					System.out.print("3");
					text_num += "3";
					text.setText(text_num);
				} else if (e.getActionCommand() == "4") {
					number01 += "4";
					System.out.print("4");
					text_num += "4";
					text.setText(text_num);
				} else if (e.getActionCommand() == "5") {
					number01 += "5";
					System.out.print("5");
					text_num += "5";
					text.setText(text_num);
				} else if (e.getActionCommand() == "6") {
					number01 += "6";
					System.out.print("6");
					text_num += "6";
					text.setText(text_num);
				} else if (e.getActionCommand() == "7") {
					number01 += "7";
					System.out.print("7");
					text_num += "7";
					text.setText(text_num);
				} else if (e.getActionCommand() == "8") {
					number01 += "8";
					System.out.print("8");
					text_num += "8";
					text.setText(text_num);
				} else if (e.getActionCommand() == "9") {
					number01 += "9";
					System.out.print("9");
					text_num += "9";
					text.setText(text_num);
				} else if (e.getActionCommand() == "0") {
					number01 += "0";
					System.out.print("0");
					text_num += "0";
					text.setText(text_num);
				}
			} else {
				if (e.getActionCommand() == "1") {
					number02 += "1";
					System.out.print("1");
					text_num += "1";
					text.setText(text_num);
				} else if (e.getActionCommand() == "2") {
					number02 += "2";
					System.out.print("2");
					text_num += "2";
					text.setText(text_num);
				} else if (e.getActionCommand() == "3") {
					number02 += "3";
					System.out.print("3");
					text_num += "3";
					text.setText(text_num);
				} else if (e.getActionCommand() == "4") {
					number02 += "4";
					System.out.print("4");
					text_num += "4";
					text.setText(text_num);
				} else if (e.getActionCommand() == "5") {
					number02 += "5";
					System.out.print("5");
					text_num += "5";
					text.setText(text_num);
				} else if (e.getActionCommand() == "6") {
					number02 += "6";
					System.out.print("6");
					text_num += "6";
					text.setText(text_num);
				} else if (e.getActionCommand() == "7") {
					number02 += "7";
					System.out.print("7");
					text_num += "7";
					text.setText(text_num);
				} else if (e.getActionCommand() == "8") {
					number02 += "8";
					System.out.print("8");
					text_num += "8";
					text.setText(text_num);
				} else if (e.getActionCommand() == "9") {
					number02 += "9";
					System.out.print("9");
					text_num += "9";
					text.setText(text_num);
				} else if (e.getActionCommand() == "0") {
					number01 += "0";
					System.out.print("0");
					text_num += "0";
					text.setText(text_num);
				}
			}

			if (e.getActionCommand() == "+") {
				to1 = Integer.parseInt(number01);
				rel = "+";
				text_num += " + ";
				text.setText(text_num);
				System.out.print(" + ");
			}

			if (e.getActionCommand() == "C") {
				number1 = 0;
				number2 = 0;
				to1 = 0;
				to2 = 0;
				number01 = "0";
				number02 = "0";
				rel = "";
				text_num = "";
				text.setText(text_num);
			}

			// 더하기 연산 종료
			if (rel.equals("+")) {
				if (e.getActionCommand() == "=") {
					to2 = Integer.parseInt(number02);
					text_num += " = " + ((int) to1 + (int) to2);
					text.setText(text_num);
					System.out.println("\n더하기 계산결과 : " + ((int) to1 + (int) to2));
				}
			}
			if (e.getActionCommand() == "-") {
				to1 = Integer.parseInt(number01);
				rel = "-";
				text_num += " - ";
				text.setText(text_num);
				System.out.print(" - ");
			}

			// 빼기 연산 종료
			if (rel.equals("-")) {
				if (e.getActionCommand() == "=") {
					to2 = Integer.parseInt(number02);
					text_num += " = " + ((int) to1 - (int) to2);
					text.setText(text_num);
					System.out.println("\n빼기 계산결과 : " + (to1 - to2));
				}
			}
			if (e.getActionCommand() == "*") {
				to1 = Integer.parseInt(number01);
				rel = "*";
				text_num += " x ";
				text.setText(text_num);
				System.out.print(" x ");
			}

			// 곱하기 연산 종료
			if (rel.equals("*")) {
				if (e.getActionCommand() == "=") {
					to2 = Integer.parseInt(number02);
					text_num += " = " + ((int) to1 * (int) to2);
					text.setText(text_num);
					System.out.println("\n곱하기 계산결과 : " + (to1 * to2));
				}
			}
			if (e.getActionCommand() == "/") {
				to1 = Integer.parseInt(number01);
				rel = "/";
				text_num += " / ";
				text.setText(text_num);
				System.out.print(" / ");
			}

			// 나누기 연산 종료
			if (rel.equals("/")) {
				if (e.getActionCommand() == "=") {
					to2 = Integer.parseInt(number02);
					text_num += " = " + ((float) to1 / (float) to2);
					text.setText(text_num);
					System.out.println("\n나누기 계산결과 : " + ((float) to1 / (float) to2));
				}
			}
		}
	}
}

💻 실행 화면

728x90
반응형

'Project > Study | etc' 카테고리의 다른 글

랜덤 숫자 게임  (0) 2022.08.11
구구단 게임  (0) 2022.08.11
상품 구매 프로그램  (0) 2022.06.29
성적 관리 프로그램 + 설계, 메소드 구현  (0) 2022.06.28
JAVA의 정석 3판 - 두 점 사이 거리 계산  (0) 2022.06.26

댓글