1 import java.awt.*; 2 3 import javax.swing.*; 4 5 import java.awt.event.*; 6 7 8 class Cal extends JFrame implements ActionListener,MouseListener{ 9 10 11 JMenuBar menubar; 12 JMenu menu_check,menu_edit,menu_help; 13 JMenuItem menuitem_standard,menuitem_science,menuitem_check,menuitem_exit,menuitem_copy, 14 menuitem_paste,menuitem1_copy,menuitem1_paste,menuitem_chelp,menuitem_about; 15 JTextField ta1; 16 int x,result2; 17 double op1,op2,opall; 18 private boolean end=false,flag=false,add=false,sub=false,cheng=false,chu=false,flagop2=false; 19 JButton b_mc,b_mr,b_ms,b_mjia,b_mjian,b_tui,b_ce,b_c,b_jj, 20 b_dui,b_7,b_8,b_9,b_chu,b_baifenhao,b_4,b_5,b_6,b_cheng, 21 b_daoshu,b_1,b_2,b_3,b_jian,b_0,b_dian,b_jia,b_dengyu; 22 JPanel p_all,p_button1,p_button2,p_txt,p1,p2,p3,p4,p5; 23 private String str,resultstr; 24 JPopupMenu popupmenu; 25 Container con=this.getContentPane(); 26 Font font=new Font("微软雅黑",Font.PLAIN,12); 27 Color color=new Color(120,220,120); 28 29 30 Cal(String s){ 31 32 super(s); 33 setSize(220,315); 34 setResizable(false); 35 setVisible(true); 36 Dimension scr=Toolkit.getDefaultToolkit().getScreenSize(); 37 Dimension frm=this.getSize(); 38 setLocation((scr.width-frm.width)/2,(scr.height-frm.height)/2); 39 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 40 41 //-----------------------------------------------------------------制作框架结构------------------------- 42 //---------------------------------------菜单栏--------------------------- 43 44 menubar=new JMenuBar(); 45 menubar.setPreferredSize(new Dimension(frm.width,19)); 46 47 48 menu_check=new JMenu("查看(V)"); 49 menu_check.setFont(font); 50 menu_check.setForeground(Color.black); 51 menuitem_standard=new JMenuItem("标准型"); 52 menuitem_standard.setFont(font); 53 menuitem_standard.setForeground(Color.black); 54 menuitem_science=new JMenuItem("科学型"); 55 menuitem_science.setFont(font); 56 menuitem_science.setForeground(Color.black); 57 menuitem_check=new JMenuItem("查看分组"); 58 menuitem_check.setFont(font); 59 menuitem_check.setForeground(Color.black); 60 menuitem_exit=new JMenuItem("退出"); 61 menuitem_exit.setFont(font); 62 menuitem_exit.setForeground(Color.black); 63 menuitem_exit.addActionListener(this); 64 menu_check.add(menuitem_standard); 65 menu_check.add(menuitem_science); 66 menu_check.addSeparator(); 67 menu_check.add(menuitem_check); 68 menu_check.addSeparator(); 69 menu_check.add(menuitem_exit); 70 menubar.add(menu_check); 71 72 73 menu_edit=new JMenu("编辑(E)"); 74 menu_edit.setFont(font); 75 menu_edit.setForeground(Color.black); 76 menu_edit.setMnemonic(KeyEvent.VK_E); 77 menuitem_copy=new JMenuItem("复制(C) "); 78 menuitem_copy.setFont(font); 79 menuitem_copy.setForeground(Color.black); 80 menuitem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); 81 menuitem_copy.addActionListener(this); 82 menuitem_paste=new JMenuItem("粘贴(P) "); 83 menuitem_paste.setFont(font); 84 menuitem_paste.setForeground(Color.black); 85 menuitem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); 86 menuitem_paste.addActionListener(this); 87 menu_edit.add(menuitem_copy); 88 menu_edit.add(menuitem_paste); 89 menubar.add(menu_edit); 90 91 92 menu_help=new JMenu("帮助(H)"); 93 menu_help.setFont(font); 94 menu_help.setForeground(Color.black); 95 menuitem_chelp=new JMenuItem("查看帮助"); 96 menuitem_chelp.setFont(font); 97 menuitem_chelp.setForeground(Color.black); 98 menuitem_about=new JMenuItem("关于计算器"); 99 menuitem_about.setFont(font); 100 menuitem_about.setForeground(Color.black); 101 menuitem_about.addActionListener(this); 102 menu_help.add(menuitem_chelp); 103 menu_help.addSeparator(); 104 menu_help.add(menuitem_about); 105 menubar.add(menu_help); 106 107 setJMenuBar(menubar); 108 109 //--------------------------------------文本框---------------------------------- 110 111 ta1=new JTextField("0"); 112 ta1.setFont(new Font("微软雅黑",Font.PLAIN,13)); 113 ta1.setEditable(false); 114 //ta1.setOpaque(false); 115 ta1.setHorizontalAlignment(JTextField.RIGHT); 116 ta1.setPreferredSize(new Dimension((frm.width-26),45)); 117 ta1.addMouseListener(this); 118 119 120 p_all=new JPanel(); 121 p_all.setPreferredSize(new Dimension((frm.width-6),250)); 122 //p_all.setBackground(color); 123 p_all.setLayout(new FlowLayout(FlowLayout.CENTER,0,3)); 124 p_txt=new JPanel(); 125 p_txt.setPreferredSize(new Dimension((frm.width-6),53)); 126 p_txt.setLayout(new FlowLayout(FlowLayout.CENTER,0,7)); 127 p_txt.add(ta1); 128 p_all.add(p_txt); 129 con.add(p_all,BorderLayout.CENTER); 130 131 //-------------------------------------按钮区---------------------------------- 132 133 p_button1=new JPanel(); 134 p_button1.setPreferredSize(new Dimension((frm.width-25),131)); 135 p_button1.setLayout(new FlowLayout(FlowLayout.LEFT,0,3)); 136 p_all.add(p_button1); 137 138 139 140 p1=new JPanel(); 141 p1.setPreferredSize(new Dimension((frm.width-25),127)); 142 p1.setLayout(new GridLayout(4,5,5,6)); 143 144 145 b_mc=new JButton("MC"); 146 b_mc.setFont(new Font("微软雅黑",Font.PLAIN,11)); 147 b_mc.setMargin(new Insets(0,0,0,0)); 148 b_mc.setForeground(Color.blue); 149 b_mc.addActionListener(this); 150 b_mr=new JButton("MR"); 151 b_mr.setFont(new Font("微软雅黑",Font.PLAIN,11)); 152 b_mr.setMargin(new Insets(0,0,0,0)); 153 b_mr.setForeground(Color.blue); 154 b_mr.addActionListener(this); 155 b_ms=new JButton("MS"); 156 b_ms.setFont(new Font("微软雅黑",Font.PLAIN,11)); 157 b_ms.setMargin(new Insets(0,0,0,0)); 158 b_ms.setForeground(Color.blue); 159 b_ms.addActionListener(this); 160 b_mjia=new JButton("M+"); 161 b_mjia.setFont(new Font("微软雅黑",Font.PLAIN,11)); 162 b_mjia.setMargin(new Insets(0,0,0,0)); 163 b_mjia.setForeground(Color.blue); 164 b_mjia.addActionListener(this); 165 b_mjian=new JButton("M-"); 166 b_mjian.setFont(new Font("微软雅黑",Font.PLAIN,11)); 167 b_mjian.setMargin(new Insets(0,0,0,0)); 168 b_mjian.setForeground(Color.blue); 169 b_mjian.addActionListener(this); 170 b_tui=new JButton("←"); 171 b_tui.setFont(new Font("微软雅黑",Font.BOLD,14)); 172 b_tui.setMargin(new Insets(0,0,0,0)); 173 b_tui.addActionListener(this); 174 b_tui.setForeground(Color.red); 175 b_ce=new JButton("CE"); 176 b_ce.setFont(new Font("微软雅黑",Font.PLAIN,11)); 177 b_ce.setMargin(new Insets(0,0,0,0)); 178 b_ce.setForeground(Color.red); 179 b_ce.addActionListener(this); 180 b_c=new JButton("C"); 181 b_c.setFont(new Font("微软雅黑",Font.PLAIN,11)); 182 b_c.setMargin(new Insets(0,0,0,0)); 183 b_c.setForeground(Color.red); 184 b_c.addActionListener(this); 185 b_jj=new JButton("±"); 186 b_jj.setFont(new Font("微软雅黑",Font.PLAIN,14)); 187 b_jj.setMargin(new Insets(0,0,0,0)); 188 b_jj.setForeground(Color.red); 189 b_jj.addActionListener(this); 190 b_dui=new JButton("√"); 191 b_dui.setFont(new Font("微软雅黑",Font.PLAIN,11)); 192 b_dui.setMargin(new Insets(0,0,0,0)); 193 b_dui.setForeground(Color.red); 194 b_dui.addActionListener(this); 195 b_7=new JButton("7"); 196 b_7.setFont(new Font("微软雅黑",Font.PLAIN,14)); 197 b_7.setMargin(new Insets(0,0,0,0)); 198 b_7.setForeground(Color.blue); 199 b_7.setMnemonic(KeyEvent.VK_7); 200 b_7.addActionListener(this); 201 b_8=new JButton("8"); 202 b_8.setFont(new Font("微软雅黑",Font.PLAIN,14)); 203 b_8.setMargin(new Insets(0,0,0,0)); 204 b_8.setForeground(Color.blue); 205 b_8.setMnemonic(KeyEvent.VK_8); 206 b_8.addActionListener(this); 207 b_9=new JButton("9"); 208 b_9.setFont(new Font("微软雅黑",Font.PLAIN,14)); 209 b_9.setMargin(new Insets(0,0,0,0)); 210 b_9.setForeground(Color.blue); 211 b_9.setMnemonic(KeyEvent.VK_9); 212 b_9.addActionListener(this); 213 b_chu=new JButton("/"); 214 b_chu.setFont(new Font("微软雅黑",Font.PLAIN,14)); 215 b_chu.setMargin(new Insets(0,0,0,0)); 216 b_chu.setForeground(Color.red); 217 b_chu.addActionListener(this); 218 b_baifenhao=new JButton("%"); 219 b_baifenhao.setFont(new Font("微软雅黑",Font.PLAIN,11)); 220 b_baifenhao.setMargin(new Insets(0,0,0,0)); 221 b_baifenhao.setForeground(Color.blue); 222 b_baifenhao.addActionListener(this); 223 b_4=new JButton("4"); 224 b_4.setFont(new Font("微软雅黑",Font.PLAIN,14)); 225 b_4.setMargin(new Insets(0,0,0,0)); 226 b_4.setForeground(Color.blue); 227 b_4.setMnemonic(KeyEvent.VK_4); 228 b_4.addActionListener(this); 229 b_5=new JButton("5"); 230 b_5.setFont(new Font("微软雅黑",Font.PLAIN,14)); 231 b_5.setMargin(new Insets(0,0,0,0)); 232 b_5.setForeground(Color.blue); 233 b_5.setMnemonic(KeyEvent.VK_5); 234 b_5.addActionListener(this); 235 b_6=new JButton("6"); 236 b_6.setFont(new Font("微软雅黑",Font.PLAIN,14)); 237 b_6.setMargin(new Insets(0,0,0,0)); 238 b_6.setForeground(Color.blue); 239 b_6.setMnemonic(KeyEvent.VK_6); 240 b_6.addActionListener(this); 241 b_cheng=new JButton("*"); 242 b_cheng.setFont(new Font("微软雅黑",Font.PLAIN,14)); 243 b_cheng.setMargin(new Insets(0,0,0,0)); 244 b_cheng.setForeground(Color.red); 245 b_cheng.addActionListener(this); 246 b_daoshu=new JButton("1/x"); 247 b_daoshu.setFont(new Font("微软雅黑",Font.PLAIN,11)); 248 b_daoshu.setMargin(new Insets(0,0,0,0)); 249 b_daoshu.setForeground(Color.blue); 250 b_daoshu.addActionListener(this); 251 b_1=new JButton("1"); 252 b_1.setFont(new Font("微软雅黑",Font.PLAIN,14)); 253 b_1.setMargin(new Insets(0,0,0,0)); 254 b_1.setForeground(Color.blue); 255 b_1.setMnemonic(KeyEvent.VK_1); 256 b_1.addActionListener(this); 257 b_2=new JButton("2"); 258 b_2.setFont(new Font("微软雅黑",Font.PLAIN,14)); 259 b_2.setMargin(new Insets(0,0,0,0)); 260 b_2.setForeground(Color.blue); 261 b_2.setMnemonic(KeyEvent.VK_2); 262 b_2.addActionListener(this); 263 b_3=new JButton("3"); 264 b_3.setFont(new Font("微软雅黑",Font.PLAIN,14)); 265 b_3.setMargin(new Insets(0,0,0,0)); 266 b_3.setForeground(Color.blue); 267 b_3.setMnemonic(KeyEvent.VK_3); 268 b_3.addActionListener(this); 269 b_jian=new JButton("-"); 270 b_jian.setFont(new Font("微软雅黑",Font.PLAIN,14)); 271 b_jian.setMargin(new Insets(0,0,0,0)); 272 b_jian.setForeground(Color.red); 273 b_jian.addActionListener(this); 274 b_0=new JButton("0"); 275 b_0.setFont(new Font("微软雅黑",Font.PLAIN,14)); 276 b_0.setMargin(new Insets(0,0,0,0)); 277 b_0.setPreferredSize(new Dimension(75,27)); 278 b_0.setForeground(Color.blue); 279 b_0.setMnemonic(KeyEvent.VK_0); 280 b_0.addActionListener(this); 281 JLabel L1=new JLabel(); 282 L1.setPreferredSize(new Dimension(5,3)); 283 b_dian=new JButton("."); 284 b_dian.setFont(new Font("微软雅黑",Font.BOLD,14)); 285 b_dian.setMargin(new Insets(0,0,0,0)); 286 b_dian.setPreferredSize(new Dimension(35,27)); 287 b_dian.setForeground(Color.blue); 288 b_dian.addActionListener(this); 289 JLabel L2=new JLabel(); 290 L2.setPreferredSize(new Dimension(5,3)); 291 b_jia=new JButton("+"); 292 b_jia.setFont(new Font("微软雅黑",Font.BOLD,14)); 293 b_jia.setMargin(new Insets(0,0,0,0)); 294 b_jia.setPreferredSize(new Dimension(35,27)); 295 b_jia.setForeground(Color.red); 296 b_jia.addActionListener(this); 297 b_dengyu=new JButton("="); 298 b_dengyu.setFont(new Font("微软雅黑",Font.BOLD,22)); 299 b_dengyu.setMargin(new Insets(0,0,0,0)); 300 b_dengyu.setPreferredSize(new Dimension(35,60)); 301 b_dengyu.setForeground(Color.blue); 302 b_dengyu.addActionListener(this); 303 304 305 p1.add(b_mc); 306 p1.add(b_mr); 307 p1.add(b_ms); 308 p1.add(b_mjia); 309 p1.add(b_mjian); 310 p1.add(b_tui); 311 p1.add(b_ce); 312 p1.add(b_c); 313 p1.add(b_jj); 314 p1.add(b_dui); 315 p1.add(b_7); 316 p1.add(b_8); 317 p1.add(b_9); 318 p1.add(b_chu); 319 p1.add(b_baifenhao); 320 p1.add(b_4); 321 p1.add(b_5); 322 p1.add(b_6); 323 p1.add(b_cheng); 324 p1.add(b_daoshu); 325 326 p_button1.add(p1); 327 328 329 p_button2=new JPanel(); 330 p_button2.setPreferredSize(new Dimension((frm.width-24),65)); 331 p_button2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); 332 p_all.add(p_button2); 333 334 335 p2=new JPanel(); 336 p2.setPreferredSize(new Dimension(156,65)); 337 p2.setLayout(new FlowLayout(FlowLayout.LEFT,0,1)); 338 p3=new JPanel(); 339 p3.setPreferredSize(new Dimension(39,62)); 340 p3.setLayout(new FlowLayout(FlowLayout.LEFT,4,0)); 341 342 343 p_button2.add(p2); 344 p_button2.add(p3); 345 346 p4=new JPanel(); 347 p4.setPreferredSize(new Dimension(156,27)); 348 p4.setLayout(new GridLayout(1,4,5,5)); 349 p5=new JPanel(); 350 p5.setPreferredSize(new Dimension(156,39)); 351 p5.setLayout(new FlowLayout(FlowLayout.LEFT,0,5)); 352 p2.add(p4); 353 p2.add(p5); 354 355 356 p4.add(b_1); 357 p4.add(b_2); 358 p4.add(b_3); 359 p4.add(b_jian); 360 p5.add(b_0); 361 p5.add(L1); 362 p5.add(b_dian); 363 p5.add(L2); 364 p5.add(b_jia); 365 p3.add(b_dengyu); 366 367 //---------------------------------------快捷菜单对象------------------------- 368 369 popupmenu=new JPopupMenu(); //快捷菜单对象 370 371 372 menuitem1_copy=new JMenuItem("复制"); 373 menuitem1_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); 374 menuitem1_copy.addActionListener(this); //监视鼠标右击菜单”复制“ 375 popupmenu.add(menuitem1_copy); 376 377 menuitem1_paste=new JMenuItem("粘贴"); 378 menuitem1_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); 379 menuitem1_paste.addActionListener(this); //监视鼠标右击菜单”粘贴“ 380 popupmenu.add(menuitem1_paste); 381 ta1.add(popupmenu); 382 383 con.validate(); 384 validate(); 385 386 } 387 388 //---------------------------------------功能区------------------------------------ 389 390 391 public void actionPerformed(ActionEvent e){ 392 if(e.getSource()==menuitem_about){ 393 JOptionPane.showMessageDialog(null,"这是一个非常简单的计算器!欢迎改进,钟作明","关于计算器",JOptionPane.INFORMATION_MESSAGE); 394 } 395 if(e.getSource()==menuitem1_copy){ 396 ta1.selectAll(); 397 ta1.copy(); 398 } 399 400 else if(e.getSource()==menuitem1_paste){ 401 ta1.setEditable(true); 402 ta1.setText(""); 403 ta1.paste(); 404 ta1.setEditable(false); 405 } 406 else if(e.getSource()==menuitem_copy){ 407 ta1.selectAll(); 408 ta1.copy(); 409 } 410 else if(e.getSource()==menuitem_paste){ 411 ta1.setEditable(true); 412 ta1.setText(""); 413 ta1.paste(); 414 ta1.setEditable(false); 415 } 416 else if(e.getSource()==menuitem_exit){ 417 System.exit(0); 418 } 419 else if(e.getSource()==b_ce){ 420 ta1.setEditable(true); 421 ta1.setText("0"); 422 ta1.setEditable(false); 423 } 424 else if(e.getSource()==b_c){ 425 ta1.setEditable(true); 426 ta1.setText("0"); 427 ta1.setEditable(false); 428 } 429 else if(e.getSource()==b_tui) 430 { 431 String str=ta1.getText(); 432 StringBuffer s=new StringBuffer(str); 433 int L=s.length(); 434 try{ 435 if(L!=1){ 436 s=s.deleteCharAt(L-1); 437 } 438 else{ 439 s=new StringBuffer("0"); 440 } 441 } 442 catch(Exception e1){} 443 444 ta1.setText(String.valueOf(s)); 445 } 446 447 //---------------------------------------------数字键----------------------------- 448 if(e.getSource()==b_1) 449 { 450 addString(1); 451 } 452 else if(e.getSource()==b_2) 453 { 454 addString(2); 455 } 456 else if(e.getSource()==b_3) 457 { 458 addString(3); 459 } 460 else if(e.getSource()==b_4) 461 { 462 addString(4); 463 } 464 else if(e.getSource()==b_5) 465 { 466 addString(5); 467 } 468 else if(e.getSource()==b_6) 469 { 470 addString(6); 471 } 472 else if(e.getSource()==b_7) 473 { 474 addString(7); 475 } 476 else if(e.getSource()==b_8) 477 { 478 addString(8); 479 } 480 else if(e.getSource()==b_9) 481 { 482 addString(9); 483 } 484 else if(e.getSource()==b_0) 485 { 486 addString(0); 487 } 488 else if(e.getSource()==b_dian){ 489 StringBuffer s1=new StringBuffer(ta1.getText()); 490 StringBuffer dian=new StringBuffer("."); 491 if(String.valueOf(s1).indexOf(".")==-1){ 492 s1.append(dian); 493 } 494 ta1.setText(String.valueOf(s1)); 495 } 496 else if(e.getSource()==b_jj){ 497 String txt=ta1.getText(); 498 int result=Integer.parseInt(txt); 499 if(txt.indexOf("-")==-1&&txt.length()>0){ 500 if(result!=0){ 501 txt="-"+txt; 502 } 503 } 504 else{ 505 StringBuffer txt1=new StringBuffer(txt); 506 txt1=txt1.deleteCharAt(0); 507 txt=String.valueOf(txt1); 508 } 509 ta1.setText(txt); 510 } 511 else if(e.getSource()==b_dui){ 512 str=ta1.getText(); 513 Double d=Double.parseDouble(str); 514 if(d>=0){ 515 double d1=Math.sqrt(d); 516 String s = String.valueOf(d1); 517 if(s.endsWith(".0")==true){ 518 int z=(int)d1; 519 str=String.valueOf(z); 520 } 521 else{ 522 str=String.valueOf(d1); 523 } 524 ta1.setText(str); 525 } 526 else{ 527 JOptionPane.showMessageDialog(null,"根号底数不能为负数","提醒", JOptionPane.INFORMATION_MESSAGE); 528 } 529 end=true; 530 } 531 else if(e.getSource()==b_baifenhao){ 532 str=ta1.getText(); 533 Double d=Double.parseDouble(str); 534 ta1.setText(""+d/100); 535 end=true; 536 } 537 else if(e.getSource()==b_daoshu){ 538 str=ta1.getText(); 539 Double d=Double.parseDouble(str); 540 if(d!=0){ 541 ta1.setText(""+1/d); 542 } 543 else{ 544 JOptionPane.showMessageDialog(null,"除数不能为零","提醒", JOptionPane.INFORMATION_MESSAGE); 545 } 546 end=true; 547 } 548 else if(e.getSource()==b_jia){ 549 str = ta1.getText(); 550 op1 = Double.parseDouble(str); 551 end=true; 552 x=0; 553 opall=op1; 554 flagop2=false; 555 } 556 else if(e.getSource()==b_jian){ 557 str = ta1.getText(); 558 op1 = Double.parseDouble(str); 559 end=true; 560 x=1; 561 opall=op1; 562 flagop2=false; 563 } 564 else if(e.getSource()==b_cheng){ 565 str = ta1.getText(); 566 op1 = Double.parseDouble(str); 567 end=true; 568 x=2; 569 opall=op1; 570 flagop2=false; 571 } 572 else if(e.getSource()==b_chu){ 573 str = ta1.getText(); 574 op1 = Double.parseDouble(str); 575 end=true; 576 x=3; 577 opall=op1; 578 flagop2=false; 579 } 580 else if(e.getSource()==b_dengyu){ 581 str = ta1.getText(); 582 if(flagop2==false){ 583 op2 = Double.parseDouble(str); 584 flagop2=true; 585 } 586 switch(x) 587 { 588 case 0 : 589 opall=opall+op2; 590 String s=String.valueOf(opall); 591 if(s.endsWith(".0")==true){ 592 result2=(int)opall; 593 resultstr=String.valueOf(result2); 594 } 595 else{ 596 resultstr=String.valueOf(opall); 597 } 598 ta1.setText(resultstr); 599 break; 600 case 1 : 601 opall=opall-op2; 602 s=String.valueOf(opall); 603 if(s.endsWith(".0")==true){ 604 result2=(int)opall; 605 resultstr=String.valueOf(result2); 606 } 607 else{ 608 resultstr=String.valueOf(opall); 609 } 610 ta1.setText(resultstr); 611 break; 612 case 2 : 613 opall=opall*op2; 614 s=String.valueOf(opall); 615 if(s.endsWith(".0")==true){ 616 result2=(int)opall; 617 resultstr=String.valueOf(result2); 618 } 619 else{ 620 resultstr=String.valueOf(opall); 621 } 622 ta1.setText(resultstr); 623 break; 624 case 3 : 625 opall=opall/op2; 626 s=String.valueOf(opall); 627 if(s.endsWith(".0")==true){ 628 result2=(int)opall; 629 resultstr=String.valueOf(result2); 630 } 631 else{ 632 resultstr=String.valueOf(opall); 633 } 634 ta1.setText(resultstr); 635 break; 636 } 637 end=true; 638 } 639 } 640 public void addString(int num) 641 { 642 String s=null; 643 s=String.valueOf(num); 644 //如果end==true;,那么屏幕清空 645 if(end==true) 646 { 647 ta1.setText("0"); 648 end=false; 649 } 650 if((ta1.getText()).equals("0")) 651 { 652 ta1.setText(s); 653 } 654 else 655 { 656 if(ta1.getText().length()<21){ 657 str=ta1.getText()+s; 658 ta1.setText(str); 659 } 660 } 661 } 662 public void mouseClicked(MouseEvent mec){ 663 if(mec.getModifiers()==mec.BUTTON3_MASK){ 664 popupmenu.show(ta1,mec.getX(),mec.getY()); 665 } 666 } 667 public void mousePressed(MouseEvent ms){} 668 public void mouseReleased(MouseEvent md){} 669 public void mouseEntered(MouseEvent ms){} 670 public void mouseExited(MouseEvent mex){} 671 public void mouseDragged(MouseEvent med){} 672 } 673 674 675 public class Calculator { 676 677 public static void main(String[] args) { 678 // TODO Auto-generated method stub 679 Cal jishuanji=new Cal("计算器"); 680 } 681 682 }

浙公网安备 33010602011771号