자바로 구현한 계산기 소스입니다.
꽃햇님
import java.awt.*;
import java.text.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.*;
import javax.swing.border.BevelBorder;
public class Calculator extends Frame implements ActionListener, KeyListener
{
private Panel pnButton1, pnButton2, pnButton3, pnButton4;
Button Backspace, CE, C, MC, MR, MS, M, zero, one, two, three, four, five, six, seven, eight, nine, slash, sqrt, per, x, equal, plus, minus, gob, jum, pm;
JLabel save_message;
JTextField text;
GridBagLayout layout;
GridBagConstraints container;
String first = ;
String second = ;
String total = ;
String yunsan = ;
static int equal_i = 1;
public Calculator()
{
super(계산기);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
pnButton1 = new Panel(new GridLayout(4,5,2,2)); // 객체 세로 갯수, 객체 가로 갯수 , 객체크기, 객체크기
pnButton2 = new Panel(new GridLayout(5,1,3,3));
pnButton3 = new Panel(new BorderLayout(5,5));
pnButton4 = new Panel(new GridLayout(1,3,3,3));
text = new JTextField(0, 1);
text.setHorizontalAlignment(text.RIGHT); // 초기에 오른쪽으로 이동
text.setEditable(false); // text에 버튼입력이 안되게 함
text.setDisabledTextColor(Color.BLACK);
text.setBackground(Color.WHITE);
save_message = new JLabel();
save_message.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
save_message.setPreferredSize(new Dimension(30, 30));
Backspace = new Button(Backspace);
CE = new Button(CE);
C = new Button(C);
MC = new Button(MC);
MR = new Button(MR);
MS = new Button(MS);
M = new Button(M);
zero = new Button(0);
one = new Button(1);
two = new Button(2);
three = new Button(3);
four = new Button(4);
five = new Button(5);
six = new Button(6);
seven = new Button(7);
eight = new Button(8);
nine = new Button(9);
slash = new Button(/);
sqrt = new Button(sqrt);
per = new Button(%);
x = new Button(1/x);
equal = new Button(=);
plus = new Button(+);
minus = new Button(-);
gob = new Button(*);
jum = new Button(.);
pm = new Button(+/-);
one.setForeground(Color.BLUE);
two.setForeground(Color.BLUE);
three.setForeground(Color.BLUE);
four.setForeground(Color.BLUE);
five.setForeground(Color.BLUE);
six.setForeground(Color.BLUE);
seven.setForeground(Color.BLUE);
eight.setForeground(Color.BLUE);
nine.setForeground(Color.BLUE);
zero.setForeground(Color.BLUE);
pm.setForeground(Color.BLUE);
jum.setForeground(Color.BLUE);
M.setForeground(Color.RED);
MC.setForeground(Color.RED);
MS.setForeground(Color.RED);
MR.setForeground(Color.RED);
Backspace.setForeground(Color.RED);
CE.setForeground(Color.RED);
C.setForeground(Color.RED);
gob.setForeground(Color.RED);
slash.setForeground(Color.RED);
plus.setForeground(Color.RED);
minus.setForeground(eground(Color.RED);
equal.setForeground(Color.RED);
sqrt.setForeground(Color.BLUE);
per.setForeground(Color.BLUE);
x.setForeground(Color.BLUE);
pnButton1.add(seven);
pnButton1.add(eight);
pnButton1.add(nine);
pnButton1.add(slash);
pnButton1.add(sqrt);
pnButton1.add(four);
pnButton1.add(five);
pnButton1.add(six);
pnButton1.add(gob);
pnButton1.add(per);
pnButton1.add(one);
pnButton1.add(two);
pnButton1.add(three);
pnButton1.add(minus);
pnButton1.add(x);
pnButton1.add(zero);
pnButton1.add(pm);
pnButton1.add(jum);
pnButton1.add(plus);
pnButton1.add(equal);
pnButton2.add(save_message);
pnButton2.add(M);
pnButton2.add(MC);
pnButton2.add(MR);
pnButton2.add(MS);
pnButton4.add(Backspace);
pnButton4.add(CE);
pnButton4.add(C);
pnButton3.add(BorderLayout.NORTH, text);
pnButton3.add(BorderLayout.CENTER, pnButton4);
add(BorderLayout.NORTH, pnButton3);
add(BorderLayout.CENTER, pnButton1);
add(BorderLayout.WEST, pnButton2);
Backspace.addActionListener(this);
C.addActionListener(this);
CE.addActionListener(this);
MC.addActionListener(this);
MR.addActionListener(this);
MS.addActionListener(this);
M.addActionListener(this);
zero.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
slash.addActionListener(this);
gob.addActionListener(this);
minus.addActionListener(this);
plus.addActionListener(this);
sqrt.addActionListener(this);
per.addActionListener(this);
x.addActionListener(this);
equal.addActionListener(this);
jum.addActionListener(this);
pm.addActionListener(this);
text.addKeyListener(this);
setResizable(false);
setSize(350, 250);
setBackground(new Color(250,250,250));
Dimension d = getToolkit().getScreenSize();
setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
setVisible(true);
} // end 생성자
public void keyTyped(KeyEvent e)
{
} // end public void keyTyped(KeyEvent e)
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == one)
{
test(1);
}
else if(e.getSource() == two)
{
test(2);
}
else if(e.getSource() == three)
{
test(3);
}
else if(e.getSource() == four)
{
test(4);
}
else if(e.getSource() == five)
{
test(5);
}
else if(e.getSource() == six)
{
test(6);
}
else if(e.getSource() == seven)
{
test(7);
}
else if(e.getSource() == eight)
{
test(8);
}
else if(e.getSource() == nine)
{
test(9);
}
else if(e.getSource() == zero)
{
test(0);
}
else if(e.getSource() == plus)
{
yunsan(plus);
}
else if(e.getSource() == minus)
{
yunsan(minus);
}
else if(e.getSource() == gob)
{
yunsan(gob);
}
else if(e.getSource() == slash)
{
yunsan(slash);
}
else if(e.getSource() == sqrt) // 루트
p; {
Double sqrt = Double.parseDouble(text.getText());
sqrt = Math.sqrt(sqrt);
String val7 = Double.toString(sqrt);
text.setText(val7);
}
else if(e.getSource() == per) // 퍼센트
{
if(total == && yunsan == )
{}
else
{
Double percent = Double.parseDouble(text.getText());
percent = percent / (100.0 / percent);
String val6 = Double.toString(percent);
text.setText(val6);
}
}
else if(e.getSource() == x) // 백분율
{
Double x = Double.parseDouble(text.getText());
x = 100.0 / (100.0 * x);
String val6 = Double.toString(x);
text.setText(val6);
}
else if(e.getSource() == pm) // 음수, 양수 만들기
{
Double pam = Double.parseDouble(text.getText());
if(pam 0)
{
Double real_pam = pam * -1;
String val4 = Double.toString(real_pam);
text.setText(val4);
}
else if(pam == 0) {}
else if(pam 0)
{
Double real_pam = pam * -1;
String val5 = Double.toString(real_pam);
text.setText(val5);
}
first = text.getText();
second = text.getText();
}
else if(e.getSource() == jum) // 소숫점 만들기
{
int jum_i = 0;
if((jum_i = text.getText().indexOf(., jum_i)) != -1) {}
else
{
test(.);
}
}
else if(e.getSource() == CE)
{
text.setText(0);
}
else if(e.getSource() == C)
{
second = ;
first = ;
total = ;
yunsan = ;
text.setText(0);
}
else if(e.getSource() == equal)
{
equal(yunsan);
}
else if(e.getSource() == Backspace) // 백스페이스
{
if(equal_i == text.getText().length())
{
text.setText(0);
equal_i = 1;
}
else
{
String val3 = text.getText().substring(0, text.getText().length() - equal_i);
text.setText(val3);
}
second = text.getText();
}
} // end public void actionPerformed(ActionEvent e)
private void test(String num) // 첫번째 값 입력 함수
{
if(first == && yunsan == ) // 아무런 값이 입력되지 않았을때
{
text.setText(num);
first = text.getText();
}
else if(first == 0 && yunsan == ) // 0을 입력했을때
{
text.setText(num);
first = text.getText();
}
else if(first != 0 && yunsan == ) // 0이 아닌 한자릿수로 입력되어있을때
{
String val1 = text.getText() + num;
text.setText(val1);
first = text.getText();
}
if(yunsan != && second != && text.getText() != 0으로 나눌 수 없습니다.) // 두번째 값을 넣는 함수
{
test2(num);
}
}
private void test2(String num) // 두번째에 버튼으로 값을 넣는 함수
{
&; if(second != && yunsan != && first == second) // yunsan이 눌렸을 때
{
text.setText(num);
}
else if(first != second && second.length() 0 && second != 0) // second값이 한자리수일때
{
String val2 = second + num;
text.setText(val2);
}
second = text.getText(); // 현재 값이 들어가야 다음에 입력되었을때 붙여나갈수 있다.
}
private void yunsan(String four_yunsan) // 사칙연산 눌렀을 때
{
first = text.getText(); // 첫번째 값에 현재 입력되어 있는 값이 들어가야 한다.
second = first; // 두번째 값을 입력하지 않고 equal을 계속 누를때를 위해 second에 first 값이 들어가야 한다.
yunsan = four_yunsan;
}
private void equal(String num) // = 눌렀을 때
{
if(yunsan == plus)
{
Double first_Double = Double.parseDouble(first);
Double second_Double = Double.parseDouble(second);
Double total_Double = first_Double + second_Double;
String t_total = Double.toString(total_Double);
text.setText(t_total);
}
else if(yunsan == minus)
{
Double total_Double;
Double first_Double = Double.parseDouble(first);
Double second_Double = Double.parseDouble(second);
if(second_Double 0)
{
total_Double = first_Double + second_Double;
}
else
{
total_Double = first_Double - second_Double;
}
String t_total = Double.toString(total_Double);
text.setText(t_total);
}
else if(yunsan == slash)
{
Double first_Double = Double.parseDouble(first);
Double second_Double = Double.parseDouble(second);
Double total_Double = first_Double / second_Double;
String t_total = Double.toString(total_Double);
text.setText(t_total);
}
else if(yunsan == gob)
{
Double first_Double = Double.parseDouble(first);
Double second_Double = Double.parseDouble(second);
Double total_Double = first_Double * second_Double;
String t_total = Double.toString(total_Double);
text.setText(t_total);
}
first = text.getText(); // 연산된 후 first에 현재 total값이 들어가야 다음 연산을 예외처리 없이 할수 있음
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
}
} // end public class Calculator extends JFrame implements ActionListener
간단한 예외처리만 해놨습니다.
예를 들면 25 누른후 +버튼 누른후 =버튼을 계속 누르면 25씩 쭉 더해져 갑니다.
그 상태에서 다시 예를들면 값이 250이었다
여기서 다시 +버튼 누른후 =버튼 누르면 250씩 더해갑니다.
이런식으로 예외처리 간단하게 해놨습니디만...실제적인 계산기 소스와 비교해보면 한참 모자라고....
아직도 예외처리 해야할 부분이 좀 많습니다.....;;
소스가 길긴 하지만 UI부분 노가다가 긴거지 소스자체는 별로 안깁니다.
제 소스에서 고쳐야 할 점이나 예외처리 하시면 그 부분에 대한 알고리즘이나 소스를 알려주시면 감사하겠습니다.
-
도란도란 2025-09-12
통짜 클래스라 그런지 읽기가 어려워요 ㅎ
좀 나눘으면 좋겠어욯ㅎㅎ
