1 2 23 24 25 import java.awt.*; 26 27 public class CalcGUI extends Frame implements CalcInputParserConstants { 28 29 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 55 static Label display = new Label("0 "); 56 57 60 static final CharCollector collector = new CharCollector(); 61 62 static final CharStream getCollector() 63 { 64 return collector; 65 } 66 67 70 static String label = "0"; 71 72 75 static boolean firstDigit = true; 76 77 80 static boolean error = false; 81 82 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 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 image) { 238 display.setText(label = image); 239 } 240 241 public static void Error(String image) { 242 print(image); 243 Toolkit.getDefaultToolkit().beep(); 244 collector.Clear(); 245 label = "0"; 246 error = true; 247 } 248 249 } 250 | Popular Tags |