1
+ /* Jason Lee
2
+ Jsnlee At Ucdavis.edu
3
+ */
4
+
5
+ import javax .swing .*;
6
+ import java .awt .*;
7
+ import java .awt .event .*;
8
+ r
9
+ public class Calculator implements ActionListener
10
+ {
11
+ static JButton [] digitButton = new JButton [10 ];
12
+ static JButton [] temp = new JButton [18 ];
13
+ static JButton addButton , subButton , multiplyButton , divideButton , equalButton , clearButton , clearEverything , negativeButton ;
14
+ static JTextField textField ;
15
+ static double operationOne , operationTwo , result ;
16
+ static char operator ;
17
+ static boolean newNumber ;
18
+ static void addDigit (int i ) {
19
+ if (newNumber ) {
20
+ textField .setText ("" +i );
21
+ newNumber = false ;
22
+ }
23
+ else {
24
+ textField .setText (textField .getText () + i );
25
+ }
26
+ }
27
+
28
+ // To control actions
29
+ public void actionPerformed (ActionEvent e ) {
30
+ for (int i = 0 ; i < 10 ; i ++) {
31
+ if (event .getSource () == digitButton [i ]) {
32
+ addDigit (i );
33
+ return ;
34
+ }
35
+ }
36
+
37
+ // To Add Button
38
+ if (event .getSource () == addButton ) {
39
+ operationOne = Double .parseDouble (textField .getText ());
40
+ newNumber = true ;
41
+ operator = '+' ;
42
+ return ;
43
+ }
44
+
45
+ // To Subtract Button
46
+ if (event .getSource () == subButton ) {
47
+ operationOne = Double .parseDouble (textField .getText ());
48
+ newNumber = true ;
49
+ operator = '-' ;
50
+ return ;
51
+ }
52
+ // To Multiply Button
53
+ if (event .getSource () == multiplyButton ) {
54
+ operationOne = Double .parseDouble (textField .getText ());
55
+ newNumber = true ;
56
+ operator = '*' ;
57
+ return ;
58
+ }
59
+
60
+ // To Divide Button
61
+ if (event .getSource () == divideButton ) {
62
+ operationOne = Double .parseDouble (textField .getText ());
63
+ newNumber = true ;
64
+ operator = '/' ;
65
+ return ;
66
+ }
67
+
68
+ // To Clear Button
69
+ if (event .getSource () == clearButton ) {
70
+ operationOne = 0 ;
71
+ textField .setText (" " +result );
72
+ newNumber = true ;
73
+ return ;
74
+ }
75
+
76
+ // To Clear Everything
77
+ if (event .getSource () == clearEverything ) {
78
+ operationTwo = 0 ;
79
+ operationOne = 0 ;
80
+ result = 0 ;
81
+ textField .setText (" " +result );
82
+ newNumber = true ;
83
+ return ;
84
+
85
+ }
86
+
87
+ // To Negative Button
88
+ if (event .getSource () == negativeButton ) {
89
+ if (operationTwo != 0 ) {
90
+ operationTwo = operationTwo * -1 ;
91
+ }
92
+ if (operationOne != 0 ) {
93
+ operationOne = operationOne * -1 ;
94
+ }
95
+ if (result != 0 ) {
96
+ result = result * -1 ;
97
+ }
98
+ textField .setText (" " + result );
99
+ newNumber = true ;
100
+ return ;
101
+ }
102
+
103
+ // To Equal Button
104
+ if (event .getSource () == equalButton ) {
105
+ operationTwo = Double .parseDouble (textField .getText ());
106
+ switch (operator ) {
107
+ case '/' : result = operationOne / operationTwo ; break ;
108
+ case '*' : result = operationOne * operationTwo ; break ;
109
+ case '-' : result = operationOne - operationTwo ; break ;
110
+ case '+' : result = operationOne + operationTwo ; break ;
111
+ }
112
+ textField .setText (" " +result );
113
+ newNumber = true ;
114
+ return ;
115
+ }
116
+ }
117
+
118
+ public static void main (String [] args ) {
119
+ //initialize the calculator
120
+ JFrame frm = new JFrame (" Jason Lee " );
121
+ ActionListener AL = new Calculator ();
122
+ Container contentPane = frm .getContentPane ();
123
+ contentPane .setLayout (new FlowLayout ());
124
+ frm .pack ();
125
+ //set the sizing
126
+ frm .setSize (310 ,240 );
127
+ contentPane .add (new JLabel ("Calculator" ,JLabel .CENTER ));
128
+ contentPane .add (textField = new JTextField ("" ,25 ));
129
+ textField .setHorizontalAlignment (JTextField .RIGHT );
130
+ JPanel C = new JPanel (new GridLayout (4 ,4 ,2 ,2 ));
131
+ String digits [] = {"C" , "CE" , " +/- " , "/" , "7" , "8" , "9" , "*" , "4" , "5" , "6" , "-" , "1" , "2" , "3" , "+" , "." , " = " };
132
+ // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
133
+ for (int i =0 ; i <18 ; i ++) {
134
+ C .add (temp [i ] = new JButton (digits [i ]));
135
+ temp [i ].addActionListener (AL );
136
+ }
137
+ //create digit buttons
138
+ digitButton [1 ]= temp [12 ]; // 1
139
+ digitButton [2 ]= temp [13 ]; // 2
140
+ digitButton [3 ]= temp [14 ]; // 3
141
+ digitButton [4 ]= temp [8 ]; // 4
142
+ digitButton [5 ]= temp [9 ]; // 5
143
+ digitButton [6 ]= temp [10 ]; // 6
144
+ digitButton [7 ]= temp [4 ]; // 7
145
+ digitButton [8 ]= temp [5 ]; // 8
146
+ digitButton [9 ]= temp [6 ]; // 9
147
+
148
+ //create buttons
149
+ divideButton = temp [3 ]; // /
150
+ addButton = temp [15 ]; // +
151
+ subButton = temp [11 ]; // -
152
+ equalButton = temp [17 ]; // =
153
+ multiplyButton = temp [7 ]; // *
154
+ clearButton = temp [0 ]; // Clear
155
+ clearEverything = temp [1 ]; // Clear Everthing
156
+ negativeButton = temp [2 ]; // +/-
157
+
158
+ //create layout using jpanel
159
+ JPanel D = new JPanel (new GridLayout (1 ,2 ,2 ,2 ));
160
+ D .add (digitButton [0 ] = new JButton (" 0 " ));
161
+ digitButton [0 ].addActionListener (AL );
162
+ JPanel E = new JPanel (new GridLayout (1 ,2 ,2 ,2 ));
163
+ E .add (temp [16 ]);
164
+ E .add (temp [17 ]);
165
+
166
+ //create layout using jpanel
167
+ contentPane .add (C );
168
+ contentPane .add (D );
169
+ D .add (E );
170
+
171
+ // closing the calcuatlor with the exit
172
+ frm .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
173
+ frm .setVisible (true );
174
+ }
175
+ }
0 commit comments