JAVA - 자바 GUI 로 계산기 만들기

2024. 1. 4. 10:51Full Stack Course 풀스택과정/JAVA 자바

728x90

배울내용:

자바 계산기 만들기 

Swing으로 계산기 만들기 

Eclipse Java Swing

Eclipse 자바 스윙

자바 TextField 이벤트 

자바 버튼 이벤트

자바 이벤트

자바 초기화 버튼 만들기

자바 GUI 만들기 

make calculator with GUI Java

 

 

 

 

 

 

생각외로 간단해보이는 계산기

그냥 조금 시간들여서 뚝딱 만들어야지 했지만 몇시간만에 겨우 기능을 작동하게 만들었다

 

필자는 이미 여러번 만들어봤는데도 조금 힘들었다

 

 

 

 

완성본 모습 

 

 

 

 

 

한번 만들어보자 

 

 

 

 

 

 

 

 

 

 

 

 

먼저  window builder로 틀을 만들어준다 

 

 

 

 

 

만드는 방법은 

 

 

 

1 .  컨테이너에 JPanel을 만든뒤에

2.  레이아웃을 Absolute로 주면 자유자제로 크기 조정이 가능하다

3. 그리고 그안에 넣고싶은 걸 3번중에 골라 넣으면 된다

 

 

그리고 헷갈리지 않게 변수명도 하나하나 바꿔준다

 

 

 

 

 

 

 

일일이 source에서 바꾸지말고 Design(windows builder)에서 아까 만들었던거 하나하나  클릭후 

바꿔준다 


CALC__B_O_multi 를 이렇게 지은이유는

CALC = Caculator 계산기

B = Button 버튼

O = Operator 연산자

multi = multiply 곱하다 

를 줄여서 저렇게 만든다 

 

 

본인 입맛대로 지으면 된다 

 

 

 

 

참고로 왼쪽하단에 속성 (Properties) 가 있다 여기에 변수인 variable을 바꿔주면 된다 

그러면 알아서 source에서는 변경되니 편하다 

 

 

 

 

 

 

 

 

이렇게 설정해서 클릭시 열리고 닫히고를 만들어준다

 

 

 

 

 

 

이렇게 panel 자체를 가려주면 계산기 필요할때 계산기 버튼클릭하고

글자변경필요할때 글자변경을 클릭해줘서 창을뛰워주면된다

 

 

 

 

 

 

 

 

이제

 

여기에 버튼을 입력시 넣어줄건데 

저게 CALC_Main  라고하면은 

버튼입력시 addActionListener로 

CALC_Main  .setText() 해주는게아니라

어느한 String 형 변수에 += 시켜주면 된다 

 

 

 

 

 

 

그리고나서 그 변경된걸 CALC_Main 에 넣어주면된다

가운데 보이는거는 CALC_B_N_1 은 1번 을 누르면 2초정도 빨간색으로 있다가 돌아오는 코드다 

원하는 기능있으면 만들어주면 된다 

 

 

 

 

내가 추가기능넣은것 중하나는 

 

 

이거는 만약에 누가 100000000 을 입력하고 싶은데 일일이 다 버튼 지우는게 귀찮을수있으니

Delete 을 꾹누르고 있으면 0.1 초에 1개씩 찍어주도록 만들어준다 

 

 

 

 

 

 

 

 

 

 

이번에는 아까 Main에 넣은 입력값을 나눠줘야한다

예를들어 입력값에 

1+1 이되어있으면 이걸 어떻게 1+1인걸 인식시켜주는가?

 

이런것이다 

 

 

코드를 해석하면 CALC_B_Change_Count++ 는 클릭이 1번 될때마다 1씩 더해줘서 로그1~7 까지 기록 남긴다 

그러나 이것보다는 아까 말했던 식을 구분해야하는데

먼저 제곱 인 ^2 를 따로 없애고 

input.replaceAll("+\\-%^x/]","");

이렇게 되어있다 이거는 RE 라고 Regular Expression으로 정규표현식으로 표현한것이다 

이거는 따로 공부를 해서 이해 해야하는데 그렇게 어렵진않다 

일단 내가 쓴걸 해석하자면

 

[] 이거 안에 있는걸 조건으로 걸고

맨앞에 ^ 이거는 ^~~~~~     ^ 뒤에 나올것을 제외한 나머지를 말하는것이다 

