numL openL
exit
numL 에 숫자가 오고 openL에 사칙연산이 나온다.
여러자리 연산과 연속 연산 이 가능하나
소숫점에 대한 한계를 남겼다.
1. Calculator
package com.kitri.awt.event;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends Frame {
Panel pN = new Panel();
Panel pC= new Panel();
Label numL = new Label();
Label openL = new Label();
String bt[] = {"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};
Button btn[] = new Button[16];
int len = btn.length;
Button exit = new Button("종료");
CalculatorController calculatorController;
public Calculator() {
super("계산기");
pN.setLayout(new BorderLayout(5, 0));
pN.add(numL,"Center");
numL.setBackground(Color.gray);
numL.setForeground(Color.white);
pN.add(openL,"East");
openL.setBackground(Color.DARK_GRAY);
openL.setForeground(Color.white);
Font f = new Font("굴림",Font.BOLD, 20);
numL.setFont(f);
openL.setFont(f);
pC.setLayout(new GridLayout(4, 4, 5, 5));
for(int i=0; i<len;i++) {
btn[i]= new Button(bt[i]);
btn[i].setBackground(new Color(250-i*10,250-i,250-i));
pC.add(btn[i]);
}
setLayout(new BorderLayout(0, 10));
add(pN,"North");
add(pC,"Center");
add(exit,"South");
setBounds(300, 200, 300, 500);
setVisible(true);
calculatorController = new CalculatorController(this);
for(int i=0;i<len;i++) {
btn[i].addActionListener(calculatorController);
}
exit.addActionListener(calculatorController);
}
public static void main(String[] args) {
new Calculator();
}
}
2. CalculatorController
package com.kitri.awt.event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorController implements ActionListener {
Calculator calculator;
CalculatorService calculatorService;
public CalculatorController(Calculator calculator) {
this.calculator = calculator;
calculatorService = new CalculatorService(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object ob = e.getSource();
// System.out.println(ob);
if (ob == calculator.exit) {
calculatorService.exit();
} else {
for (int i = 0; i < calculator.len; i++) {
if (ob == calculator.btn[i]) { // 7
calculatorService.classify(i);
break;
}
}
}
}
}
3. CalculatorService
package com.kitri.awt.event;
public class CalculatorService {
CalculatorController calculatorController;
private Calculator cc;
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
StringBuffer sb3 = new StringBuffer();
double result=0;
public CalculatorService(CalculatorController calculatorController) {
this.calculatorController = calculatorController;
cc = calculatorController.calculator;
}
public void exit() {
System.exit(0);
}
public void classify(int i) {
// System.out.println(cc.btn[i].getLabel()); //누른 버튼
if (cc.btn[i].getLabel() == "C") { // clear
clear();
} else if (cc.btn[i].getLabel() == "=") { // =
result();
} else if (isNumber(cc.btn[i].getLabel())) { // 숫자이면
if (sb1.length() == 0) {
sb1.append(cc.btn[i].getLabel());
// System.out.println(sb1.length());
// System.out.println(sb1);
cc.numL.setText(sb1.toString());
} else {
if (sb2.length() == 0) {
sb1.append(cc.btn[i].getLabel());
// System.out.println(sb1);
cc.numL.setText(sb1.toString());
} else {
sb3.append(cc.btn[i].getLabel());
// System.out.println(sb3);
cc.numL.setText(sb3.toString());
}
}
} else { // 사칙연산버튼
cc.openL.setText(cc.btn[i].getLabel());
if (sb1.length() == 0) {
sb1.append(0);
sb2.replace(0, sb2.length(), cc.btn[i].getLabel());
// System.out.println(sb2);
} else {
sb2.replace(0, sb2.length(), cc.btn[i].getLabel());
// System.out.println(sb2);
}
}
}
private void result() {
// System.out.println(sb1.toString()+sb2.toString()+sb3.toString());
sb1.reverse();
sb3.reverse();
int num1 = 0;
for(int i=0; i<sb1.length(); i++) {
num1 += (sb1.toString().charAt(i)-48)*(Math.pow(10, i));
}
int num3 = 0;
for(int i=0; i<sb3.length(); i++) {
num3 += (sb3.toString().charAt(i)-48)*(Math.pow(10, i));
}
// System.out.println("num1 === "+num1);
// System.out.println("num3 === "+num3);
if(sb2.toString().equals("+")) {
result = num1+num3;
} else if(sb2.toString().equals("-")) {
result = num1-num3;
} else if(sb2.toString().equals("*")) {
result = num1*num3;
} else if(sb2.toString().equals("/")){
result = (double)num1/(double)num3;
} else {
result = num1;
}
// System.out.println(result);
cc.numL.setText(""+result);
// sb1을 result 로 sb2,sb3 초기화
// 소숫점을 어떻게 처리해야할까 >> 일단 강제 int
sb1.replace(0, sb1.length(), ""+(int)result);
sb2.delete(0, sb2.length());
sb3.delete(0, sb3.length());
}
private void clear() {
cc.numL.setText("");
cc.openL.setText("");
sb1.delete(0, sb1.capacity());
sb2.delete(0, sb2.capacity());
sb3.delete(0, sb3.capacity());
// System.out.println(sb1);
// System.out.println(sb2);
// System.out.println(sb3);
// System.out.println("다지웠댜");
}
private boolean isNumber(String str) { // return type이 boolean이면 is를 붙인다.
boolean flag = false;
int y = str.charAt(0) - 48;
if (y >= 0 && y <= 9) {
flag = true;
}
return flag;
}
}
[출처] 빡쏘끼룩
'IT > JAVA' 카테고리의 다른 글
[Java] WindowBuilder 설치&사용 (0) | 2020.10.08 |
---|---|
[Java] Window 창 닫는 code (0) | 2020.10.06 |
[Java] java.awt.event - 야구게임 완성 (디자인,Controller,Service 3개로 분리) / 유효성검사 (0) | 2020.10.04 |
[Java] java.awt.event - Logic과 디자인 2개로 분리 (0) | 2020.10.03 |
[Java] java.awt.event 예제 (0) | 2020.10.02 |