콘솔로 만든 후에 지난 학기에 배웠던 swing으로 해보면 어떨까 싶었다.
복습할 겸 좋을 것 같아서 만들어보았다.
swing으로 만든 간단 사칙연산 계산기
package project_1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SwingMain {
JFrame frame;
JPanel panel;
JLabel result;
String total = null;
// 전역변수 초기화
int a = -1;
int b = -1;
int CalCase = 0;
boolean count;
public void Layout() {
frame = new JFrame();
panel = new JPanel();
JButton btn_num1 = new JButton("1");
JButton btn_num2 = new JButton("2");
JButton btn_num3 = new JButton("3");
JButton btn_num4 = new JButton("4");
JButton btn_num5 = new JButton("5");
JButton btn_num6 = new JButton("6");
JButton btn_num7 = new JButton("7");
JButton btn_num8 = new JButton("8");
JButton btn_num9 = new JButton("9");
JButton btn_num0 = new JButton("0");
JButton btn_plus = new JButton("+");
JButton btn_minus = new JButton("-");
JButton btn_mul = new JButton("*");
JButton btn_div = new JButton("/");
JButton btn_equal = new JButton("=");
JButton btn_reset = new JButton("ac");
result = new JLabel("0", JLabel.RIGHT);
panel.setLayout(null);
// panel에 요소들 추가
panel.add(result);
panel.add(btn_plus);
panel.add(btn_minus);
panel.add(btn_mul);
panel.add(btn_div);
panel.add(btn_num1);
panel.add(btn_num2);
panel.add(btn_num3);
panel.add(btn_num4);
panel.add(btn_num5);
panel.add(btn_num6);
panel.add(btn_num7);
panel.add(btn_num8);
panel.add(btn_num9);
panel.add(btn_num0);
panel.add(btn_equal);
panel.add(btn_reset);
// 각 요소 레이아웃에 배치
result.setBounds(50, 50, 230, 50);
btn_plus.setBounds(50, 110, 50, 50);
btn_minus.setBounds(110, 110, 50, 50);
btn_mul.setBounds(170, 110, 50, 50);
btn_div.setBounds(230, 110, 50, 50);
btn_num1.setBounds(50, 170, 50, 50);
btn_num2.setBounds(110, 170, 50, 50);
btn_num3.setBounds(170, 170, 50, 50);
btn_num4.setBounds(50, 230, 50, 50);
btn_num5.setBounds(110, 230, 50, 50);
btn_num6.setBounds(170, 230, 50, 50);
btn_num7.setBounds(50, 290, 50, 50);
btn_num8.setBounds(110, 290, 50, 50);
btn_num9.setBounds(170, 290, 50, 50);
btn_num0.setBounds(50, 350, 170, 50);
btn_equal.setBounds(230, 170, 50, 110);
btn_reset.setBounds(230, 290, 50, 110);
// 이벤트 발생
btn_num1.addActionListener(new Calculate());
btn_num2.addActionListener(new Calculate());
btn_num3.addActionListener(new Calculate());
btn_num4.addActionListener(new Calculate());
btn_num5.addActionListener(new Calculate());
btn_num6.addActionListener(new Calculate());
btn_num7.addActionListener(new Calculate());
btn_num8.addActionListener(new Calculate());
btn_num9.addActionListener(new Calculate());
btn_num0.addActionListener(new Calculate());
btn_plus.addActionListener(new Arithmetic());
btn_minus.addActionListener(new Arithmetic());
btn_mul.addActionListener(new Arithmetic());
btn_div.addActionListener(new Arithmetic());
btn_equal.addActionListener(new Equal());
btn_reset.addActionListener(new AllClear());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingMain SwingCal = new SwingMain();
SwingCal.Layout();
}
class Calculate implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(count==false) {
if(btn.getText().equals("1"))
a = 1;
else if(btn.getText().equals("2"))
a = 2;
else if(btn.getText().equals("3"))
a = 3;
else if(btn.getText().equals("4"))
a = 4;
else if(btn.getText().equals("5"))
a = 5;
else if(btn.getText().equals("6"))
a = 6;
else if(btn.getText().equals("7"))
a = 7;
else if(btn.getText().equals("8"))
a = 8;
else if(btn.getText().equals("9"))
a = 9;
else if(btn.getText().equals("0"))
a = 0;
}
else if(count==true) {
if(btn.getText().equals("1"))
b = 1;
else if(btn.getText().equals("2"))
b = 2;
else if(btn.getText().equals("3"))
b = 3;
else if(btn.getText().equals("4"))
b = 4;
else if(btn.getText().equals("5"))
b = 5;
else if(btn.getText().equals("6"))
b = 6;
else if(btn.getText().equals("7"))
b = 7;
else if(btn.getText().equals("8"))
b = 8;
else if(btn.getText().equals("9"))
b = 9;
else if(btn.getText().equals("0"))
b = 0;
}
}
}
class Arithmetic implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
count = true;
if(btn.getText().equals("+"))
CalCase = 1;
else if(btn.getText().equals("-"))
CalCase = 2;
else if(btn.getText().equals("*"))
CalCase = 3;
else if(btn.getText().equals("/"))
CalCase = 4;
}
}
class Equal implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(CalCase==1) {
total = Integer.toString(a+b);
}
else if(CalCase==2) {
total = Integer.toString(a-b);
}
else if(CalCase==3) {
total = Integer.toString(a*b);
}
else if(CalCase==4) {
total = Integer.toString(a/b);
}
result.setText(total);
}
}
class AllClear implements ActionListener {
public void actionPerformed(ActionEvent e) {
a = -1;
b = -1;
CalCase = 0;
total = null;
result.setText("0");
count = false;
}
}
}
보완해야 할 것
1. 배열을 이용해서 하면 좋을 것 같다.
2. JLabel 칸이 보이도록한다.
3. 다양한 기능 추가해보기 -> 공학용 계산기처럼
4. 이전에 계산했던 내용 저장 -> 다시 볼 수 있도록
5. 결과값에 계속해서 연산 가능하게
'Java > 계산기' 카테고리의 다른 글
JAVA로 만든 사칙연산 간단 계산기 (0) | 2020.02.08 |
---|