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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
package Calculator;
import org.w3c.dom.Text;
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Arrays;
public class Calculator extends JFrame {
JLabel Display1;
JButton number1,number2,number3,number4,number5,number6,number7,number8,number9,number0 , Add , Subtract , Multiplication , Division , Delete , Clear , Submit ;
JTextField real_timeText = new JTextField(5), output = new JTextField(0);
JPanel Fixed_layout = new JPanel(), Text_container = new JPanel(), Text_container2 = new JPanel(), Button_container = new JPanel(), ButtonLine1_container = new JPanel(), ButtonLine2_container = new JPanel(), ButtonLine3_container = new JPanel(), ButtonLine4_container = new JPanel();
String[] Ssuanshi_save = new String[100];
FlowLayout fl = new FlowLayout(); BorderLayout Bl = new BorderLayout();
Calculator(){
Display1 = new JLabel("计算后的结果:");
number1= new JButton("1") ; number2= new JButton("2") ; number3= new JButton("3") ; number4= new JButton("4") ; number5= new JButton("5") ; number6= new JButton("6") ; number7= new JButton("7") ; number8= new JButton("8") ; number9= new JButton("9") ; number0= new JButton("0") ; Add = new JButton("+") ; Subtract = new JButton("-") ; Multiplication = new JButton("*") ; Division = new JButton("/") ; Delete = new JButton("<-") ; Clear = new JButton("C") ; Submit = new JButton("计算") ;
Font buttonFont = new Font("宋体", Font.PLAIN, 21); number1.setFont(buttonFont); number2.setFont(buttonFont); number3.setFont(buttonFont); number4.setFont(buttonFont); number5.setFont(buttonFont); number6.setFont(buttonFont); number7.setFont(buttonFont); number8.setFont(buttonFont); number9.setFont(buttonFont); number0.setFont(buttonFont); Add.setFont(buttonFont); Subtract.setFont(buttonFont); Multiplication.setFont(buttonFont); Division.setFont(buttonFont); Delete.setFont(buttonFont); Clear.setFont(buttonFont); Submit.setFont(buttonFont);
Font textFont = new Font("宋体", Font.PLAIN, 27); real_timeText.setFont(textFont); output.setFont(textFont);
number1.setPreferredSize(new Dimension(50, 50)); number2.setPreferredSize(new Dimension(50, 50)); number3.setPreferredSize(new Dimension(50, 50)); number4.setPreferredSize(new Dimension(50, 50)); number5.setPreferredSize(new Dimension(50, 50)); number6.setPreferredSize(new Dimension(50, 50)); number7.setPreferredSize(new Dimension(50, 50)); number8.setPreferredSize(new Dimension(50, 50)); number9.setPreferredSize(new Dimension(50, 50)); number0.setPreferredSize(new Dimension(50, 50)); Add.setPreferredSize(new Dimension(50, 50)); Subtract.setPreferredSize(new Dimension(50, 50)); Multiplication.setPreferredSize(new Dimension(50, 50)); Division.setPreferredSize(new Dimension(50, 50)); Delete.setPreferredSize(new Dimension(50, 50)); Clear.setPreferredSize(new Dimension(50, 50));
ButtonListener listener = new ButtonListener(); number1.addActionListener(listener); number2.addActionListener(listener); number3.addActionListener(listener); number4.addActionListener(listener); number5.addActionListener(listener); number6.addActionListener(listener); number7.addActionListener(listener); number8.addActionListener(listener); number9.addActionListener(listener); number0.addActionListener(listener); Add.addActionListener(listener); Subtract.addActionListener(listener); Multiplication.addActionListener(listener); Division.addActionListener(listener); Delete.addActionListener(listener); Clear.addActionListener(listener); Submit.addActionListener(listener);
super.setLayout(Bl); super.add(Fixed_layout); Fixed_layout.add(Text_container, BorderLayout.NORTH);
Text_container.setLayout(new BorderLayout()); Text_container.add(real_timeText, BorderLayout.CENTER); super.add(Text_container, BorderLayout.NORTH); real_timeText.setPreferredSize(new Dimension(0, 50));
Button_container.setLayout(new BorderLayout()); super.add(Button_container, BorderLayout.CENTER); ButtonLine1_container.setLayout(new FlowLayout()); ButtonLine2_container.setLayout(new FlowLayout()); ButtonLine3_container.setLayout(new FlowLayout());
ButtonLine1_container.add(number1); ButtonLine1_container.add(number2); ButtonLine1_container.add(number3); ButtonLine1_container.add(number4); ButtonLine1_container.add(number5); Button_container.add(ButtonLine1_container, BorderLayout.NORTH); ButtonLine2_container.add(number6); ButtonLine2_container.add(number7); ButtonLine2_container.add(number8); ButtonLine2_container.add(number9); ButtonLine2_container.add(number0); Button_container.add(ButtonLine2_container, BorderLayout.CENTER); ButtonLine3_container.add(Clear); ButtonLine3_container.add(Division); ButtonLine3_container.add(Multiplication); ButtonLine3_container.add(Subtract); ButtonLine3_container.add(Add); Button_container.add(ButtonLine3_container, BorderLayout.SOUTH);
Text_container2.setLayout(new BorderLayout()); Text_container2.add(output, BorderLayout.CENTER); Text_container2.add(Submit, BorderLayout.NORTH); super.add(Text_container2, BorderLayout.SOUTH); output.setPreferredSize(new Dimension(0, 50));
output.setEditable(false); super.setTitle("王果冻的小计算器"); super.setBounds(100,100,350,353); super.setVisible(true); super.setResizable(false);
} class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String command = e.getActionCommand();
switch (command) { case "1": String Text = real_timeText.getText(); String newText = Text + "1"; real_timeText.setText(newText); break; case "2": String Text1 = real_timeText.getText(); String newText1 = Text1 + "2"; real_timeText.setText(newText1); break; case "3": String Text3 = real_timeText.getText(); String newText3 = Text3 + "3"; real_timeText.setText(newText3); break; case "4": String Text4 = real_timeText.getText(); String newText4 = Text4 + "4"; real_timeText.setText(newText4); break; case "5": String Text5 = real_timeText.getText(); String newText5 = Text5 + "5"; real_timeText.setText(newText5); break; case "6": String Text6 = real_timeText.getText(); String newText6 = Text6 + "6"; real_timeText.setText(newText6); break; case "7": String Text7 = real_timeText.getText(); String newText7 = Text7 + "7"; real_timeText.setText(newText7); break; case "8": String Text8 = real_timeText.getText(); String newText8 = Text8 + "8"; real_timeText.setText(newText8); break; case "9": String Text9 = real_timeText.getText(); String newText9 = Text9 + "9"; real_timeText.setText(newText9); break; case "0": String Text0 = real_timeText.getText(); String newText0 = Text0 + "0"; real_timeText.setText(newText0); break; case "+": String Text10 = real_timeText.getText(); String newText10 = Text10 + "+"; real_timeText.setText(newText10); break; case "-": String Text11 = real_timeText.getText(); String newText11 = Text11 + "-"; real_timeText.setText(newText11); break; case "/": String Text12 = real_timeText.getText(); String newText12 = Text12 + "/"; real_timeText.setText(newText12); break; case "*": String Text13 = real_timeText.getText(); String newText13 = Text13 + "*"; real_timeText.setText(newText13); break; case "计算": String Text14 = real_timeText.getText(); Ssuanshi_save = (new String[Text14.length()]); for (int i=0; i<Ssuanshi_save.length; i++){ Ssuanshi_save[i] = String.valueOf(Text14.charAt(i)); } for (int i=0; i<Ssuanshi_save.length; i++){
if (Ssuanshi_save[i].equals("*")) { StringBuilder str1Builder = new StringBuilder(); for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) { if (Ssuanshi_save[BL_1].matches("[0-9.]")) { str1Builder.insert(0, Ssuanshi_save[BL_1]); } else { break; } } String str1 = str1Builder.toString();
StringBuilder str2Builder = new StringBuilder(); for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) { if (Ssuanshi_save[BL_2].matches("[0-9.]")) { str2Builder.append(Ssuanshi_save[BL_2]); } else { break; } } String str2 = str2Builder.toString(); double operand1 = Double.parseDouble(str1); double operand2 = Double.parseDouble(str2); String out = String.valueOf(operand1 * operand2); output.setText(out); }else if(Ssuanshi_save[i].equals("/")){ StringBuilder str1Builder = new StringBuilder(); for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) { if (Ssuanshi_save[BL_1].matches("[0-9.]")) { str1Builder.insert(0, Ssuanshi_save[BL_1]); } else { break; } } String str1 = str1Builder.toString();
StringBuilder str2Builder = new StringBuilder(); for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) { if (Ssuanshi_save[BL_2].matches("[0-9.]")) { str2Builder.append(Ssuanshi_save[BL_2]); } else { break; } } String str2 = str2Builder.toString(); double operand1 = Double.parseDouble(str1); double operand2 = Double.parseDouble(str2); String out = String.valueOf(operand1 / operand2); output.setText(out); }else if(Ssuanshi_save[i].equals("+")){ StringBuilder str1Builder = new StringBuilder(); for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) { if (Ssuanshi_save[BL_1].matches("[0-9.]")) { str1Builder.insert(0, Ssuanshi_save[BL_1]); } else { break; } } String str1 = str1Builder.toString();
StringBuilder str2Builder = new StringBuilder(); for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) { if (Ssuanshi_save[BL_2].matches("[0-9.]")) { str2Builder.append(Ssuanshi_save[BL_2]); } else { break; } } String str2 = str2Builder.toString(); double operand1 = Double.parseDouble(str1); double operand2 = Double.parseDouble(str2); String out = String.valueOf(operand1 + operand2); output.setText(out); }else if(Ssuanshi_save[i].equals("-")){ StringBuilder str1Builder = new StringBuilder(); for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) { if (Ssuanshi_save[BL_1].matches("[0-9.]")) { str1Builder.insert(0, Ssuanshi_save[BL_1]); } else { break; } } String str1 = str1Builder.toString();
StringBuilder str2Builder = new StringBuilder(); for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) { if (Ssuanshi_save[BL_2].matches("[0-9.]")) { str2Builder.append(Ssuanshi_save[BL_2]); } else { break; } } String str2 = str2Builder.toString(); double operand1 = Double.parseDouble(str1); double operand2 = Double.parseDouble(str2); String out = String.valueOf(operand1 - operand2); output.setText(out); }
} break; case "C": real_timeText.setText(""); output.setText(""); break;
} } }
public static void main(String[] args) { Calculator OBJ = new Calculator(); }
}
|