ActionEvent 보면서 공부
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorFrameApp extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bEquals, bPlus,
bMinus, bMulti, bDiv, bClear;
private JLabel label;
private String operation="";
public CalculatorFrameApp(String title) {
super(title);
initButtons();
init();
}
private void initButtons() {
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
bEquals = new JButton("=");
bPlus = new JButton("+");
bMinus = new JButton("-");
bMulti = new JButton("*");
bDiv = new JButton("/");
bClear = new JButton("C");
b0.setFont(new Font("DIALOG", Font.PLAIN, 20));
b1.setFont(new Font("DIALOG", Font.PLAIN, 20));
b2.setFont(new Font("DIALOG", Font.PLAIN, 20));
b3.setFont(new Font("DIALOG", Font.PLAIN, 20));
b4.setFont(new Font("DIALOG", Font.PLAIN, 20));
b5.setFont(new Font("DIALOG", Font.PLAIN, 20));
b6.setFont(new Font("DIALOG", Font.PLAIN, 20));
b7.setFont(new Font("DIALOG", Font.PLAIN, 20));
b8.setFont(new Font("DIALOG", Font.PLAIN, 20));
b9.setFont(new Font("DIALOG", Font.PLAIN, 20));
bDiv.setFont(new Font("DIALOG", Font.PLAIN, 20));
bPlus.setFont(new Font("DIALOG", Font.PLAIN, 20));
bMinus.setFont(new Font("DIALOG", Font.PLAIN, 20));
bMulti.setFont(new Font("DIALOG", Font.PLAIN, 20));
bClear.setFont(new Font("DIALOG", Font.PLAIN, 20));
bEquals.setFont(new Font("DIALOG", Font.PLAIN, 20));
b0.setBackground(Color.WHITE);
b1.setBackground(Color.WHITE);
b2.setBackground(Color.WHITE);
b3.setBackground(Color.WHITE);
b4.setBackground(Color.WHITE);
b5.setBackground(Color.WHITE);
b6.setBackground(Color.WHITE);
b7.setBackground(Color.WHITE);
b8.setBackground(Color.WHITE);
b9.setBackground(Color.WHITE);
bDiv.setBackground(Color.YELLOW);
bPlus.setBackground(Color.YELLOW);
bMinus.setBackground(Color.YELLOW);
bMulti.setBackground(Color.YELLOW);
bClear.setBackground(Color.GREEN);
bEquals.setBackground(Color.CYAN);
}
private void init() {
label = new JLabel("0");
label.setFont(new Font("DIALOG", Font.BOLD, 30));
label.setHorizontalAlignment(JLabel.RIGHT);
label.setBackground(Color.LIGHT_GRAY);
label.setForeground(Color.WHITE);
JPanel p = new JPanel(new GridLayout(4, 4, 5, 5));
p.setBackground(Color.BLACK);
p.add(bMulti);
p.add(bDiv);
p.add(bPlus);
p.add(bMinus);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b0);
p.add(bEquals);
p.add(bClear);
Container container=getContentPane();
container.setLayout(new BorderLayout(10, 10));
container.setBackground(Color.BLACK);
container.add(label, BorderLayout.NORTH);
container.add(p, BorderLayout.CENTER);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bDiv.addActionListener(this);
bPlus.addActionListener(this);
bMinus.addActionListener(this);
bMulti.addActionListener(this);
bClear.addActionListener(this);
bEquals.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setBounds(500, 100, 500, 500);
setVisible(true);
}
public static void main(String[] args) {
new CalculatorFrameApp("계산기");
}
@Override
public void actionPerformed(ActionEvent e) {
JButton eventSource = (JButton) e.getSource();
if(eventSource == bClear){
operation = "";
label.setText("0");
} else if(eventSource == bEquals){
String[] operatorArray = {"*", "/", "+", "-"};
int index = -1;
for(String operator : operatorArray){
index = operation.indexOf(operator, 1);
if(index != -1){break;}
}
if(index < 0) return;
try {
int num1 = Integer.parseInt(operation.substring(0, index));
String operator = operation.substring(index, index+1);
int num2 = Integer.parseInt(operation.substring(index + 1));
int result = 0;
switch (operator){
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
case"+":
result = num1 + num2;
break;
case"-":
result = num1 - num2;
break;
}
label.setText("" +result);
operation = result + "";
} catch (ArithmeticException exception){
label.setText("[에러] 0으로 나눌 수 없습니다.");
operation = "";
} catch (NumberFormatException exception){
JOptionPane.showMessageDialog(this, "입력한 연산식이 형식에 맞지 않습니다.");
} catch (Exception exception){
JOptionPane.showMessageDialog(this, "프로그램 실행에 예기치 못한 오류가 발생 되었습니다.");
System.exit(0);
}
} else {
operation += eventSource.getText();
label.setText(operation);
}
}
}