+가 있고 , \\ 이 있는데 이건 \ 이다 , - 가있고...  / 까지있다

그러면 여기에 있는 연산자 빼고는 다지워주고 이걸 operator에 넣고

(그뒤에 만약 연산자가 ^2면 operator를 ^2로 바꿔줌)

 

그리고 switch 문으로 따로 넣어줘서 저렇게 계산하면된다 

 

필자는 RE 를 좋아한다 

저걸안쓰면 for문이나 while문을 여러번써서 charAt 으로 하나하나 뽑아주고

너무 복잡해지기 때문에 저렇게 쓴다 

 

 

 

 

 

 

이걸 실행해보면 

 

 

 

이렇게 계산되고 계산된 식은 그대로 남긴다 

그리고 바로 다음 계산을 이어서 할수있게 만들어준다 

 

 

그러면 

 

 

 

 

정상 작동하는걸 볼수있다 

 

 

아래는 작동하는 영상이다 

 

 

 

 

 

 

아래는 코드다 

 

 

package CARLOS1;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class _2_Calculator extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	public static String FilePath = "..\\ProjectTest_CARLOS\\images\\";
	// CALC_전용 =========================
	public static String CalcMain = "";
	public static int CALC_Cng = 0;
	private JTextField CALC_Main;
	private JLabel CALC_L_L_info;
	private JButton CALC_B_Change;
	private JButton CALC_B_N_1;
	private JLabel CALC_L_Red_info;
	private JPanel panel_2;
	private JButton CALC_B_N_2;
	private JButton CALC_B_N_3;
	private JButton CALC_B_N_4;
	private JButton CALC_B_N_5;
	private JButton CALC_B_N_6;
	private JButton CALC_B_N_7;
	private JButton CALC_B_N_8;
	private JButton CALC_B_N_9;
	private JButton CALC_B_N_10;
	private JButton CALC_B_O_minus;
	private JButton CALC_B_O_Plus;
	private JButton CALC_B_O_sqrt;
	private JButton CALC_B_O_Remain;
	private JButton CALC_B_O_Devide;
	private JButton CALC_B_O_multi;
	private JButton CALC_B_O_clear;
	private JButton CALC_B_O_dot;
	private JButton CALC_B_Delete;
	private JLabel CALC_L_L_1;
	private JLabel CALC_L_L_2;
	private JLabel CALC_L_L_3;
	private JLabel CALC_L_L_4;
	private JLabel CALC_L_L_5;
	private JLabel CALC_L_L_6;
	private JLabel CALC_L_L_7;
	private JLabel CALC_L_L_8;
	private Timer buttonTimer;
	private int CALC_B_Change_Count = 0;
	static int CALC_ONOFF = 0;
	// ================================


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					_2_Calculator frame = new _2_Calculator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public _2_Calculator() {

		// 타이머 초기화
		buttonTimer = new Timer(300, e -> {
			CALC_B_N_1.setBackground(null);
			CALC_B_N_2.setBackground(null);
			CALC_B_N_3.setBackground(null);
			CALC_B_N_4.setBackground(null);
			CALC_B_N_5.setBackground(null);
			CALC_B_N_6.setBackground(null);
			CALC_B_N_7.setBackground(null);
			CALC_B_N_8.setBackground(null);
			CALC_B_N_9.setBackground(null);
			CALC_B_N_10.setBackground(null);
			CALC_B_O_minus.setBackground(null);
			CALC_B_O_Plus.setBackground(null);
			CALC_B_O_sqrt.setBackground(null);
			CALC_B_O_Remain.setBackground(null);
			CALC_B_O_Devide.setBackground(null);
			CALC_B_O_multi.setBackground(null);
			CALC_B_O_clear.setBackground(null);
			CALC_B_O_dot.setBackground(null);
			CALC_B_Delete.setBackground(null);
			CALC_B_Change.setBackground(null);
		});
		buttonTimer.setRepeats(false);

		/////////
		// MAIN
		/////////
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 1400, 1000);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel Mainpanel = new JPanel();
		Mainpanel.setBounds(1026, 69, 214, 70);
		contentPane.add(Mainpanel);
		Mainpanel.setLayout(null);

		JButton m_WordCngBtn = new JButton("글자 변경");
		m_WordCngBtn.setBounds(0, 0, 214, 70);
		Mainpanel.add(m_WordCngBtn);
		m_WordCngBtn.setFont(new Font("맑은 고딕", Font.BOLD, 20));

		panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(255, 0, 0), 3, true));
		panel_2.setBounds(140, 500, 1100, 400);
		contentPane.add(panel_2);

		CALC_B_Change = new JButton("변경\n실행");
		CALC_B_Change.setBounds(938, 32, 150, 343);
		CALC_B_Change.setFont(new Font("한컴 고딕", Font.PLAIN, 30));

		CALC_Main = new JTextField("");
		CALC_Main.setBounds(374, 18, 348, 57);
		CALC_Main.setColumns(10);

		CALC_L_Red_info = new JLabel("피연산자는 최대 2개 입니다. ");
		CALC_L_Red_info.setForeground(new Color(255, 0, 0));
		CALC_L_Red_info.setFont(new Font("HY헤드라인M", Font.PLAIN, 15));
		CALC_L_Red_info.setBounds(382, 84, 350, 30);

		JLabel lblNewLabel_3 = new JLabel("");
		lblNewLabel_3.setBounds(140, 75, 615, 415);
		lblNewLabel_3.setIcon(new ImageIcon(FilePath + "vogue1.jpg"));
		contentPane.add(lblNewLabel_3);

		CALC_B_N_1 = new JButton("1");
		CALC_B_N_1.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_1.setBounds(374, 114, 78, 75);

		CALC_B_N_2 = new JButton("2");
		CALC_B_N_2.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_2.setBounds(464, 114, 78, 75);

		CALC_B_N_3 = new JButton("3");
		CALC_B_N_3.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_3.setBounds(554, 114, 78, 75);

		CALC_B_N_4 = new JButton("4");
		CALC_B_N_4.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_4.setBounds(374, 217, 78, 75);

		CALC_B_N_5 = new JButton("5");
		CALC_B_N_5.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_5.setBounds(464, 217, 78, 75);

		CALC_B_N_6 = new JButton("6");
		CALC_B_N_6.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_6.setBounds(554, 217, 78, 75);

		CALC_B_N_7 = new JButton("7");
		CALC_B_N_7.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_7.setBounds(374, 315, 78, 75);

		CALC_B_N_8 = new JButton("8");
		CALC_B_N_8.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_8.setBounds(464, 315, 78, 75);

		CALC_B_N_9 = new JButton("9");
		CALC_B_N_9.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_9.setBounds(554, 315, 78, 75);

		CALC_B_N_10 = new JButton("0");
		CALC_B_N_10.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_N_10.setBounds(644, 220, 78, 170);

		CALC_B_Delete = new JButton("←");
		CALC_B_Delete.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_Delete.setBounds(644, 114, 78, 75);

		CALC_B_O_minus = new JButton("-");
		CALC_B_O_minus.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_minus.setBounds(834, 35, 78, 75);

		CALC_B_O_Plus = new JButton("+");
		CALC_B_O_Plus.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_Plus.setBounds(744, 35, 78, 75);

		CALC_B_O_sqrt = new JButton("^2");
		CALC_B_O_sqrt.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_sqrt.setBounds(744, 225, 78, 75);

		CALC_B_O_Remain = new JButton("%");
		CALC_B_O_Remain.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_Remain.setBounds(834, 225, 78, 75);

		CALC_B_O_Devide = new JButton("÷");
		CALC_B_O_Devide.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_Devide.setBounds(744, 130, 78, 75);

		CALC_B_O_multi = new JButton("x");
		CALC_B_O_multi.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_multi.setBounds(834, 130, 78, 75);

		CALC_B_O_dot = new JButton(".");
		CALC_B_O_dot.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_dot.setBounds(744, 315, 78, 75);

		CALC_B_O_clear = new JButton("C");
		CALC_B_O_clear.setFont(new Font("한컴 고딕", Font.PLAIN, 30));
		CALC_B_O_clear.setBounds(834, 315, 78, 75);

		CALC_L_L_info = new JLabel("계산기 log 기록");
		CALC_L_L_info.setBounds(110, 18, 143, 32);
		CALC_L_L_info.setFont(new Font("한컴 고딕", Font.BOLD, 15));

		CALC_L_L_1 = new JLabel("log1: ");
		CALC_L_L_1.setBounds(27, 57, 320, 32);

		CALC_L_L_2 = new JLabel("log2: ");
		CALC_L_L_2.setBounds(27, 99, 320, 32);

		CALC_L_L_3 = new JLabel("log3: ");
		CALC_L_L_3.setBounds(27, 141, 320, 32);

		CALC_L_L_4 = new JLabel("log4: ");
		CALC_L_L_4.setBounds(27, 183, 320, 32);

		CALC_L_L_5 = new JLabel("log5: ");
		CALC_L_L_5.setBounds(27, 225, 320, 32);

		CALC_L_L_6 = new JLabel("log6: ");
		CALC_L_L_6.setBounds(27, 267, 320, 32);

		CALC_L_L_7 = new JLabel("log7: ");
		CALC_L_L_7.setBounds(27, 309, 320, 32);

		CALC_L_L_8 = new JLabel("log8: ");
		CALC_L_L_8.setBounds(27, 352, 320, 32);

		panel_2.add(CALC_B_N_6);
		panel_2.add(CALC_Main);
		panel_2.add(CALC_L_L_info);
		panel_2.add(CALC_L_Red_info);
		panel_2.add(CALC_B_Change);
		panel_2.add(CALC_B_N_1);
		panel_2.add(CALC_B_N_2);
		panel_2.add(CALC_B_N_3);
		panel_2.add(CALC_B_N_4);
		panel_2.add(CALC_B_N_5);
		panel_2.add(CALC_B_N_7);
		panel_2.add(CALC_B_N_8);
		panel_2.add(CALC_B_N_9);
		panel_2.add(CALC_B_N_10);
		panel_2.add(CALC_B_O_minus);
		panel_2.add(CALC_B_O_Plus);
		panel_2.add(CALC_B_O_sqrt);
		panel_2.add(CALC_B_O_Remain);
		panel_2.add(CALC_B_O_Devide);
		panel_2.add(CALC_B_O_multi);
		panel_2.add(CALC_B_O_dot);
		panel_2.add(CALC_B_O_clear);
		panel_2.add(CALC_B_Delete);
		panel_2.add(CALC_L_L_1);
		panel_2.add(CALC_L_L_2);
		panel_2.add(CALC_L_L_3);
		panel_2.add(CALC_L_L_4);
		panel_2.add(CALC_L_L_5);
		panel_2.add(CALC_L_L_6);
		panel_2.add(CALC_L_L_7);
		panel_2.add(CALC_L_L_8);
		panel_2.setLayout(null);

		///////////
		// 메인버튼2
		///////////
		JButton m_WordCngBtn_1 = new JButton("계산기");
		m_WordCngBtn_1.setFont(new Font("맑은 고딕", Font.BOLD, 20));
		m_WordCngBtn_1.setBounds(788, 69, 214, 70);
		contentPane.add(m_WordCngBtn_1);
		panel_2.setVisible(false);
		setLocationRelativeTo(null);

		m_WordCngBtn_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CALC_ONOFF++;
				if (CALC_ONOFF > 1) {
					CALC_ONOFF = 0;
					panel_2.setVisible(true);
				}
				if (CALC_ONOFF == 1) {
					panel_2.setVisible(false);
				}
			}
		}); 
		//////////////
		// <CALC Event>
		//////////////

		CALC_B_N_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "1";
				CALC_Main.setText(CalcMain);
				CALC_B_N_1.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "2";
				CALC_Main.setText(CalcMain);
				CALC_B_N_2.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "3";
				CALC_Main.setText(CalcMain);
				CALC_B_N_3.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "4";
				CALC_Main.setText(CalcMain);
				CALC_B_N_4.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "5";
				CALC_Main.setText(CalcMain);
				CALC_B_N_5.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "6";
				CALC_Main.setText(CalcMain);
				CALC_B_N_6.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "7";
				CALC_Main.setText(CalcMain);
				CALC_B_N_7.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "8";
				CALC_Main.setText(CalcMain);
				CALC_B_N_8.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "9";
				CALC_Main.setText(CalcMain);
				CALC_B_N_9.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_N_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "0";
				CALC_Main.setText(CalcMain);
				CALC_B_N_10.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_minus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "-";
				CALC_Main.setText(CalcMain);
				CALC_B_O_minus.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_Plus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "+";
				CALC_Main.setText(CalcMain);
				CALC_B_O_Plus.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_sqrt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "^2";
				CALC_Main.setText(CalcMain);
				CALC_B_O_sqrt.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_Remain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "%";
				CALC_Main.setText(CalcMain);
				CALC_B_O_Remain.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_Devide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "/";
				CALC_Main.setText(CalcMain);
				CALC_B_O_Devide.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_multi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += "x";
				CALC_Main.setText(CalcMain);
				CALC_B_O_multi.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain = "";
				CALC_Main.setText(CalcMain);
				CALC_B_O_clear.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_O_dot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CalcMain += ".";
				CALC_Main.setText(CalcMain);
				CALC_B_O_dot.setBackground(Color.red);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

		CALC_B_Delete.addMouseListener(new MouseAdapter() {
			Timer timer = new Timer(100, new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String CalcMain_tmp = "";
					if (CalcMain.length() == 0) {
						CalcMain = "";
						CALC_Main.setText(CalcMain);
					} else {
						for (int i = 0; i < CalcMain.length() - 1; i++) {
							char c = CalcMain.charAt(i);
							CalcMain_tmp += c;
						}

						CalcMain = "";
						CalcMain += CalcMain_tmp;
						CALC_Main.setText(CalcMain);
					}

					CALC_B_Delete.setBackground(Color.red);

					if (buttonTimer.isRunning()) {
						buttonTimer.restart();
					} else {
						buttonTimer.start();
					}
				}
			});

			@Override
			public void mousePressed(MouseEvent e) {
				timer.start();
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				timer.stop();
			}
		});

		CALC_B_Delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String CalcMain_tmp = "";
				if (CalcMain.length() == 0) {
					CalcMain = "";
					CALC_Main.setText(CalcMain);
				} else {
					for (int i = 0; i < CalcMain.length() - 1; i++) {
						char c = CalcMain.charAt(i);
						CalcMain_tmp += c;
					}

					CalcMain = "";
					CalcMain += CalcMain_tmp;
					CALC_Main.setText(CalcMain);
				}

				CALC_B_Delete.setBackground(Color.red);

				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});
		CALC_B_Change.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CALC_B_Change_Count++;
				String input = CALC_Main.getText();
				double A ,B;
				String[] parts = input.split("(\\^2|[+\\-%÷√x/])");
				if (parts.length >= 2) {
					  A = Double.parseDouble(parts[0].trim());
					  B = Double.parseDouble(parts[1].trim());
				} else  {
					  A = Double.parseDouble(parts[0].trim());
					  B = 0;
				}
				String tmp1 = "";
				if (input.contains("^2")) {
					tmp1 = "^2";
					input = input.replaceAll("^2", ""); 
				}
				
				String operator = input.replaceAll("[^+\\-%^÷x/]", "");
				double result = 0;
				if(tmp1 == "^2") {
					operator = tmp1;
				}
				switch (operator) {
				case "+": result = A + B; break;
				case "-": result = A - B; break;
				case "%": result = A % B; break;
				case "^2": result = Math.pow(A, 2); break;
				case "/": result = A / B; break;
				case "√": result = Math.sqrt(A); break;
				case "x": result = A * B; break;
				}
				
				String CALC_logs = String.valueOf("log"+CALC_B_Change_Count+": "+result);
				
				switch(CALC_B_Change_Count) {
				case 1: CALC_L_L_1.setText(CALC_logs); break;
				case 2: CALC_L_L_2.setText(CALC_logs); break;
				case 3: CALC_L_L_3.setText(CALC_logs); break;
				case 4: CALC_L_L_4.setText(CALC_logs); break;
				case 5: CALC_L_L_5.setText(CALC_logs); break;
				case 6: CALC_L_L_6.setText(CALC_logs); break;
				case 7: CALC_L_L_7.setText(CALC_logs); break;
				case 8: CALC_L_L_8.setText(CALC_logs); break;
				default : CALC_B_Change_Count= 0; break; 
				}
				 
				CALC_Main.setText(String.valueOf(result));
				CALC_B_Change.setBackground(Color.red); 
				CalcMain =String.valueOf(result);
				if (buttonTimer.isRunning()) {
					buttonTimer.restart();
				} else {
					buttonTimer.start();
				}
			}
		});

	}
}

 

위에는 미완성본이다 그러나 90%는 완성해놨으니 나머지 10%를 한번 스스로 고쳐보자 

 

 

 

 

그럼 다음번에는 좀더 복잡한걸 만들어보겠다 

 

 

 

 

그럼 20000~ 

 

728x90