工程一:记事本的实现
以下是本人设计的java记事本,功能比较简单粗暴,纯粹是为了巩固java IO和GUI的内容不足之处,欢迎各路大神指正!
以下是UI层的代码
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 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | //记事本界面层 import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; //问题:(此处注释掉变编译不通过) import javax.swing.*; import java.awt.datatransfer.*; import javax.swing.undo.*; import javax.swing.text.Document; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditListener; import javax.swing.event.*; public class noteBookUI{ //定义一个全局变量用于存储当前打开文件的路径 String nowFileSrc = null ; //执行入口 public static void main(String args[]){ noteBookUI nui = null ; nui = new noteBookUI(); nui.initBO(); } noteBookBO nbo = null ; //初始化业务对象 public void initBO(){ nbo = new noteBookBO(); } /** *以下实现ui界面 */ JFrame jFrame; JMenu jm1,jm2,jm3; JMenuBar jmb; JMenuItem jmt1,jmt2,jmt3,jmt4,jmt5,jmt6,jmt7,jmt8,jmt9,jmt10, jmt11,jmt12,jmt13,jmt14,jmt15,jmt16,jmt17; //文本输入区域 final JTextArea text;JScrollPane jp; public noteBookUI(){ //主窗口 jFrame = new JFrame( "记事本" ); //菜单条 jmb= new JMenuBar(); //主菜单 //创建统一字体 Font font_jm= new Font( "微软雅黑" ,Font.PLAIN, 20 ); jm1= new JMenu( "文件" ); jm1.setFont(font_jm); jm2= new JMenu( "编辑" ); jm2.setFont(font_jm); jm3= new JMenu( "格式" ); jm3.setFont(font_jm); //设置按钮的大小 jm1.setPreferredSize( new Dimension( 50 , 20 )); jm2.setPreferredSize( new Dimension( 50 , 20 )); jm3.setPreferredSize( new Dimension( 50 , 20 )); //定义两个常量,设置按钮的长宽 int WIDTH = 200 ; //宽度 int HEIGH = 30 ; //高度 //创建文件菜单下的菜单按钮 //创建字体对象 Font font_jmt= new Font( "微软雅黑" ,Font.PLAIN, 20 ); jmt1 = new JMenuItem( " 新建(N) Ctrl+N" ); jmt1.setFont(font_jmt); jmt2 = new JMenuItem( " 打开(O) Ctrl+O" ); jmt2.setFont(font_jmt); jmt3 = new JMenuItem( " 保存(S) Ctrl+S" ); jmt3.setFont(font_jmt); jmt4 = new JMenuItem( " 另存为(A)" ); jmt4.setFont(font_jmt); jmt5 = new JMenuItem( " 退出(X)" ); jmt5.setFont(font_jmt); //设置按钮大小 jmt1.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt2.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt3.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt4.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt5.setPreferredSize( new Dimension(WIDTH,HEIGH)); //将相关按钮添加到相关菜单 jm1.add(jmt1); jm1.add(jmt2); jm1.add(jmt3); jm1.add(jmt4); //创建编辑菜单下的按钮 jmt6 = new JMenuItem( " 撤销(U) Ctrl+Z" ); jmt6.setFont(font_jmt); jmt7 = new JMenuItem( " 剪切(T) Ctrl+X" ); jmt7.setFont(font_jmt); jmt8 = new JMenuItem( " 复制(C) Ctrl+C" ); jmt8.setFont(font_jmt); jmt9 = new JMenuItem( " 粘贴(P) Ctrl+V" ); jmt9.setFont(font_jmt); jmt10 = new JMenuItem( " 删除(L) Del" ); jmt10.setFont(font_jmt); jmt11 = new JMenuItem( " 查找(F) Ctrl+F" ); jmt11.setFont(font_jmt); jmt12 = new JMenuItem( " 查找下一个(N) F3" ); jmt12.setFont(font_jmt); jmt13 = new JMenuItem( " 替换(R) Ctrl+H" ); jmt13.setFont(font_jmt); jmt14 = new JMenuItem( " 替换(A) Ctrl+A" ); jmt14.setFont(font_jmt); //设置按钮大小 jmt6.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt7.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt8.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt9.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt10.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt11.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt12.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt13.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt14.setPreferredSize( new Dimension(WIDTH,HEIGH)); //添加相关按钮到编辑下拉菜单中 jm2.add(jmt6); jm2.add(jmt7); jm2.add(jmt8); jm2.add(jmt9); jm2.add(jmt10); jm2.add(jmt11); jm2.add(jmt12); jm2.add(jmt13); jm2.add(jmt14); //创建格式下拉菜单下的菜单 jmt15 = new JMenuItem( " 自动换行(W)" ); jmt15.setFont(font_jmt); jmt16 = new JMenuItem( " 字体(F)" ); jmt16.setFont(font_jmt); jmt15.setPreferredSize( new Dimension(WIDTH,HEIGH)); jmt16.setPreferredSize( new Dimension(WIDTH,HEIGH)); //添加 jm3.add(jmt15); jm3.add(jmt16); jmb.add(jm1); jmb.add(Box.createHorizontalStrut( 50 )); jmb.add(jm2); jmb.add(Box.createHorizontalStrut( 50 )); jmb.add(jm3); jFrame.add(jmb); //添加位置大小属性 jFrame.add(jmb, "North" ); //创建滚动输入窗体 text = new JTextArea(); //设置输入框的默认字体 Font font_write = new Font( "微软雅黑" ,Font.PLAIN, 20 ); text.setFont(font_write); jp = new JScrollPane(text); //显示水平(垂直)滚动条 jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); jFrame.add(jp); //往编辑区域添加撤销相关组件 final UndoManager undo = new UndoManager(); Document doc = text.getDocument(); //注册给定的观察者以便开始接收文档发生不可撤消的编辑的通知 doc.addUndoableEditListener( new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); //快捷键实现 //新建 text.getActionMap().put( "NewFile" , new AbstractAction( "NewFile" ) { public void actionPerformed(ActionEvent evt) { //新建文件之前弹出是否保存当前文件数据窗口 //beforeCreatFile(); String srcFile = null ; //选择存储路径,弹出路径选择窗口 //srcFile = srcSelect("新建"); srcFile = showSelectSrc_newFile(); //如果用户没有选择路径直接取消操作,则退出该按钮操作 if (srcFile== null ){ return ; } else { //路径选择好后,弹出文件名字输入框 //String fileName = showGetName(); //调用业务类中的方法创建文件,成功后初始化当前文件路径并弹出提示窗口 nowFileSrc = nbo.doCreateFile(srcFile); //更改窗体标题为当前文件名 jFrame.setTitle(srcFile); //创建成功后弹出提示窗口 JOptionPane.showMessageDialog( null , "创建文件成功" ); } } }); //打开 text.getActionMap().put( "Open" , new AbstractAction( "Open" ) { public void actionPerformed(ActionEvent evt) { String srcFile = null ; srcFile = showSelectSrc_openFile(); //判断是否选择了路径,如果路径选择成功,执行打开操作 if (srcFile!= null ){ nowFileSrc = srcFile; String fileInfo = nbo.doOpen(srcFile); //将文件的信息传递给文本框 text.setText(fileInfo); } else { JOptionPane.showMessageDialog( null , "文件打开失败,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //保存 text.getActionMap().put( "Conserver" , new AbstractAction( "Conserver" ) { public void actionPerformed(ActionEvent evt) { //获取输入文本区域的值 String a = text.getText(); System.out.println(a); //当前已打开文件,执行保存操作,否则提示创建或打开文件 if (nowFileSrc!= null ){ int rtn = nbo.doConserve(nowFileSrc,a); if (rtn== 1 ){ JOptionPane.showMessageDialog( null , "保存成功!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( null , "保存失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } else { String src = showSelectSrc_saveOther(); int rtn = nbo.doConserve(src,a); if (rtn== 1 ){ JOptionPane.showMessageDialog( null , "保存成功!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( null , "保存失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } } }); //另存为 text.getActionMap().put( "SaveAs" , new AbstractAction( "SaveAs" ) { public void actionPerformed(ActionEvent evt) { //进行所有操作之前先判断用户是否已经打开文件 if (nowFileSrc== null ){ showNotOpenFileInfo(); return ; } else { //获取输入文本区域的值 String a = text.getText(); //跳转至路径选择界面 String srcFile4 = null ; srcFile4 = showSelectSrc_saveOther(); //判断路径选择操作是否成功 if (srcFile4== null ){ return ; } else { nbo.doConserve(srcFile4,a); } } } }); //撤销 text.getActionMap().put( "Undo" , new AbstractAction( "Undo" ) { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()){ undo.undo(); } else { JOptionPane.showMessageDialog( null , "当前不能进行撤销操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } catch (CannotUndoException e){ //不可撤销后弹出提示信息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //剪切 text.getActionMap().put( "JianQie" , new AbstractAction( "JianQie" ) { public void actionPerformed(ActionEvent evt) { //判断当前编辑区域有无文本 if (text.getText().equals( "" )){ JOptionPane.showMessageDialog( null , "当前文本编辑区域为空,不能进行剪切操作" , "提示" , JOptionPane.ERROR_MESSAGE); } else { int rtn = nbo.doJianQie(text); if (rtn == 1 ){ JOptionPane.showMessageDialog( null , "剪切成功!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( null , "剪切操作失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } } }); //复制 text.getActionMap().put( "Copy" , new AbstractAction( "Copy" ) { public void actionPerformed(ActionEvent evt) { int rtn = nbo.doCopy(text); if (rtn == - 1 ){ JOptionPane.showMessageDialog( null , "复制操作失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //粘贴 text.getActionMap().put( "Paste" , new AbstractAction( "Paste" ) { public void actionPerformed(ActionEvent evt) { try { int rtn = nbo.doPaste(text); if (rtn==- 1 ){ //剪切板内容不是文本内容 JOptionPane.showMessageDialog( null , "当前内容是非文本,无法粘贴!" , "提示" , JOptionPane.ERROR_MESSAGE); } if (rtn== 1 ){ //剪切板为空 JOptionPane.showMessageDialog( null , "当前剪切板无内容,请先进行复制操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //删除 text.getActionMap().put( "Delete" , new AbstractAction( "Delete" ) { public void actionPerformed(ActionEvent evt) { int rtn = nbo.doDelete(text); if (rtn == - 1 ){ JOptionPane.showMessageDialog( null , "删除操作失败,请选择您要删除的区域!" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //查找 text.getActionMap().put( "Search" , new AbstractAction( "Search" ) { public void actionPerformed(ActionEvent evt) { try { if ( "" .equals(text.getText())){ //文本编辑区域为空,提示无法查找 JOptionPane.showMessageDialog( null , "当前编辑区域没有内容,无法查找!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { showSearch(); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //查找下一个 text.getActionMap().put( "SearchNext" , new AbstractAction( "SearchNext" ) { public void actionPerformed(ActionEvent evt) { //这里和查找操作一样 try { if ( "" .equals(text.getText())){ //文本编辑区域为空,提示无法查找 JOptionPane.showMessageDialog( null , "当前编辑区域没有内容,无法查找!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { showSearch(); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //替换 text.getActionMap().put( "Replace" , new AbstractAction( "Replace" ) { //这里和查找操作一样 public void actionPerformed(ActionEvent evt) { try { if ( "" .equals(text.getText())){ //文本编辑区域为空,提示无法查找 JOptionPane.showMessageDialog( null , "当前编辑区域没有内容,无法查找!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { showSearch(); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //全选 text.getActionMap().put( "SelectAll" , new AbstractAction( "SelectAll" ) { public void actionPerformed(ActionEvent evt) { try { if ( "" .equals(text.getText())){ //若文本区域没有文本内容,弹出提示框 JOptionPane.showMessageDialog( null , "当前编辑区域没有内容,无法全选!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { nbo.chooseAll(text); } } catch (Exception e){ //有异常弹出提示信息 JOptionPane.showMessageDialog( null , "操作出错,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } //标记 } }); //添加相关事件 text.getInputMap().put(KeyStroke.getKeyStroke( "control N" ), "NewFile" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control O" ), "Open" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control S" ), "Conserver" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control Z" ), "Undo" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control X" ), "JianQie" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control C" ), "Copy" ); text.getInputMap().put(KeyStroke.getKeyStroke( "Del" ), "Delete" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control F" ), "Search" ); text.getInputMap().put(KeyStroke.getKeyStroke( "F3" ), "SearchNext" ); text.getInputMap().put(KeyStroke.getKeyStroke( "control A" ), "SelectAll" ); //设置窗口属性 jFrame.setVisible( true ); jFrame.setSize( 1000 , 1000 ); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //监听相关事件 //监听主菜单按钮 //监听编辑按钮事件,每次单击菜单按钮都需要更新各个按钮是否可操作 jm2.addMenuListener( new MenuListener(){ //由于MenuListener是抽象类,所以实现另外两个方法 public void menuDeselected(MenuEvent e){ return ; } public void menuCanceled(MenuEvent e){ return ; } public void menuSelected(MenuEvent e){ //更新各个按钮可操作状态 //判断撤销是否可用(为什么不用变成final的?) System.out.println( "Test jm2ActionListen" ); jmt6.setEnabled(undo.canUndo()); //判断剪切是否可用 if ((text.getText()== null )||(text.getSelectedText()== null )){ jmt7.setEnabled( false ); } //判断复制是否可用 if ((text.getText()== null )||(text.getSelectedText()== null )){ jmt8.setEnabled( false ); } //判断粘贴是否可用 String value = null ; try { Clipboard c1 = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipT = c1.getContents( null ); value = (String)clipT.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex){ value = "" ; } if (value.equals( "" )){ System.out.println( "Test jm2ActionListen zhanTie" ); jmt9.setEnabled( false ); } //判断删除是否可用 if (nbo.doDelete(text)!= 1 ){ jmt10.setEnabled( false ); } } }); //监听新建按钮事件 jmt1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //新建文件之前弹出是否保存当前文件数据窗口 //beforeCreatFile(); String srcFile = null ; //选择存储路径,弹出路径选择窗口 //srcFile = srcSelect("新建"); srcFile = showSelectSrc_newFile(); //如果用户没有选择路径直接取消操作,则退出该按钮操作 if (srcFile== null ){ return ; } else { //路径选择好后,弹出文件名字输入框 //String fileName = showGetName(); //调用业务类中的方法创建文件,成功后初始化当前文件路径并弹出提示窗口 nowFileSrc = nbo.doCreateFile(srcFile); //创建成功后弹出提示窗口 JOptionPane.showMessageDialog( null , "创建文件成功" ); } } }); //类似地监听其他按钮 //监听打开按钮 jmt2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ String srcFile = null ; srcFile = showSelectSrc_openFile(); //判断是否选择了路径,如果路径选择成功,执行打开操作 if (srcFile!= null ){ nowFileSrc = srcFile; String fileInfo = nbo.doOpen(srcFile); //将文件的信息传递给文本框 text.setText(fileInfo); } else { JOptionPane.showMessageDialog( null , "文件打开失败,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //监听保存 jmt3.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //获取输入文本区域的值 String a = text.getText(); System.out.println(a); //当前已打开文件,执行保存操作,否则提示创建或打开文件 if (nowFileSrc!= null ){ int rtn = nbo.doConserve(nowFileSrc,a); if (rtn== 1 ){ JOptionPane.showMessageDialog( null , "保存成功!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( null , "保存失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } else { String src = showSelectSrc_saveOther(); int rtn = nbo.doConserve(src,a); if (rtn== 1 ){ JOptionPane.showMessageDialog( null , "保存成功!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( null , "保存失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } } }); //监听另存为按钮 jmt4.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //进行所有操作之前先判断用户是否已经打开文件 if (nowFileSrc== null ){ showNotOpenFileInfo(); return ; } else { //获取输入文本区域的值 String a = text.getText(); //跳转至路径选择界面 String srcFile4 = null ; srcFile4 = showSelectSrc_saveOther(); //判断路径选择操作是否成功 if (srcFile4== null ){ return ; } else { nbo.doConserve(srcFile4,a); } } } }); /** *编辑菜单下的按钮监听 */ //监听撤销按钮 jmt6.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { undo.undo(); } catch (CannotRedoException cre){ JOptionPane.showMessageDialog( null , "当前不能进行撤销操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } //更新按钮是否可用 jmt6.setEnabled(undo.canUndo()); } }); //监听剪切按钮 jmt7.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //判断当前编辑区域有无文本 if (text.getText().equals( "" )){ JOptionPane.showMessageDialog( null , "当前文本编辑区域为空,不能进行剪切操作" , "提示" , JOptionPane.ERROR_MESSAGE); } else { int rtn = nbo.doJianQie(text); if (rtn == 1 ){ JOptionPane.showMessageDialog( null , "剪切成功!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( null , "剪切操作失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } } }); //监听复制按钮 jmt8.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ int rtn = nbo.doCopy(text); if (rtn == - 1 ){ JOptionPane.showMessageDialog( null , "复制操作失败,请重新操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //监听粘贴按钮 jmt9.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ try { int rtn = nbo.doPaste(text); if (rtn==- 1 ){ //剪切板内容不是文本内容 JOptionPane.showMessageDialog( null , "当前内容是非文本,无法粘贴!" , "提示" , JOptionPane.ERROR_MESSAGE); } if (rtn== 1 ){ //剪切板为空 JOptionPane.showMessageDialog( null , "当前剪切板无内容,请先进行复制操作!" , "提示" , JOptionPane.ERROR_MESSAGE); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //监听删除按钮 jmt10.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ int rtn = nbo.doDelete(text); if (rtn == - 1 ){ JOptionPane.showMessageDialog( null , "删除操作失败,请选择您要删除的区域!" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //监听查找按钮 jmt11.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ try { if ( "" .equals(text.getText())){ //文本编辑区域为空,提示无法查找 JOptionPane.showMessageDialog( null , "当前编辑区域没有内容,无法查找!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { showSearch(); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //监听查找下一个 jmt12.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ try { if ( "" .equals(text.getText())){ //文本编辑区域为空,提示无法查找 JOptionPane.showMessageDialog( null , "当前编辑区域没有内容,无法查找!" , "提示" , JOptionPane.ERROR_MESSAGE); } else { showSearch(); } } catch (Exception e){ //有操作异常,弹出异常消息 JOptionPane.showMessageDialog( null , "操作错误,请重新操作" , "提示" , JOptionPane.ERROR_MESSAGE); } } }); //监听替换 jmt13.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //首先弹出查找/查找下一个/替换窗口 showReplace(); } }); //监听全选 jmt14.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //首先弹出查找/查找下一个/替换窗口 nbo.chooseAll(text); } }); } //新建路径选择界面 public String showSelectSrc_newFile(){ FileDialog fd= new FileDialog( new Frame(), "新建" ,FileDialog.SAVE); //System.out.println("000"); fd.setVisible( true ); if (fd.getFile()== null ||fd.getDirectory()== null ){ return null ; } else { return fd.getDirectory()+fd.getFile(); } } //另存为路径选择界面 public String showSelectSrc_saveOther(){ FileDialog fd= new FileDialog( new Frame(), "另存为" ,FileDialog.SAVE); //System.out.println("000"); fd.setVisible( true ); if (fd.getFile()== null ||fd.getDirectory()== null ){ return null ; } else { return fd.getDirectory()+fd.getFile(); } } //打开路径选择界面 public String showSelectSrc_openFile(){ FileDialog fd= new FileDialog( new Frame(), "打开" ,FileDialog.LOAD); fd.setVisible( true ); if (fd.getFile()== null ||fd.getDirectory()== null ){ return null ; } else { return fd.getDirectory()+fd.getFile(); } } //确定提示界面 public void showDoSuccess(String doWhat){ JOptionPane.showConfirmDialog( null , "确定" +doWhat+ "?" , doWhat, JOptionPane.YES_NO_OPTION); } //当前未打开文件时的提示界面 public void showNotOpenFileInfo(){ JOptionPane.showMessageDialog( null , "当前未打开文件,请先打开或者创建相关文件再进行其它操作" ); } /* //文件名字输入界面,返回相关文件名字(用于重命名和新建功能) public String showGetName(){ String name = JOptionPane.showInputDialog(null,"请输入文件名字:\n","输入文件名",JOptionPane.PLAIN_MESSAGE); return name; } */ //进行新操作前(新建文件和关闭窗口)询问是否保存当前数据显示界面 public void beforeCreatFile(){ //弹出该窗口的前提是已经打开了相关文件 if (nowFileSrc!= null ){ final JFrame frame_remindSave = new JFrame( "记事本" ); //创建一个垂直box Box remindSaveBox = Box.createVerticalBox(); //文字提示信息 JPanel jpanel_remindLabel = new JPanel(); JLabel jlabel_tiShi = new JLabel( "是否将更改保存到" +nowFileSrc+ "?" ); jpanel_remindLabel.add(jlabel_tiShi); remindSaveBox.add(jpanel_remindLabel); remindSaveBox.add(Box.createVerticalStrut( 5 )); //确定和取消按钮 JPanel jpanel_remindButton = new JPanel(); JButton baoCun_btn = new JButton( "保存" ); JButton fangQi_btn = new JButton( "放弃" ); jpanel_remindButton.add(baoCun_btn); jpanel_remindButton.add(fangQi_btn); remindSaveBox.add(jpanel_remindButton); frame_remindSave.add(remindSaveBox); //设置窗口属性 frame_remindSave.setVisible( true ); frame_remindSave.setSize( 1000 , 200 ); frame_remindSave.setResizable( false ); frame_remindSave.setLocation( 500 , 500 ); frame_remindSave.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); //监听相关按钮 //监听保存按钮 baoCun_btn.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ nbo.doConserve(nowFileSrc,text.getText()); System.out.println( "000" ); frame_remindSave.dispose(); } }); //监听放弃按钮 fangQi_btn.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ System.out.println( "001" ); frame_remindSave.dispose(); } }); } else { //当前没有打开文件则直接退出该方法 return ; } } //查找显示界面 //JFrame frame1; public void showSearch(){ final String [] selectInfo = new String[ 2 ]; //默认状态查找是向下查找,查找方框内信息是空 selectInfo[ 0 ] = "向下" ; selectInfo[ 1 ] = "" ; JFrame frame1 = new JFrame( "查找" ); //创建一个垂直box Box findInfoBox = Box.createVerticalBox(); JPanel jpanel_danXuan = new JPanel(); //文本输入框 JLabel label_fingInfo = new JLabel( "查找内容" ); final JTextField jtext_findInfo = new JTextField( 10 ); JPanel findInfo_panel = new JPanel(); findInfo_panel.add(label_fingInfo); findInfo_panel.add(jtext_findInfo); findInfoBox.add(findInfo_panel); findInfoBox.add(Box.createVerticalStrut( 10 )); //单选按钮 JRadioButton jrb1 = new JRadioButton( "向下" ); JRadioButton jrb2 = new JRadioButton( "向上" ); //默认选中的是向下按钮 jrb1.setSelected( true ); ButtonGroup group = new ButtonGroup(); group.add(jrb1); group.add(jrb2); jpanel_danXuan.add(jrb1); jpanel_danXuan.add(jrb2); findInfoBox.add(jpanel_danXuan); findInfoBox.add(Box.createVerticalStrut( 10 )); //确定和取消按钮 JPanel jpanel_queDingQuXiao = new JPanel(); JButton queDing_btn = new JButton( "确定" ); JButton quXiao_btn = new JButton( "取消" ); jpanel_queDingQuXiao.add(queDing_btn); jpanel_queDingQuXiao.add(quXiao_btn); findInfoBox.add(jpanel_queDingQuXiao); findInfoBox.add(Box.createVerticalStrut( 5 )); //提示信息 JPanel jpanel_tiShi = new JPanel(); final JLabel jlabel_tiShi = new JLabel( "请输入您要查找的信息" ); jlabel_tiShi.setForeground( new Color( 238 , 32 , 32 )); jpanel_tiShi.add(jlabel_tiShi); findInfoBox.add(jpanel_tiShi); frame1.add(findInfoBox); //设置窗口属性 frame1.setVisible( true ); frame1.setSize( 1000 , 200 ); frame1.setResizable( false ); frame1.setLocation( 500 , 500 ); frame1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); //添加按钮事件 //监听单选按钮事件 jrb1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ //将相应的选取的信息赋值给数组selectInfo selectInfo[ 0 ] = "向下" ; } }); jrb2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ //将相应的选取的信息赋值给数组selectInfo selectInfo[ 0 ] = "向上" ; } }); //监听确定、取消按钮事件 queDing_btn.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //确定按钮,执行查找操作 selectInfo[ 1 ] = jtext_findInfo.getText(); int rtn = nbo.doFindString(text,selectInfo); /** *根据返回值,弹出相应的提示信息 */ if (rtn== 0 ){ //如果输入字符串为空,提示输入为空 jlabel_tiShi.setText( "您输入的查找内容为空,请重新输入" ); } else if (rtn== 1 ){ //返回1,查找成功 jlabel_tiShi.setText( "查找成功" ); } else { //找不到内容 jlabel_tiShi.setText(selectInfo[ 0 ]+ "查找" + "找不到该内容" ); } } }); quXiao_btn.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //返回null return ; } }); } //替换窗口 public void showReplace(){ JFrame frame2 = new JFrame( "替换" ); //创建一个垂直box Box replaceBox = Box.createVerticalBox(); //创建查找内容板块 JPanel jpanel_search = new JPanel(); JLabel jlabel_search = new JLabel( "查找内容 " ); final JTextField jtext_search = new JTextField( 15 ); JButton jbutton_search = new JButton( "查找下一个" ); jpanel_search.add(jlabel_search); jpanel_search.add(jtext_search); jpanel_search.add(jbutton_search); replaceBox.add(jpanel_search); replaceBox.add(Box.createVerticalStrut( 10 )); //创建替换内容板块 JPanel jpanel_replace = new JPanel(); JLabel jlabel_replace = new JLabel( " 替换 " ); final JTextField jtext_replace = new JTextField( 15 ); JButton jbutton_replace = new JButton( "替换" ); JButton jbutton_replaceall = new JButton( "全部替换" ); jpanel_replace.add(jlabel_replace); jpanel_replace.add(jtext_replace); jpanel_replace.add(jbutton_replace); jpanel_replace.add(jbutton_replaceall); replaceBox.add(jpanel_replace); //创建取消板块以及信息提示板块 JPanel jpanel_cancel = new JPanel(); JButton jbutton_cancel = new JButton( "取消" ); final JLabel jlabel_tiShi = new JLabel( "显示提示信息" ); jlabel_tiShi.setForeground( new Color( 238 , 32 , 32 )); jpanel_cancel.add(jbutton_cancel); replaceBox.add(jpanel_cancel); replaceBox.add(jlabel_tiShi); frame2.add(replaceBox); //设置窗口属性 frame2.setVisible( true ); frame2.setSize( 1000 , 200 ); frame2.setResizable( false ); frame2.setLocation( 500 , 500 ); frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); //监听按钮事件 //监听查找下一个按钮 //获取查找输入框中的内容 final JTextArea jtext_search1 = text; jbutton_search.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ //由于查找方法传递的参数是字符串数组,所以先创建一个字符串数组用于传递信息 String [] info = new String[ 2 ]; info[ 0 ] = "向下" ; info[ 1 ] = jtext_search.getText(); int rtn = nbo.doFindString(text,info); System.out.println(rtn); //根据返回值相应显示提示信息 if (rtn== 0 ){ //如果输入字符串为空,提示输入为空 jlabel_tiShi.setText( "您输入的查找内容为空,请重新输入" ); } else if (rtn== 1 ){ //返回1,查找成功 jlabel_tiShi.setText( "查找成功,您可以选择替换" ); } else { //找不到内容 jlabel_tiShi.setText( "找不到该内容,该字符可能不存在或者您已将光标移到最后" ); } } }); //监听替换按钮 jbutton_replace.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ int rtn = nbo.doReplace(jtext_search1,jtext_replace.getText()); if (rtn== 0 ){ jlabel_tiShi.setText( "您还没有选中文字,无法执行替换" ); } else { jlabel_tiShi.setText( "替换成功" ); } } }); //监听全部替换按钮 jbutton_replaceall.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ int rtn = nbo.doReplaceAll(text,jtext_search.getText(),jtext_replace.getText()); if (rtn== 0 ){ jlabel_tiShi.setText( "请输入替换内容和查找内容" ); } else if (rtn==- 1 ){ jlabel_tiShi.setText( "找不到内容,请重新输入" ); } else { jlabel_tiShi.setText( "全部替换成功" ); } } }); } /* //添加关闭窗口触发时间 addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { super.windowClosing(e); //弹出询问是否保存修改后的文件 } }); */ } |
以下是BO业务层代码
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 | //记事本 import java.io.*; import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; import javax.swing.*; import java.awt.datatransfer.*; import javax.swing.undo.UndoManager; import java.lang.StringBuffer.*; public class noteBookBO{ /** *定义文件菜单下的业务方法 */ //做到打开文件新建文件将窗口标题改为对应文件路劲,关闭窗口触发时间没有实现 noteBookDA nda = new noteBookDA(); //定义新建方法 public String doCreateFile(String srcFile){ String src = null ; System.out.println( "test doCreatFile" +srcFile); try { File f = nda.creatFile(srcFile); src = f.getAbsolutePath(); System.out.println(src); } catch (IOException ie){ System.out.println(ie); } //获取当前文件的路径并返回 return src; } //定义打开方法 public String doOpen(String srcFile){ String rtn = null ; try { rtn = nda.dataRead(srcFile); } catch (IOException ie){ System.out.println(ie); } return rtn; } //定义保存方法,如果所传递的文件路径所指文件不存在,则对应返回-1,如果保存成功,返回1 public int doConserve(String srcFile,String data){ int rtn; try { nda.dataWrite(srcFile,data); rtn = 1 ; } catch (Exception ie){ rtn = - 1 ; } return rtn; } //定义另存为方法 public void doNewConserve(String srcFile,String data) { try { nda.dataWrite(srcFile,data); } catch (IOException ie){ System.out.println(ie); } } //定义退出方法 public void quit(){ System.exit( 0 ); } /** *定义编辑菜单下的业务方法 */ //定义撤销的方法,返回1表示撤销成功,返回0表示撤销失败 public int doCeXiao(JTextArea text){ System.out.println( "000" ); UndoManager undoManager = new UndoManager(); text.getDocument().addUndoableEditListener(undoManager); if (undoManager.canUndo()){ System.out.println( "001" ); undoManager.undo(); return 1 ; } else { System.out.println( "002" ); return 0 ; } } //定义剪切方法,成功返回1,失败返回-1 public int doJianQie(JTextArea text){ //先copy int rtn_copy = doCopy(text); //后delete int rtn_delete = doDelete(text); if ((rtn_copy) == 1 &&(rtn_delete== 1 )){ return 1 ; } else { return - 1 ; } } //定义复制方法 //获取系统剪贴板,成功返回1,没有选中操作的文本区域,返回-1 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); final Clipboard c1 = clipboard; public int doCopy(JTextArea text){ String temp = text.getSelectedText(); if (temp== null ){ return - 1 ; } else { StringSelection textString = new StringSelection(temp); //Clipboard clipboard = new Clipboard(temp); c1.setContents(textString, null ); return 1 ; } } /*定义粘贴方法,返回1表示操作成功,返回0表示剪切板空白 *返回-1表示粘贴内容是非文本 *出现相应的异常,相应地返回-2和-3 */ public int doPaste(JTextArea text){ // 获取剪切板中的内容 try { Transferable clipT = c1.getContents( null ); String value = null ; if (clipT != null ){ // 检查内容是否是文本类型 if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)){ //获取剪切板中的数据 value = (String)clipT.getTransferData(DataFlavor.stringFlavor); //执行替换操作 int start = text.getSelectionStart(); int end = text.getSelectionEnd(); text.replaceRange(value, start, end); return 1 ; } else { //粘贴内容非文本返回-1 return - 1 ; } } else { //剪切板为空返回0 return 0 ; } } catch (UnsupportedFlavorException ufe){ //捕获到此异常表示操作不支持该数据类型,返回-2 return - 2 ; } catch (IOException ie){ //捕获到此异常表示IO操作有问题,返回-3 return - 3 ; } } //定义删除方法,成功返回1,如果没有选定操作的文本区域,返回-1 public int doDelete(JTextArea text){ int start = text.getSelectionStart(); int end = text.getSelectionEnd(); // 删除被选取的文本 if (start==end){ return - 1 ; } else { text.replaceRange( "" , start, end); return 1 ; } } //定义查找方法,查找成功返回1,查找内容为空返回0,找不到该字符串返回-1 int start ; int end ; public int doFindString(JTextArea text,String [] searchValue ){ if ( "" .equals(searchValue[ 1 ])){ //为空,返回0 return 0 ; } else { //判断是向下还是向上搜索,如果是向上,将字符串反转 //获取当前光标的位置 int caretPosition; if (searchValue[ 0 ].equals( "向上" )){ if ((text.getSelectedText()!= null )&&(text.getSelectedText().equals(searchValue[ 1 ]))){ //如果文本选中了要查找的值,说明已经进行过查找操作,此时光标位置应该移动到start处 caretPosition = start; } else { //如果还没有进行查找操作,应该获取当前光标的位置 caretPosition = text.getCaretPosition(); } //先将字符串反转 String text1 = text.getText(); StringBuffer StringBuffer1 = new StringBuffer(text1); String textReverse = StringBuffer1.reverse().toString(); //同时当前光标对应的位置也应该对应改变 caretPosition = text1.length() - caretPosition; //查找前,查找的信息也需要反转 StringBuffer StringBuffer2 = new StringBuffer(searchValue[ 1 ]); String infoReverse = StringBuffer2.reverse().toString(); /** *执行查找操作,在操作中,由于字符串被反转 *所以将字符串长度减去得出来的索引对应的是原字符串 *的end的值 */ end = textReverse.length() - textReverse.indexOf(infoReverse, caretPosition); start = end - infoReverse.length(); //选中文本 if (end>textReverse.length()){ //end值大于文本长度,表示该文本不存在 return - 1 ; } else { //查找成功,选中文本 text.select(start,end); return 1 ; } } else { String getInfo = text.getText(); caretPosition = text.getCaretPosition(); //查找对应的字符 start = getInfo.indexOf(searchValue[ 1 ], caretPosition); end = getInfo.indexOf(searchValue[ 1 ], caretPosition) + searchValue[ 1 ].length(); //选中文本 if (start==- 1 ){ //文本不存在 return - 1 ; } else { //查找成功 text.select(start,end); return 1 ; } } } } //定义替换,返回0表示没有选中文本,返回1表示替换成功 public int doReplace(JTextArea text,String replaceInfo){ if (text.getSelectedText()== null ){ return 0 ; } else { //执行替换操作 text.replaceRange(replaceInfo, start, end); return 1 ; } } //定义全部替换方法,输入为空,返回0,成功替换全部返回1,找不到相应的字符返回-1 public int doReplaceAll(JTextArea text,String regex,String replacement){ String textInfo = text.getText(); if ( "" .equals(textInfo)|| "" .equals(regex)){ return 0 ; } else { if (textInfo.indexOf(regex)!=- 1 ){ //如果替换的字符串存在,则执行替换操作 String textDoneReplace = textInfo.replaceAll(regex,replacement); //替换完之后将数据写进输入区域 text.setText(textDoneReplace); return 1 ; } else { return - 1 ; } } } //定义全选 public void chooseAll(JTextArea text){ text.selectAll(); } } |
以下是DA数据层代码
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 | //数据层面 import java .io.*; public class noteBookDA{ //主方法,测试用 public static void main(String [] args){ noteBookDA test = new noteBookDA(); // try{ // //String st = test.dataRead("text.txt"); // //System.out.println(st); // //String st = new String("asldfkjlsdkfj"); // //test.dataWrite("text.txt",st); // //test.creatFile("D:/HaiGe/mycode/L2-W4/代码练习/test","name1"); // }catch(IOException ie){ // System.out.println("IOException"); // } } //定义数据操作方法 //在指定目录创建新文件 public File creatFile(String src) throws IOException{ String fileSrc = src; File f = new File(fileSrc); if (f.exists()){ return null ; } else { try { // 创建新文件 f.createNewFile(); } catch (IOException e){ System.out.println( "创建新文件时出现了错误。。。" ); e.printStackTrace(); } return f; } } //定义数据读取方法,返回读取到的数据的字符串 public String dataRead(String srcFile) throws IOException{ String dataString; File f = new File(srcFile); FileReader fr = new FileReader(f); char [] cbuf = new char [( int )f.length()]; fr.read(cbuf); dataString = new String(cbuf); fr.close(); return dataString; } //定义数据写入方法 public void dataWrite(String srcFile,String writeData) throws IOException{ String dataString; File f = new File(srcFile); FileWriter fw = new FileWriter(f); fw.write(writeData); fw.close(); } //定义数据替换 public void dataReplace(String srcFile,String oldData,String newData) throws IOException{ //Fiel f= new File(srcFile); } //数据查找 public void dataSearch(String allData,String tarData){ } //定义数据删除 public void dataDelete(String srcFile,String deleteData){ //String da = dataRead(srcFile); } } |
该记事本只是实现了简单的输入编辑保存等基本操作,主要用于巩固IO和UI的知识,不足之处,恳请各位指出!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步