KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CalcGUI


1
2 /*
3  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
5  * intellectual property rights relating to technology embodied in the product
6  * that is described in this document. In particular, and without limitation,
7  * these intellectual property rights may include one or more of the U.S.
8  * patents listed at http://www.sun.com/patents and one or more additional
9  * patents or pending patent applications in the U.S. and in other countries.
10  * U.S. Government Rights - Commercial software. Government users are subject
11  * to the Sun Microsystems, Inc. standard license agreement and applicable
12  * provisions of the FAR and its supplements. Use is subject to license terms.
13  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
14  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
15  * product is covered and controlled by U.S. Export Control laws and may be
16  * subject to the export or import laws in other countries. Nuclear, missile,
17  * chemical biological weapons or nuclear maritime end uses or end users,
18  * whether direct or indirect, are strictly prohibited. Export or reexport
19  * to countries subject to U.S. embargo or to entities identified on U.S.
20  * export exclusion lists, including, but not limited to, the denied persons
21  * and specially designated nationals lists is strictly prohibited.
22  */

23
24
25 import java.awt.*;
26
27 public class CalcGUI extends Frame implements CalcInputParserConstants {
28
29   /**
30    * A button object is created for each calculator button. Since
31    * there is going to be only one calculator GUI, these objects can
32    * be static.
33    */

34   static Button one = new Button("1");
35   static Button two = new Button("2");
36   static Button three = new Button("3");
37   static Button four = new Button("4");
38   static Button five = new Button("5");
39   static Button six = new Button("6");
40   static Button seven = new Button("7");
41   static Button eight = new Button("8");
42   static Button nine = new Button("9");
43   static Button zero = new Button("0");
44   static Button dot = new Button(".");
45   static Button equal = new Button("=");
46   static Button add = new Button("+");
47   static Button sub = new Button("-");
48   static Button mul = new Button("*");
49   static Button div = new Button("/");
50   static Button quit = new Button("QUIT");
51
52   /**
53    * The display window with its initial setting.
54    */

55   static Label display = new Label("0 ");
56
57   /**
58    * The class that collects the characters produced by the GUI
59    */

60   static final CharCollector collector = new CharCollector();
61
62   static final CharStream getCollector()
63   {
64      return collector;
65   }
66
67   /**
68    * string that holds what the calculator displays
69    */

70   static String JavaDoc label = "0";
71
72   /**
73    * flag to indicate if this is the first digit.
74    */

75   static boolean firstDigit = true;
76
77   /**
78    * Indicates an error has occured
79    */

80   static boolean error = false;
81
82   /**
83    * Constructor that creates the full GUI. This is called by the
84    * main program to create one calculator GUI.
85    */

86   public CalcGUI() {
87
88     super("Calculator");
89
90     GridBagLayout gb = new GridBagLayout();
91     setLayout(gb);
92     GridBagConstraints gbc = new GridBagConstraints();
93
94     display.setFont(new Font("TimesRoman", Font.BOLD, 18));
95     display.setAlignment(Label.RIGHT);
96     gbc.gridwidth = GridBagConstraints.REMAINDER;
97     gbc.fill = GridBagConstraints.BOTH;
98     gbc.weightx = 1.0;
99     gbc.weighty = 0.0;
100     gb.setConstraints(display, gbc);
101     add(display);
102
103     Panel buttonPanel = new Panel();
104     buttonPanel.setFont(new Font("TimesRoman", Font.BOLD, 14));
105     buttonPanel.setLayout(new GridLayout(4,4));
106     buttonPanel.add(one); buttonPanel.add(two); buttonPanel.add(three); buttonPanel.add(four);
107     buttonPanel.add(five); buttonPanel.add(six); buttonPanel.add(seven); buttonPanel.add(eight);
108     buttonPanel.add(nine); buttonPanel.add(zero); buttonPanel.add(dot); buttonPanel.add(equal);
109     buttonPanel.add(add); buttonPanel.add(sub); buttonPanel.add(mul); buttonPanel.add(div);
110     gbc.weighty = 1.0;
111     gb.setConstraints(buttonPanel, gbc);
112     add(buttonPanel);
113
114     quit.setFont(new Font("TimesRoman", Font.BOLD, 14));
115     gbc.gridheight = GridBagConstraints.REMAINDER;
116     gbc.weighty = 0.0;
117     gb.setConstraints(quit, gbc);
118     add(quit);
119     pack();
120     show();
121   }
122
123   /**
124    * Here we just return the character that is input.
125    */

126   public boolean handleEvent(Event evt) {
127     char c = 0;
128  
129     if (evt.id != Event.ACTION_EVENT) {
130       return false;
131     }
132
133     if (evt.target == quit) {
134       System.exit(0);
135     }
136
137     if (error)
138     {
139        if (evt.target == zero)
140        {
141           error = false;
142           print("0");
143           firstDigit = true;
144           return true;
145        }
146
147        return false;
148     }
149
150     if (evt.target == equal) {
151       c = '=';
152       firstDigit = true;
153     }
154     else if (evt.target == add) {
155       c = '+';
156       label = "0";
157       firstDigit = true;
158     }
159     else if (evt.target == sub) {
160       c = '-';
161       label = "0";
162       firstDigit = true;
163     }
164     else if (evt.target == mul) {
165       c = '*';
166       label = "0";
167       firstDigit = true;
168     }
169     else if (evt.target == div) {
170       c = '/';
171       label = "0";
172       firstDigit = true;
173     }
174     else
175     {
176       if (firstDigit)
177          label = "";
178       firstDigit = false;
179
180       if (evt.target == one) {
181         c = '1';
182         label += c;
183       }
184       else if (evt.target == two) {
185         c = '2';
186         label += c;
187       }
188       else if (evt.target == three) {
189         c = '3';
190         label += c;
191       }
192       else if (evt.target == four) {
193         c = '4';
194         label += c;
195       }
196       else if (evt.target == five) {
197         c = '5';
198         label += c;
199       }
200       else if (evt.target == six) {
201         c = '6';
202         label += c;
203       }
204       else if (evt.target == seven) {
205         c = '7';
206         label += c;
207       }
208       else if (evt.target == eight) {
209         c = '8';
210         label += c;
211       }
212       else if (evt.target == nine) {
213         c = '9';
214         label += c;
215       }
216       else if (evt.target == zero) {
217         c = '0';
218         label += c;
219       }
220       else if (evt.target == dot) {
221         c = '.';
222         label += ".";
223       }
224       else
225         return false;
226     }
227
228     print(label);
229     collector.put(c);
230     return true;
231   }
232
233   public static void print(double value) {
234     display.setText(label = Double.toString(value));
235   }
236
237   public static void print(String JavaDoc image) {
238     display.setText(label = image);
239   }
240
241   public static void Error(String JavaDoc image) {
242      print(image);
243      Toolkit.getDefaultToolkit().beep();
244      collector.Clear();
245      label = "0";
246      error = true;
247   }
248
249 }
250
Popular Tags