Design and code a GUI calculator in java
Design and code a Swing GUI calculator. You can use Display 17.19 as a starting
point, but your calculator will be more sophisticated. Your calculator will have
two text fields that the user cannot change: One labeled “Result” will contain the
result of performing the operation, and the other labeled “Operand” will be for
the user to enter a number to be added, subtracted, and so forth from the
result. The user enters the number for the “Operand” text field by clicking buttons
labeled with the digits 0 through 9 and a decimal point, just as in a real calculator.
Allow the operations of addition, subtraction, multiplication, and division. Use a
GridLayout manager to produce a button pad that looks similar to the keyboard
on a real calculator.
When the user clicks a button for an operation, the following occurs: the operation
is performed, the “Result” text field is updated, and the “Operand” text field is
cleared. Include a button labeled “Reset” that resets the “Result” to 0.0. Also
include a button labeled “Clear” that resets the “Operand” text field so it is blank.
Hint: Define an exception class named DivisonByZeroException. Have your
code throw and catch a DivisonByZeroException if the user attempts to “divide
by zero.” Your code will catch the DivisonByZeroException and output a suitable
message to the “Operand” text field. The user may then enter a new substitute
number in the “Operand” text field. Because values of type double are, in effect,
approximate values, it makes no sense to test for equality with 0.0. Consider an
operand to be “equal to zero” if it is in the range -1.0e-10 to +1.0e-10.
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /** * Calculator.java */ import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.Container; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Calculator extends JFrame /*implements ActionListener*/ { private JTextField _result, _operand; private JButton _one, _two, _three, _four, _five, _six, _seven, _eight, _nine, _zero, _point, _add, _sub, _mult, _div; public Calculator () { setTitle( "Question 3" ); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout( new BorderLayout()); add(this.createTextFields(), BorderLayout.NORTH); add(this.createNumbers(), BorderLayout.CENTER); } public JPanel createTextFields() { JPanel text = new JPanel(); text.add( new JLabel( "Result" )); _result = new JTextField( "0" , 10); _result.setEditable(false); text.add(_result); text.add( new JLabel( "Operand" )); _operand = new JTextField(10); _operand.setEditable(false); text.add(_operand); return text; } public JPanel createNumbers() { JPanel nums = new JPanel(); nums.setLayout( new BorderLayout()); JPanel ops = new JPanel(); ops.setLayout( new GridLayout(4,1)); _add = new JButton( "+" ); _add.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Calculator.this.operatorPressed( "+" ); } }); ops.add(_add); _sub = new JButton( "-" ); _sub.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Calculator.this.operatorPressed( "-" ); } }); ops.add(_sub); _mult = new JButton( "*" ); _mult.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Calculator.this.operatorPressed( "*" ); } }); ops.add(_mult); _div = new JButton( "/" ); _div.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { Calculator.this.divisionPressed(); } catch (DivisionByZeroException dbze) { _operand.setText( "Error" ); } } }); ops.add(_div); nums.add(ops, BorderLayout.EAST); JPanel numbers = new JPanel(); numbers.setLayout( new GridLayout(4,3)); _one = new JButton( "1" ); _one.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "1" ); } }); numbers.add(_one); _two = new JButton( "2" ); _two.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "2" ); } }); numbers.add(_two); _three = new JButton( "3" ); _three.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "3" ); } }); numbers.add(_three); _four = new JButton( "4" ); _four.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "4" ); } }); numbers.add(_four); _five = new JButton( "5" ); _five.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "5" ); } }); numbers.add(_five); _six = new JButton( "6" ); _six.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "6" ); } }); numbers.add(_six); _seven = new JButton( "7" ); _seven.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "7" ); } }); numbers.add(_seven); _eight = new JButton( "8" ); _eight.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "8" ); } }); numbers.add(_eight); _nine = new JButton( "9" ); _nine.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "9" ); } }); numbers.add(_nine); _point = new JButton( "." ); _point.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "." ); } }); numbers.add(_point); _zero = new JButton( "0" ); _zero.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { _operand.setText(_operand. getText () + "0" ); } }); numbers.add(_zero); nums.add(numbers, BorderLayout.CENTER); return nums; } private double getNumFromResult() { return Double.parseDouble(_result. getText ()); } private double getNumFromOperand() { return Double.parseDouble(_operand. getText ()); } private void divisionPressed() throws DivisionByZeroException { double result = getNumFromResult(); double num = getNumFromOperand(); if ( num < 1.0e-10 && num > -1.0e-10 ) { throw new DivisionByZeroException(); } // end of if () else { result /= num; _result.setText( "" + result); _operand.setText( "" ); } //end of else } private void operatorPressed(String op) { double result = getNumFromResult(); double num = getNumFromOperand(); if ( op.equals( "+" ) ) { result += num; } // end of if () else if ( op.equals( "-" ) ) { result -= num; } // end of if () else if ( op.equals( "*" ) ) { result *= num; } // end of if () _result.setText( "" + result); _operand.setText( "" ); } public static void main (String[] args) { Calculator calc = new Calculator(); calc.pack(); calc.show(); } } public class DivisionByZeroException extends Exception{ public DivisionByZeroException (){ } } |
Leave a reply