|
| 1 | +package com.eomjinyoung.lesson01.exam01; |
| 2 | + |
| 3 | +import java.awt.Container; |
| 4 | +import java.awt.Dimension; |
| 5 | +import java.awt.event.ActionEvent; |
| 6 | +import java.awt.event.ActionListener; |
| 7 | + |
| 8 | +import javax.swing.Box; |
| 9 | +import javax.swing.BoxLayout; |
| 10 | +import javax.swing.JButton; |
| 11 | +import javax.swing.JComboBox; |
| 12 | +import javax.swing.JFrame; |
| 13 | +import javax.swing.JOptionPane; |
| 14 | +import javax.swing.JTextField; |
| 15 | + |
| 16 | +@SuppressWarnings("serial") |
| 17 | +public class CalculatorFrame extends JFrame implements ActionListener{ |
| 18 | + JTextField operand1 = new JTextField(4); |
| 19 | + JTextField operand2 = new JTextField(4); |
| 20 | + String[] operatorData = {"+", "-", "*", "/"}; |
| 21 | + JComboBox<String> operator = new JComboBox<String>(operatorData); |
| 22 | + JButton equal = new JButton("="); |
| 23 | + JTextField result = new JTextField(6); |
| 24 | + JButton clear = new JButton("Clear"); |
| 25 | + |
| 26 | + public CalculatorFrame() { |
| 27 | + this.setTitle("Lesson01-Exam01"); |
| 28 | + |
| 29 | + Container contentPane = this.getContentPane(); |
| 30 | + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); |
| 31 | + |
| 32 | + contentPane.add(Box.createVerticalGlue()); |
| 33 | + contentPane.add(this.createInputForm()); |
| 34 | + contentPane.add(this.createToolBar()); |
| 35 | + contentPane.add(Box.createVerticalGlue()); |
| 36 | + |
| 37 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 38 | + this.pack(); |
| 39 | + this.setLocationRelativeTo(null); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void actionPerformed(ActionEvent event) { |
| 44 | + if (event.getSource() == equal) { |
| 45 | + double a = Double.parseDouble(operand1.getText()); |
| 46 | + double b = Double.parseDouble(operand2.getText()); |
| 47 | + double r = 0; |
| 48 | + |
| 49 | + try { |
| 50 | + switch (operator.getSelectedItem().toString()) { |
| 51 | + case "+": r = a + b; break; |
| 52 | + case "-": r = a - b; break; |
| 53 | + case "*": r = a * b; break; |
| 54 | + case "/": |
| 55 | + if (b == 0) throw new Exception("0 으로 나눌 수 없습니다!"); |
| 56 | + r = a / b; break; |
| 57 | + } |
| 58 | + |
| 59 | + result.setText(Double.toString(r)); |
| 60 | + |
| 61 | + } catch (Exception err) { |
| 62 | + JOptionPane.showMessageDialog( |
| 63 | + null, err.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); |
| 64 | + } |
| 65 | + } else { |
| 66 | + this.operand1.setText(""); |
| 67 | + this.operand2.setText(""); |
| 68 | + this.result.setText(""); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + private Box createInputForm() { |
| 74 | + Box box = Box.createHorizontalBox(); |
| 75 | + box.setMaximumSize(new Dimension(300, 30)); |
| 76 | + box.setAlignmentY(Box.CENTER_ALIGNMENT); |
| 77 | + box.add(operand1); |
| 78 | + box.add(operator); |
| 79 | + box.add(operand2); |
| 80 | + box.add(equal); |
| 81 | + box.add(result); |
| 82 | + equal.addActionListener(this); |
| 83 | + return box; |
| 84 | + } |
| 85 | + |
| 86 | + private Box createToolBar() { |
| 87 | + Box box = Box.createHorizontalBox(); |
| 88 | + box.add(clear); |
| 89 | + clear.addActionListener(this); |
| 90 | + return box; |
| 91 | + } |
| 92 | + |
| 93 | + public static void main(String[] args) { |
| 94 | + CalculatorFrame app = new CalculatorFrame(); |
| 95 | + app.setVisible(true); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments