|
| 1 | +package com.mateo.main; |
| 2 | + |
| 3 | +import javax.swing.JFrame; |
| 4 | +import javax.swing.JTextField; |
| 5 | + |
| 6 | +import com.mateo.main.Calculations.Operation; |
| 7 | + |
| 8 | +import javax.swing.JButton; |
| 9 | +import java.awt.event.ActionListener; |
| 10 | +import java.awt.event.ActionEvent; |
| 11 | + |
| 12 | +@SuppressWarnings("serial") |
| 13 | +public class CalcGUI extends JFrame { |
| 14 | + private JTextField textField; |
| 15 | + |
| 16 | + private Operation CURRENT_OP; |
| 17 | + |
| 18 | + int a; |
| 19 | + int b; |
| 20 | + |
| 21 | + public CalcGUI() { |
| 22 | + this.getContentPane().setLayout(null); |
| 23 | + this.setSize(500, 300); |
| 24 | + this.setVisible(true); |
| 25 | + |
| 26 | + textField = new JTextField(); |
| 27 | + textField.setBounds(75, 13, 345, 22); |
| 28 | + getContentPane().add(textField); |
| 29 | + textField.setColumns(10); |
| 30 | + |
| 31 | + JButton btn7 = new JButton("7"); |
| 32 | + btn7.addActionListener(new ActionListener() { |
| 33 | + public void actionPerformed(ActionEvent e) { |
| 34 | + textField.setText(textField.getText() + "7"); |
| 35 | + } |
| 36 | + }); |
| 37 | + btn7.setBounds(75, 48, 60, 25); |
| 38 | + getContentPane().add(btn7); |
| 39 | + |
| 40 | + JButton btn8 = new JButton("8"); |
| 41 | + btn8.addActionListener(new ActionListener() { |
| 42 | + public void actionPerformed(ActionEvent e) { |
| 43 | + textField.setText(textField.getText() + "8"); |
| 44 | + } |
| 45 | + }); |
| 46 | + btn8.setBounds(147, 48, 60, 25); |
| 47 | + getContentPane().add(btn8); |
| 48 | + |
| 49 | + JButton btn9 = new JButton("9"); |
| 50 | + btn9.addActionListener(new ActionListener() { |
| 51 | + public void actionPerformed(ActionEvent e) { |
| 52 | + textField.setText(textField.getText() + "9"); |
| 53 | + } |
| 54 | + }); |
| 55 | + btn9.setBounds(219, 48, 60, 25); |
| 56 | + getContentPane().add(btn9); |
| 57 | + |
| 58 | + JButton btn2 = new JButton("4"); |
| 59 | + btn2.addActionListener(new ActionListener() { |
| 60 | + public void actionPerformed(ActionEvent e) { |
| 61 | + textField.setText(textField.getText() + "4"); |
| 62 | + } |
| 63 | + }); |
| 64 | + btn2.setBounds(75, 86, 60, 25); |
| 65 | + getContentPane().add(btn2); |
| 66 | + |
| 67 | + JButton btn5 = new JButton("5"); |
| 68 | + btn5.addActionListener(new ActionListener() { |
| 69 | + public void actionPerformed(ActionEvent e) { |
| 70 | + textField.setText(textField.getText() + "5"); |
| 71 | + } |
| 72 | + }); |
| 73 | + btn5.setBounds(147, 86, 60, 25); |
| 74 | + getContentPane().add(btn5); |
| 75 | + |
| 76 | + JButton btn4 = new JButton("6"); |
| 77 | + btn4.addActionListener(new ActionListener() { |
| 78 | + public void actionPerformed(ActionEvent e) { |
| 79 | + textField.setText(textField.getText() + "6"); |
| 80 | + } |
| 81 | + }); |
| 82 | + btn4.setBounds(219, 86, 60, 25); |
| 83 | + getContentPane().add(btn4); |
| 84 | + |
| 85 | + JButton btn1 = new JButton("1"); |
| 86 | + btn1.addActionListener(new ActionListener() { |
| 87 | + public void actionPerformed(ActionEvent e) { |
| 88 | + textField.setText(textField.getText() + "1"); |
| 89 | + } |
| 90 | + }); |
| 91 | + btn1.setBounds(75, 124, 60, 25); |
| 92 | + getContentPane().add(btn1); |
| 93 | + |
| 94 | + JButton btn6 = new JButton("2"); |
| 95 | + btn6.addActionListener(new ActionListener() { |
| 96 | + public void actionPerformed(ActionEvent e) { |
| 97 | + textField.setText(textField.getText() + "2"); |
| 98 | + } |
| 99 | + }); |
| 100 | + btn6.setBounds(147, 124, 60, 25); |
| 101 | + getContentPane().add(btn6); |
| 102 | + |
| 103 | + JButton btn3 = new JButton("3"); |
| 104 | + btn3.addActionListener(new ActionListener() { |
| 105 | + public void actionPerformed(ActionEvent e) { |
| 106 | + textField.setText(textField.getText() + "3"); |
| 107 | + } |
| 108 | + }); |
| 109 | + btn3.setBounds(219, 124, 60, 25); |
| 110 | + getContentPane().add(btn3); |
| 111 | + |
| 112 | + JButton btn0 = new JButton("0"); |
| 113 | + btn0.addActionListener(new ActionListener() { |
| 114 | + public void actionPerformed(ActionEvent e) { |
| 115 | + textField.setText(textField.getText() + "0"); |
| 116 | + } |
| 117 | + }); |
| 118 | + btn0.setBounds(147, 162, 60, 25); |
| 119 | + getContentPane().add(btn0); |
| 120 | + |
| 121 | + JButton btnplus = new JButton("+"); |
| 122 | + btnplus.addActionListener(new ActionListener() { |
| 123 | + public void actionPerformed(ActionEvent e) { |
| 124 | + CURRENT_OP = Calculations.Operation.ADD; |
| 125 | + if (a == 0) { |
| 126 | + a = Integer.parseInt(textField.getText()); |
| 127 | + textField.setText(""); |
| 128 | + } else if (b == 0) { |
| 129 | + b = Integer.parseInt(textField.getText()); |
| 130 | + textField.setText(""); |
| 131 | + } |
| 132 | + System.out.println(a); |
| 133 | + System.out.println(b); |
| 134 | + } |
| 135 | + }); |
| 136 | + btnplus.setBounds(298, 48, 60, 25); |
| 137 | + getContentPane().add(btnplus); |
| 138 | + |
| 139 | + JButton btnminus = new JButton("-"); |
| 140 | + btnminus.addActionListener(new ActionListener() { |
| 141 | + public void actionPerformed(ActionEvent e) { |
| 142 | + CURRENT_OP = Calculations.Operation.SUBTRACT; |
| 143 | + if (a == 0) { |
| 144 | + a = Integer.parseInt(textField.getText()); |
| 145 | + textField.setText(""); |
| 146 | + } else if (b == 0) { |
| 147 | + b = Integer.parseInt(textField.getText()); |
| 148 | + textField.setText(""); |
| 149 | + } |
| 150 | + System.out.println(a); |
| 151 | + System.out.println(b); |
| 152 | + } |
| 153 | + }); |
| 154 | + btnminus.setBounds(360, 48, 60, 25); |
| 155 | + getContentPane().add(btnminus); |
| 156 | + |
| 157 | + JButton btndivide = new JButton("÷"); |
| 158 | + btndivide.addActionListener(new ActionListener() { |
| 159 | + public void actionPerformed(ActionEvent e) { |
| 160 | + CURRENT_OP = Calculations.Operation.DIVIDE; |
| 161 | + if (a == 0) { |
| 162 | + a = Integer.parseInt(textField.getText()); |
| 163 | + textField.setText(""); |
| 164 | + } else if (b == 0) { |
| 165 | + b = Integer.parseInt(textField.getText()); |
| 166 | + textField.setText(""); |
| 167 | + } |
| 168 | + System.out.println(a); |
| 169 | + System.out.println(b); |
| 170 | + } |
| 171 | + }); |
| 172 | + btndivide.setBounds(360, 86, 60, 25); |
| 173 | + getContentPane().add(btndivide); |
| 174 | + |
| 175 | + JButton btnmultiply = new JButton("x"); |
| 176 | + btnmultiply.addActionListener(new ActionListener() { |
| 177 | + public void actionPerformed(ActionEvent e) { |
| 178 | + CURRENT_OP = Calculations.Operation.MULTIPLY; |
| 179 | + if (a == 0) { |
| 180 | + a = Integer.parseInt(textField.getText()); |
| 181 | + textField.setText(""); |
| 182 | + } else if (b == 0) { |
| 183 | + b = Integer.parseInt(textField.getText()); |
| 184 | + textField.setText(""); |
| 185 | + } |
| 186 | + System.out.println(a); |
| 187 | + System.out.println(b); |
| 188 | + } |
| 189 | + }); |
| 190 | + btnmultiply.setBounds(298, 86, 60, 25); |
| 191 | + getContentPane().add(btnmultiply); |
| 192 | + |
| 193 | + JButton btnequals = new JButton("="); |
| 194 | + btnequals.addActionListener(new ActionListener() { |
| 195 | + public void actionPerformed(ActionEvent e) { |
| 196 | + switch (CURRENT_OP) { |
| 197 | + case ADD: |
| 198 | + textField.setText(Integer.toString(Calculations.calculate(Calculations.Operation.ADD, a, b))); |
| 199 | + a = Calculations.calculate(Calculations.Operation.ADD, a, b); |
| 200 | + b = 0; |
| 201 | + break; |
| 202 | + |
| 203 | + case SUBTRACT: |
| 204 | + textField.setText(Integer.toString(Calculations.calculate(Calculations.Operation.SUBTRACT, a, b))); |
| 205 | + a = Calculations.calculate(Calculations.Operation.SUBTRACT, a, b); |
| 206 | + b = 0; |
| 207 | + break; |
| 208 | + |
| 209 | + case MULTIPLY: |
| 210 | + textField.setText(Integer.toString(Calculations.calculate(Calculations.Operation.MULTIPLY, a, b))); |
| 211 | + a = Calculations.calculate(Calculations.Operation.MULTIPLY, a, b); |
| 212 | + b = 0; |
| 213 | + break; |
| 214 | + |
| 215 | + case DIVIDE: |
| 216 | + textField.setText(Integer.toString(Calculations.calculate(Calculations.Operation.DIVIDE, a, b))); |
| 217 | + a = Calculations.calculate(Calculations.Operation.DIVIDE, a, b); |
| 218 | + b = 0; |
| 219 | + break; |
| 220 | + } |
| 221 | + } |
| 222 | + }); |
| 223 | + btnequals.setBounds(298, 124, 60, 25); |
| 224 | + getContentPane().add(btnequals); |
| 225 | + } |
| 226 | + |
| 227 | + public static void main(String[] args) { |
| 228 | + System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); |
| 229 | + javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 230 | + public void run() { |
| 231 | + new CalcGUI(); |
| 232 | + |
| 233 | + } |
| 234 | + }); |
| 235 | + } |
| 236 | +} |
0 commit comments