实例介绍
【实例简介】附件包括报告,java源代码。工程文件夹名字叫NotePad,导入选择这个文件夹就可以运行整个文件,编译器是MyEclipse。
这是一个入门级示例,适合新手
【实例截图】
【核心代码】
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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | import Dialog.AboutDialog; import Dialog.MyFontDialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Calendar; import javax.swing.JFrame; public class NotePad extends JFrame implements ActionListener{ private String m_strDirectory= null ; private String m_strFile= null ; boolean isselect= false ; boolean isChange = false ; boolean isupselect= false ; boolean wasChange = false ; String btext= null ; boolean btupdown= false ; Font wasFont= new Font( "宋体" ,Font.PLAIN, 24 ); String wasText= null ; String isText= null ; JMenuBar menuBar = new JMenuBar(); static Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); JOptionPane helpAbout= new JOptionPane(); FileDialog Opentext= new FileDialog( this , "文件打开" ,FileDialog.LOAD); FileDialog Savetext= new FileDialog( this , "文件保存" ,FileDialog.SAVE); JPopupMenu fileAreaPopupMenu= new JPopupMenu(); JMenuItem fileAreaPopupMenuUndo = new JMenuItem( "撒消(U)" ); JMenuItem fileAreaPopupMenuCut = new JMenuItem( "剪切(E)" ); JMenuItem fileAreaPopupMenuCopy = new JMenuItem( "复制(C)" ); JMenuItem fileAreaPopupMenuPaste = new JMenuItem( "粘贴(P)" ); JMenuItem fileAreaPopupMenuDelete = new JMenuItem( "删除(L)" ); JMenuItem fileAreaPopupMenuSelectAll= new JMenuItem( "全选(A)" ); JMenu menuFile = new JMenu( "文件(F)" ); JMenuItem menuFileNew = new JMenuItem( "新建(N) " ); JMenuItem menuFileOpen = new JMenuItem( "打开(O) " ); JMenuItem menuFileSave = new JMenuItem( "保存(S) " ); JMenuItem menuFileSaveAs = new JMenuItem( "另存为(A) " ); JMenuItem menuFileExit = new JMenuItem( "退出(X) " ); JMenu menuEdit = new JMenu( "编辑(E)" ); JMenuItem menuEditundo = new JMenuItem( "撒消(U)" ); JMenuItem menuEditCut = new JMenuItem( "剪切(T)" ); JMenuItem menuEditCopy = new JMenuItem( "复制(C)" ); JMenuItem menuEditPaste = new JMenuItem( "粘贴(P)" ); JMenuItem menuEditDelete = new JMenuItem( "删除(L)" ); JMenuItem menuEditFind = new JMenuItem( "查找(F)" ); JMenuItem menuEditFindNext = new JMenuItem( "查找下一个(N)" ); JMenuItem menuEditReplace = new JMenuItem( "替换(R)" ); JMenuItem menuEditSelectAll= new JMenuItem( "全选(A)" ); JMenuItem menuEditTimeDate= new JMenuItem( "时间/日期(D)" ); JMenu menuFormat = new JMenu( "格式(O)" ); JCheckBoxMenuItem menuFormatLine= new JCheckBoxMenuItem( "自行换行(W)" , false ); JMenuItem menuFormatfont= new JMenuItem( "字体(F)" ); JMenu menuHelp = new JMenu( "帮助(H)" ); JMenuItem menuHelpFind= new JMenuItem( "查看帮助(H)" ); JMenuItem menuHelpAbout= new JMenuItem( "关于记事本(A)" ); Container panel =getContentPane(); JTextArea fileArea = new JTextArea(); JScrollPane gdt= new JScrollPane(fileArea); public NotePad() { this .setTitle( "记事本" ); Toolkit tool = this .getToolkit(); // 窗口图标! Image myimage = tool.getImage( "" ); this .setIconImage(myimage); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { exit(); } }); fileArea.getDocument().addDocumentListener( new FileAeraListener()); fileArea.setFont(wasFont); //---------------------------菜单栏------------------------------// menuBar.add(menuFile); menuFile.setMnemonic( 'F' ); menuBar.add(menuEdit); menuEdit.setMnemonic( 'E' ); menuBar.add(menuFormat); menuFormat.setMnemonic( 'O' ); menuBar.add(menuHelp); menuHelp.setMnemonic( 'H' ); //-----------------------------文件-------------------------------// menuFile.add(menuFileNew); //新建 menuFileNew.setMnemonic( 'N' ); menuFile.add(menuFileOpen); //打开 menuFileOpen.setMnemonic( 'O' ); // 访问健; menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); // 快捷健; menuFile.add(menuFileSave); //保存 menuFileSaveAs.setMnemonic( 'A' ); menuFile.add(menuFileSaveAs); //另保存 menuFileSave.setMnemonic( 'S' ); menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); menuFile.addSeparator(); menuFile.add(menuFileExit); //退出 menuFileExit.setMnemonic( 'X' ); //-----------------------编辑--------------------------// menuEdit.add(menuEditundo); //撤销 menuEditundo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); menuEditundo.setMnemonic( 'U' ); menuEdit.addSeparator(); menuEdit.add(menuEditCut); //剪切 menuEditCut.setMnemonic( 'T' ); menuEditCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); menuEditCopy.setMnemonic( 'C' ); menuEdit.add(menuEditCopy); //复制 menuEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); menuEdit.add(menuEditPaste); //粘贴 menuEditPaste.setMnemonic( 'P' ); menuEditPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); menuEditDelete.setMnemonic( 'L' ); menuEdit.add(menuEditDelete); //删除 menuEditDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); menuEdit.addSeparator(); menuEditFind.setMnemonic( 'F' ); menuEdit.add(menuEditFind); //查找 menuEditFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); menuEditFindNext.setMnemonic( 'N' ); menuEdit.add(menuEditFindNext); //查找下一个 menuEditFindNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, Event.ALT_MASK)); menuEditReplace.setMnemonic( 'R' ); menuEdit.add(menuEditReplace); //替换 menuEditReplace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK)); menuEdit.addSeparator(); menuEditSelectAll.setMnemonic( 'A' ); menuEdit.add(menuEditSelectAll); //全选 menuEditSelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); menuEdit.add(menuEditTimeDate); //时间/日期 menuEditTimeDate.setMnemonic( 'D' ); menuEditTimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, Event.ALT_MASK)); //--------------格式------------------------------------// menuFormatLine.setMnemonic( 'W' ); menuFormat.add(menuFormatLine); //自动换行 menuFormatfont.setMnemonic( 'F' ); menuFormat.add(menuFormatfont); //字体 //--------------帮助-----------------------------------// menuHelpFind.setMnemonic( 'H' ); menuHelp.add(menuHelpFind); //帮助查找 menuHelpAbout.setMnemonic( 'A' ); menuHelp.add(menuHelpAbout); //关于 //--------------文本区弹出菜单-------------------------// fileArea.setComponentPopupMenu(fileAreaPopupMenu); fileArea.add(fileAreaPopupMenu); fileAreaPopupMenu.add(fileAreaPopupMenuUndo); fileAreaPopupMenu.addSeparator(); fileAreaPopupMenu.add(fileAreaPopupMenuCut); fileAreaPopupMenu.add(fileAreaPopupMenuCopy); fileAreaPopupMenu.add(fileAreaPopupMenuPaste); fileAreaPopupMenu.add(fileAreaPopupMenuDelete); fileAreaPopupMenu.addSeparator(); fileAreaPopupMenu.add(fileAreaPopupMenuSelectAll); //---------------增加各种控件的消息响应------------------// menuFileNew.addActionListener( this ); menuFileOpen.addActionListener( this ); menuFileSave.addActionListener( this ); menuFileSaveAs.addActionListener( this ); menuFileExit.addActionListener( this ); menuEditundo.addActionListener( this ); menuEditCopy.addActionListener( this ); menuEditCut.addActionListener( this ); menuEditSelectAll.addActionListener( this ); menuEditDelete.addActionListener( this ); menuEditFind.addActionListener( this ); // menuEditFindNext.addActionListener(this); menuEditReplace.addActionListener( this ); menuEditPaste.addActionListener( this ); menuEditTimeDate.addActionListener( this ); menuFormatLine.addActionListener( this ); menuFormatfont.addActionListener( this ); menuHelpFind.addActionListener( this ); menuHelpAbout.addActionListener( this ); fileAreaPopupMenuUndo.addActionListener( this ); fileAreaPopupMenuCut.addActionListener( this ); fileAreaPopupMenuCopy.addActionListener( this ); fileAreaPopupMenuPaste.addActionListener( this ); fileAreaPopupMenuDelete.addActionListener( this ); fileAreaPopupMenuSelectAll.addActionListener( this ); menuEditFindNext.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (btext== null ) Search(); else { int a = 0 , b = 0 ; int FindStartPos = fileArea.getCaretPosition(); String str1, str2, str3, str4, strA, strB; str1 = fileArea.getText(); str2 = str1.toLowerCase(); str3 = btext; str4 = str3.toLowerCase(); //"区分大小写"的CheckBox被选中 if (isselect) //不区分大小写 { strA = str1; strB = str3; } else //区分大小写 { strA = str2; strB = str4; } if (isupselect) //向上搜索 { if (fileArea.getSelectedText() == null ) { a = strA.lastIndexOf(strB, FindStartPos - 1 ); } else { a = strA.lastIndexOf(strB,fileArea.getSelectionStart()-strB.length()); // FindStartPos-findText.getText().length()-1 } } else if (!isupselect) { System.out.println( "kkkkkkkk" ); if (fileArea.getSelectedText() == null ) { a = strA.indexOf(strB, FindStartPos ); } else { System.out.println( "ffffff" ); a = strA.indexOf(strB, fileArea.getSelectionEnd() ); //- findText.getText().length() 1 } } System.out.println(a); if (a > - 1 ) { if (isupselect) { fileArea.setCaretPosition(a); b = btext.length(); fileArea.select(a, a b); } else { fileArea.setCaretPosition(a); b = btext.length(); fileArea.select(a, a b); } } else { JOptionPane.showMessageDialog( null , "找不到您查找的内容!" , "记事本" , JOptionPane.INFORMATION_MESSAGE); } } } }); setSize( 770 , 520 ); this .setLocation( 300 , 150 ); panel.add(gdt,BorderLayout.CENTER); setJMenuBar(menuBar); wasText=fileArea.getText(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==menuFileNew){ //新建 startNewDocument(); } else if (e.getSource()==menuFileOpen){ //打开 Open(); } else if (e.getSource()==menuFileSave){ //保存 save(); } else if (e.getSource()==menuFileSaveAs){ //另存为 saveAs(); } else if (e.getSource()==menuFileExit){ //退出 exit(); } else if (e.getSource()==menuEditundo||e.getSource()==fileAreaPopupMenuUndo){ //撒消 undo(); } else if (e.getSource()==menuEditCut||e.getSource()==fileAreaPopupMenuCut){ //剪切 fileArea.cut(); } else if (e.getSource()==menuEditCopy||e.getSource()==fileAreaPopupMenuCopy){ //复制 fileArea.copy(); } else if (e.getSource()==menuEditPaste||e.getSource()==fileAreaPopupMenuPaste){ //粘贴 fileArea.paste(); } else if (e.getSource()==menuEditDelete||e.getSource()==fileAreaPopupMenuDelete){ //删除 Delete(); } else if (e.getSource()==menuEditSelectAll||e.getSource()==fileAreaPopupMenuSelectAll){ //选择全部 selectAll(); } else if (e.getSource()==menuEditFind){ //查找 Search(); } else if (e.getSource()==menuEditReplace){ //替换 Replace(); } else if (e.getSource()==menuEditTimeDate) { //时间/日期 setTimeDate(); } else if (e.getSource()==menuFormatLine){ //查找 if (menuFormatLine.isSelected()){ fileArea.setLineWrap( true ); } else { fileArea.setLineWrap( false ); } } else if (e.getSource()==menuHelpFind){ //查看帮助 try { // String filePath = "C://Windows//winhlp32.exe"; Runtime.getRuntime().exec( "cmd.exe /c " filePath); } catch (IOException ee) { JOptionPane.showMessageDialog( this , "打开系统的记事本帮助文件出错!" , "错误信息" ,JOptionPane.INFORMATION_MESSAGE); } catch (SecurityException ee){ ee.printStackTrace(); } catch (NullPointerException ee){ ee.printStackTrace(); } catch (IllegalArgumentException ee){ ee.printStackTrace(); } } else if (e.getSource()==menuHelpAbout){ //关于记事本 About(); } else if (e.getSource()==menuFormatfont){ //字体 setfont(); } } private class FileAeraListener implements DocumentListener{ public void insertUpdate(DocumentEvent e) { wasChange = isChange; isChange = true ; isText=fileArea.getText(); } public void removeUpdate(DocumentEvent e) { wasChange = isChange; isChange = true ; isText=fileArea.getText(); } public void changedUpdate(DocumentEvent e) { wasChange = isChange; isChange = true ; isText=fileArea.getText(); } } public void startNewDocument() { if (isChange){ int decision = JOptionPane.showConfirmDialog( this , "文件已更改 !\n" "是否选择保存?" , "注意" , JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (decision == JOptionPane.YES_OPTION){ saveDocument();fileArea.setText( "" );isChange = false ;} else if (decision == JOptionPane.NO_OPTION) fileArea.setText( "" ); else if (decision == JOptionPane.CANCEL_OPTION); } else { fileArea.setText( "" ); } } public void Open(){ if (isChange){ int decision = JOptionPane.showConfirmDialog( this , "文件已更改 !\n" "是否选择保存?" , "注意" , JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (decision == JOptionPane.YES_OPTION){ saveDocument();isChange = false ;} else if (decision == JOptionPane.NO_OPTION){openDocument();} else if (decision == JOptionPane.CANCEL_OPTION); } else openDocument(); } public void openDocument() { Opentext.setVisible( true ); String Stext= null ; try { if (Opentext.getFile() != null ) { m_strDirectory =Opentext.getDirectory(); m_strFile=Opentext.getFile(); File f = new File(m_strDirectory m_strFile); System.out.println(m_strDirectory m_strFile); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String s = br.readLine(); while (s != null ) { if (Stext== null )Stext=s "\n" ; else Stext = Stext s "\n" ; s = br.readLine(); } br.close(); fileArea.setText(Stext); isChange = false ; } } catch (FileNotFoundException e1) { System.out.println( "File not found: " ); } catch (IOException e2) { e2.printStackTrace(); } } public void saveDocument() { Savetext.setVisible( true ); BufferedWriter dataOut= null ; try { if (Savetext.getFile()!= null ) { String save=Savetext.getFile(); String save2= null ; if (save.length()> 3 ) { save2=save.substring(save.length()- 4 ); } else { save2=save; } if (save2.equals( ".txt" )) { m_strDirectory=Savetext.getDirectory(); m_strFile=Savetext.getFile(); dataOut= new BufferedWriter( new FileWriter(m_strDirectory m_strFile)); } else { m_strDirectory=Savetext.getDirectory(); m_strFile=Savetext.getFile() ".txt" ; dataOut= new BufferedWriter( new FileWriter(m_strDirectory m_strFile)); } dataOut.write(fileArea.getText()); dataOut.close(); } } catch (IOException e) { System.out.println( "File not found: " ); } } public void save(){ if (m_strFile!= null &&m_strDirectory!= null ) { try { BufferedWriter dataout= new BufferedWriter( new FileWriter(m_strDirectory m_strFile)); dataout.write(fileArea.getText()); dataout.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else saveDocument(); } public void saveAs() { saveDocument(); } public void Delete() { fileArea.replaceRange( "" , fileArea.getSelectionStart(), fileArea.getSelectionEnd()); } public void setTimeDate() { Calendar c1 =Calendar.getInstance(); int y = c1.get(Calendar.YEAR); int m = c1.get(Calendar.MONTH); int d = c1.get(Calendar.DATE); int h = c1.get(Calendar.HOUR); int m1 = c1.get(Calendar.MINUTE); int m2 = m 1 ; fileArea.setText(fileArea.getText() h ":" m1 " " y "/" m2 "/" d); } public void setfont(){ JFrame notepad; notepad= new JFrame( "Notepad" ); MyFontDialog font= new MyFontDialog(notepad,screen.width/ 2 - 140 ,screen.height/ 2 - 100 ); font.DialogShow(); if (font.getFont()!= null ) { fileArea.setFont(font.getFont()); } } public void Replace(){ final JDialog findDialog = new JDialog( this , "查找与替换" , false ); Container con = findDialog.getContentPane(); con.setLayout( new FlowLayout(FlowLayout.LEFT)); JLabel searchContentLabel = new JLabel( "查找内容(N) :" ); JLabel replaceContentLabel = new JLabel( "替换为(P) :" ); final JTextField findText = new JTextField( 30 ); final JTextField replaceText = new JTextField( 30 ); final JCheckBox matchcase = new JCheckBox( "区分大小写(C)" ); ButtonGroup bGroup = new ButtonGroup(); final JRadioButton Up = new JRadioButton( "向上(U)" ); final JRadioButton Down = new JRadioButton( "向下(D)" ); JButton searchNext = new JButton( "查找下一个(F)" ); JButton replace = new JButton( "替换(R)" ); final JButton replaceAll = new JButton( "全部替换(A)" ); JButton cancel = new JButton( "取消" ); JLabel Direction= new JLabel( "方向" ); searchNext.setFont( new Font( "宋体" ,Font.BOLD, 10 )); Down.setSelected( true ); //默认向下搜索 bGroup.add(Up); bGroup.add(Down); //"替换"按钮的事件处理 replace.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (replaceText.getText().length() == 0 && fileArea.getSelectedText() != null ) fileArea.replaceSelection( "" ); if (replaceText.getText().length() > 0 && fileArea.getSelectedText() != null ) fileArea.replaceSelection(replaceText.getText()); } }); //"替换全部"按钮的事件处理 replaceAll.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { fileArea.setCaretPosition( 0 ); //将光标放到编辑区开头 int a = 0 , b = 0 , replaceCount = 0 ; if (findText.getText().length() == 0 ) { JOptionPane.showMessageDialog(findDialog, "请填写查找内容!" , "提示" ,JOptionPane.WARNING_MESSAGE); findText.requestFocus( true ); return ; } while (a > - 1 ) { int FindStartPos = fileArea.getCaretPosition(); //获取光标位置 String str1, str2, str3, str4, strA, strB; str1 = fileArea.getText(); str2 = str1.toLowerCase(); str3 = findText.getText(); str4 = str3.toLowerCase(); if (matchcase.isSelected()) //大小写区分 { strA = str1; strB = str3; } else { strA = str2; strB = str4; } if (Up.isSelected()) //向上搜索 { if (fileArea.getSelectedText() == null ) { a = strA.lastIndexOf(strB, FindStartPos - 1 ); } else { a = strA.lastIndexOf(strB, FindStartPos- findText.getText().length() - 1 ); } } else //向下搜索 { if (fileArea.getSelectedText() == null ) { a = strA.indexOf(strB, FindStartPos); } else { a = strA.indexOf(strB, FindStartPos- findText.getText().length() 1 ); } } if (a > - 1 ) { if (Up.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } else if (Down.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } } else { if (replaceCount == 0 ) { JOptionPane.showMessageDialog(findDialog, "找不到您查找的内容!" , "记事本" ,JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(findDialog, "成功替换" replaceCount "个匹配内容!" , "替换成功" ,JOptionPane.INFORMATION_MESSAGE); } } if (replaceText.getText().length() == 0 && fileArea.getSelectedText() != null ) //用空字符代替选定内容 { fileArea.replaceSelection( "" ); replaceCount ; } if (replaceText.getText().length() > 0 && fileArea.getSelectedText() != null ) //用指定字符代替选定内容 { fileArea.replaceSelection(replaceText.getText()); replaceCount ; } } //end while } }); //"查找下一个"按钮事件处理 searchNext.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int a = 0 , b = 0 ; int FindStartPos = fileArea.getCaretPosition(); String str1, str2, str3, str4, strA, strB; str1 = fileArea.getText(); str2 = str1.toLowerCase(); str3 = findText.getText(); str4 = str3.toLowerCase(); //"区分大小写"的CheckBox被选中 if (matchcase.isSelected()) //区分大小写 { strA = str1; strB = str3; } else //不区分大小写 { strA = str2; strB = str4; } if (Up.isSelected()) //向上搜索 { if (fileArea.getSelectedText() == null ) { a = strA.lastIndexOf(strB, FindStartPos - 1 ); } else { a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1 ); } } else if (Down.isSelected()) { if (fileArea.getSelectedText() == null ) { a = strA.indexOf(strB, FindStartPos); } else { a = strA.indexOf(strB, FindStartPos - findText.getText().length() 1 ); } } if (a > - 1 ) { if (Up.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } else if (Down.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } } else { JOptionPane.showMessageDialog( null , "找不到您查找的内容!" , "记事本" , JOptionPane.INFORMATION_MESSAGE); } } }); //"取消"按钮及事件处理 cancel.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { findDialog.dispose(); } }); //创建"查找与替换"对话框的界面 findDialog.setSize( 390 , 160 ); findDialog.setResizable( false ); searchNext.setFocusable( false ); replace.setFocusable( false ); replaceAll.setFocusable( false ); matchcase.setFocusable( false ); cancel.setFocusable( false ); Up.setFocusable( false ); Down.setFocusable( false ); bGroup= new ButtonGroup(); findDialog.setLayout( null ); searchContentLabel.setBounds( 10 , 12 , 80 , 22 ); replaceContentLabel.setBounds( 10 , 42 , 80 , 22 ); findText.setBounds( 95 , 12 , 160 , 22 ); replaceText.setBounds( 95 , 42 , 160 , 22 ); searchNext.setBounds( 265 , 12 , 108 , 22 ); replace.setBounds( 265 , 42 , 108 , 22 ); replaceAll.setBounds( 265 , 72 , 108 , 22 ); cancel.setBounds( 265 , 102 , 108 , 22 ); Direction.setBounds( 10 , 72 , 80 , 22 ); matchcase.setBounds( 6 , 102 , 98 , 22 ); Down.setBounds( 95 , 72 , 80 , 22 ); Up.setBounds( 175 , 72 , 80 , 22 ); bGroup.add(Up); bGroup.add(Down); findDialog.add(searchContentLabel); findDialog.add(matchcase); findDialog.add(findText); findDialog.add(searchNext); findDialog.add(Direction); findDialog.add(replaceContentLabel); findDialog.add(replaceText); findDialog.add(replace); findDialog.add(replaceAll); findDialog.add(cancel); findDialog.add(Down); findDialog.add(Up); findDialog.setLocation( 500 , 300 ); findDialog.setVisible( true ); } public void Search(){ final JDialog findDialog = new JDialog( this , "查找下一个" , false ); Container con = findDialog.getContentPane(); con.setLayout( new FlowLayout(FlowLayout.LEFT)); JLabel searchContentLabel = new JLabel( " 查找内容(N) :" ); final JTextField findText = new JTextField( 17 ); final JCheckBox matchcase = new JCheckBox( "区分大小写(C)" ); ButtonGroup bGroup = new ButtonGroup(); final JRadioButton Up = new JRadioButton( "向上(U)" ); final JRadioButton Down = new JRadioButton( "向下(D)" ); Down.setSelected( true ); //默认向下搜索 bGroup.add(Up); bGroup.add(Down); JButton searchNext = new JButton( "查找下一个(F)" ); //"查找下一个"按钮事件处理 Up.setMnemonic( 'U' ); Down.setMnemonic( 'D' ); searchNext.setMnemonic( 'F' ); searchNext.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int a = 0 , b = 0 ; int FindStartPos = fileArea.getCaretPosition(); String str1, str2, str3, str4, strA, strB; str1 = fileArea.getText(); str2 = str1.toLowerCase(); str3 = findText.getText(); btext=str3; str4 = str3.toLowerCase(); //"区分大小写"的CheckBox被选中 if (matchcase.isSelected()) //不区分大小写 { isselect= true ; strA = str1; strB = str3; } else //区分大小写 { isselect= false ; strA = str2; strB = str4; } if (Up.isSelected()) //向上搜索 { isupselect= true ; if (fileArea.getSelectedText() == null ) { a = strA.lastIndexOf(strB, FindStartPos - 1 ); } else { a = strA.lastIndexOf(strB,fileArea.getSelectionStart()-findText.getText().length()); // FindStartPos-findText.getText().length()-1 } } else if (Down.isSelected()) { isupselect= false ; if (fileArea.getSelectedText() == null ) { a = strA.indexOf(strB, FindStartPos ); } else { a = strA.indexOf(strB, fileArea.getSelectionEnd() ); //- findText.getText().length() 1 } } if (a > - 1 ) { if (Up.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } else if (Down.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } } else { JOptionPane.showMessageDialog( null , "找不到您查找的内容!" , "记事本" , JOptionPane.INFORMATION_MESSAGE); } } }); //"取消"按钮及事件处理 JButton cancel = new JButton( " 取消 " ); cancel.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { findDialog.dispose(); } }); //创建"替换"对话框的界面 JPanel bottomPanel = new JPanel(); JPanel centerPanel = new JPanel(); JPanel topPanel = new JPanel(); JPanel direction = new JPanel(); direction.setBorder(BorderFactory.createTitledBorder( "方向" )); direction.add(Up); direction.add(Down); topPanel.add(searchContentLabel); topPanel.add(findText); topPanel.add(searchNext); bottomPanel.add(matchcase); bottomPanel.add(direction); bottomPanel.add(cancel); con.add(topPanel); con.add(centerPanel); con.add(bottomPanel); //设置"替换"对话框的大小、可更改大小(否)、位置和可见性 findDialog.setSize( 425 , 200 ); findDialog.setResizable( true ); findDialog.setLocation( 430 , 280 ); findDialog.setVisible( true ); } public void undo(){ String tempText= null ; tempText=isText; fileArea.setText(wasText); fileArea.selectAll(); wasText=tempText; } public void selectAll() { fileArea.selectAll(); } public void fileExit_actionPerformed(ActionEvent e) { System.exit( 0 ); } public void About(){ JFrame notepad; notepad= new JFrame( "Notepad" ); AboutDialog about= new AboutDialog(notepad,screen.width/ 2 - 140 ,screen.height/ 2 - 100 ); about.Dialog.setVisible( true ); } public void exit() { if (isChange == false ) System.exit( 1 ); else { int decision = JOptionPane.showConfirmDialog( this , "文件已更改 !\n" "是否选择保存?" , "注意" , JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (decision == JOptionPane.YES_OPTION) saveDocument(); else if (decision == JOptionPane.NO_OPTION) System.exit( 1 ); else if (decision == JOptionPane.CANCEL_OPTION); } } public static void main(String arg[]) { NotePad note = new NotePad(); note.show(); } } |
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论