JAVA

[JAVA Project] Calculator (WindowBuilder, Swing, JFrame 등)

MoveForward 2022. 8. 29. 00:27

1. Window builder를 사용하여 계산기 프레임 디자인

 

 

2. 각 버튼에 이벤트 생성

2-1. 숫자버튼에 이벤트 생성

JButton btn7 = new JButton("7");
		// 버튼 이벤트 설정하기
		btn7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
                // 텍스트 필드에 있는 기존 숫자 + 숫자 7 추가
				String EnterNumber = textField.getText() + btn7.getText();
				textField.setText(EnterNumber); // 텍스트 필드에 출력
			}
		});
		btn7.setFont(new Font("Tahoma", Font.BOLD, 20));
		btn7.setBounds(12, 138, 60, 60); // setBounds(버튼 위치 x좌표, y좌표, 가로크기, 세로크기)
		frame.getContentPane().add(btn7);

 

2-2. 연산자버튼 이벤트 생성

JButton btnPlus = new JButton("+");
		btnPlus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				firstnum = Double.parseDouble(textField.getText());
				textField.setText("");
				operations = "+";
			}
		});
		btnPlus.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnPlus.setBounds(210, 72, 60, 60);
		frame.getContentPane().add(btnPlus);

 

2-3. Equal( = ) 버튼 이벤트 생성

// 계산 결과 출력 버튼
		JButton btnEqual = new JButton("=");
		btnEqual.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String answer; // 정답 문자열 선언
				secondnum = Double.parseDouble(textField.getText()); // 두번째 입력 숫자
				
                // 각 연산자 별로 첫번째 입력 숫자와 두번째 입력숫자를 연산한 결과값(result)
                // 를 소수점 두번째 자리까지 텍스트 필드에 출력
                if (operations == "+") {
					result = firstnum + secondnum;
					answer = String.format("%.2f", result);
					textField.setText(answer);
				}
				else if (operations == "-") {
					result = firstnum - secondnum;
					answer = String.format("%.2f", result);
					textField.setText(answer);
				}
				else if (operations == "*") {
					result = firstnum * secondnum;
					answer = String.format("%.2f", result);
					textField.setText(answer);
				}
				else if (operations == "/") {
					result = firstnum / secondnum;
					answer = String.format("%.2f", result);
					textField.setText(answer);
				}
				else if (operations == "%") {
					result = firstnum % secondnum;
					answer = String.format("%.2f", result);
					textField.setText(answer);
				}
				
				
			}
		});
		btnEqual.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnEqual.setBounds(210, 336, 60, 60);
		frame.getContentPane().add(btnEqual);

 

2-4. 기타 다른 버튼 이벤트 생성

 

1. Backspace 버튼

		JButton btnBS = new JButton("←");
		btnBS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String backspace = null;
				
				if(textField.getText().length() > 0) {
					StringBuilder strB = new StringBuilder(textField.getText());
					strB.deleteCharAt(textField.getText().length() - 1);
					backspace = strB.toString();
					textField.setText(backspace);
				}
			}
		});
		btnBS.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnBS.setBounds(12, 72, 60, 60);
		frame.getContentPane().add(btnBS);

 

2. C (clear : 초기화) 버튼

		JButton btnC = new JButton("C");
		btnC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 텍스트 필드 초기화
				textField.setText(null);
			}
		});
		btnC.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnC.setBounds(78, 72, 60, 60);
		frame.getContentPane().add(btnC);

 

 

3. . (DOT : 소수점) 버튼

		JButton btnDot = new JButton(".");
		btnDot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(! textField.getText().contains(".")) {
					textField.setText(textField.getText() + btnDot.getText());
				}
			}
		});
		btnDot.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnDot.setBounds(78, 336, 60, 60);
		frame.getContentPane().add(btnDot);

 

 

4. +/- (Plus / Minus) 버튼

		// Plus/Minus : (-1)곱하기
		JButton btnPM = new JButton("±");
		btnPM.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				double ops = Double.parseDouble(String.valueOf(textField.getText()));
				ops = ops * (-1);
				textField.setText(String.valueOf(ops));
			}
		});
		btnPM.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnPM.setBounds(144, 336, 60, 60);
		frame.getContentPane().add(btnPM);

 

 

전체 소스

// 전체 소스