import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calc extends JFrame implements ActionListener { // ÀÌ ÁÙÀº º° ÀÇ¹Ì ¾øÀ½. // JFrameÀ» »ó¼Ó¹Þ¾ÒÀ» ¶§ ³ª¿À´Â °æ°í¸¦ ¾ø¾Ö±â À§ÇÔ public static final long serialVersionUID = 0L; // ¸â¹ö º¯¼ö ¼±¾ð public JTextField text; public boolean mode = false; public double operand1= 0; public double operand2 = 0; public String operator = ""; public Calc() { super("°è»ê±â"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); makeLayout(); } public void makeLayout() { JPanel panel = new JPanel(); text = new JTextField("0", 17); text.setHorizontalAlignment(JTextField.RIGHT); panel.add(text); add(panel, "North"); // ¹öÆ° Å©±â¸¦ °°°Ô Çϱâ À§Çؼ­.. Dimension d = new Dimension(45, 30); // ¿¬»ê ¹öÆ° »ý¼º JButton operator[] = new JButton[6]; operator[0] = new JButton("+"); operator[1] = new JButton("-"); operator[2] = new JButton("*"); operator[3] = new JButton("/"); operator[4] = new JButton("="); operator[5] = new JButton("C"); for (int i = 0; i < 6; i++) { operator[i].setPreferredSize(d); // ¹öÆ° Å©±â ¼³Á¤ operator[i].addActionListener(this); } // ¼ýÀÚ ¹öÆ° »ý¼º JButton[] button = new JButton[10]; for (int i = 0; i < 10; i++) { button[i] = new JButton(i+""); button[i].setPreferredSize(d); button[i].addActionListener(this); } // ¹öÆ°À» ³ÖÀ» ¼­ºê ÆгΠ»ý¼º JPanel[] p = new JPanel[4]; for (int i = 0; i < 4; i++) { p[i] = new JPanel(); p[i].setLayout(new FlowLayout()); p[i].setAlignmentX(JPanel.LEFT_ALIGNMENT); } p[0].add(button[7]); p[0].add(button[8]); p[0].add(button[9]); p[0].add(operator[0]); p[1].add(button[4]); p[1].add(button[5]); p[1].add(button[6]); p[1].add(operator[1]); p[2].add(button[1]); p[2].add(button[2]); p[2].add(button[3]); p[2].add(operator[2]); p[3].add(button[0]); p[3].add(operator[3]); p[3].add(operator[4]); p[3].add(operator[5]); // ÆгΠ»ý¼º JPanel pad = new JPanel(); pad.setLayout(new BoxLayout(pad, BoxLayout.Y_AXIS)); for (int i = 0; i < 4; i++) pad.add(p[i]); add(pad, BorderLayout.CENTER); pack(); setVisible(true); } public static void main(String[] args) { new Calc(); } public String cal() { // °è»ê °á°ú¸¦ ¹®ÀÚ¿­ ÇüÅ·Π¸®ÅÏ String result = ""; if(operator.equals("+")) { operand1 += operand2; result = operand1 + ""; } else if(operator == "-") { operand1 -= operand2; result = operand1 + ""; } else if(operator == "*") { operand1 *= operand2; result = operand1 + ""; } else if(operator == "/") { if (operand2 != 0) { operand1 /= operand2; result = operand1 + ""; } else { result = "NaN"; } } if (result.endsWith(".0")) { result = result.substring(0, result.length()-2); } return result; } public void cal(String op) { // ÀԷ¹ÞÀº ¼ýÀÚ°¡ ¾øÀ¸¸é ¾Æ¹« Àϵµ ÇÏÁö ¾ÊÀ½ if (text.getText().equals("") || text.getText() == null) return; if (operator.equals("")) { // °è»êÀ» óÀ½ ÇÒ ¶§ operand1 = Double.parseDouble(text.getText()); operator = op; text.setText(""); } else { // ¿¬¼Ó °è»êÀ» À§ÇÑ ºÎºÐ operand2 = Double.parseDouble(text.getText()); text.setText(cal()); operator = op; mode = true; } } public void actionPerformed(ActionEvent ae) { String cmd = ae.getActionCommand(); if(cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")) { // ¿¬»êÀÚ Ã³¸® cal(cmd); } else if (cmd.equals("=")) { // ¿¬»êÀÚ°¡ ÁöÁ¤ÀÌ ¾ÈµÇ¾î ÀÖÀ¸¸é ¾Æ¹« Àϵµ ¾ÈÇÑ´Ù. if (operator.equals("")) return; // °è»ê °á°ú ó¸® operand2 = Double.parseDouble(text.getText()); text.setText(cal()); // ´ÙÀ½ °è»êÀ» À§ÇÑ ÃʱâÈ­ operand1 = 0; operand2 = 0; operator = ""; mode = true; } else if (cmd.equals("C")) { // Ŭ¸®¾î operand1 = 0; operand2 = 0; operator = ""; text.setText("0"); } else { // ¼ýÀÚ Ã³¸® if (mode == true) { mode = false; text.setText(cmd); } else { if (text.getText().equals("0")) { text.setText(cmd); } else { text.setText(text.getText() + cmd); } } } } }