源文件、文档说明、可运行的jar文件下载地址http://download.csdn.net/source/1852333、
1/** 2 * NoteBook 3 * 4 * @versionliujunguang 5 * @version 1.00 09/10/25 6 */ 7public class NoteBook { 8 public static void main(String[] args) { 9 // Create application frame. 10 NoteBookFrame frame = new NoteBookFrame(); 11 // Show frame 12 frame.setVisible(true); 13 } 14} 15 16 17 18 19import java.awt.*; 20/** 21 * NoteBook-OpenClass. 22 * 23 * @liujunguang 24 * @version 1.00 09/10/28 25 */ 26 27class SeeClass extends Frame 28 { 29 NoteBookFrame notebookframe = null; 30 SeeClass(NoteBookFrame p) 31 { 32 notebookframe = p; 33 } 34 public void Modo() 35 { 36 System.out.println("状态栏"); 37 } 38} 39 40 41 42//编辑类中的查找选项的实现 43import javax.swing.*; 44import java.awt.*; 45import java.awt.event.*; 46import java.lang.*;//String 类 47import javax.swing.JOptionPane; 48class SearchFrame extends JFrame implements ActionListener,ItemListener//单选按钮事件接口 49 { 50 JLabel label1,label2,label3; 51 JButton but1,but2,but3,but4; 52 JTextField textfield,text; 53 JRadioButton radiobutton1,radiobutton2; 54 ButtonGroup group1; 55 JCheckBox box1; 56 NoteBookFrame notebookframe = null; 57 String sub1="",sub2=""; 58 int n=-1; 59 boolean isup = false;//判断是否是最后一个找到的内容时用到 60 boolean direction = true ;//判断查找方向 向下为 true 61 SearchFrame(NoteBookFrame p) 62 { 63 notebookframe = p; 64 label1 = new JLabel("查找内容(N):"); 65 label2 = new JLabel("方向"); 66 label3 = new JLabel("替换为(P)"); 67 68 but1 = new JButton("查找下一个(F)"); 69 but2 = new JButton(" 取 消 "); 70 but3 = new JButton(" 替 换 "); 71 but4 = new JButton(" 转 到 "); 72 textfield = new JTextField(15); 73 text = new JTextField(15); 74 75 radiobutton1 = new JRadioButton("向下(U)",true); 76 radiobutton2 = new JRadioButton("向上(D)"); 77 78 group1 = new ButtonGroup(); 79 //创建组件 80 box1 = new JCheckBox("区分大小写(C)"); 81 Container con = getContentPane(); 82 con.setLayout(null);//设置布局为空 83 group1.add(radiobutton1); 84 group1.add(radiobutton2);//将单选按钮加到按钮组中 85 86 con.add(label1);//查找 87 label1.setBounds(10,10,85,25); 88 89 con.add(label3);//替换为 90 label3.setBounds(10,45,85,25); 91 92 con.add(textfield);//文本 93 textfield.setBounds(90,10,180,25); 94 95 con.add(text);//文本 96 text.setBounds(90,45,180,25); 97 98 con.add(but1);//查找下一个 99 but1.setBounds(280,10,120,25); 100 101 con.add(label2);//方向 102 label2.setBounds(135,80,50,25); 103 104 con.add(box1); 105 box1.setBounds(10,115,120,25); 106 107 con.add(radiobutton1);//向下 108 radiobutton1.setBounds(130,115,70,25); 109 110 con.add(radiobutton2);//向上 111 radiobutton2.setBounds(200,115,70,25); 112 113 con.add(but2);//取消 114 but2.setBounds(280,115,120,25); 115 116 con.add(but3);//替换 117 but3.setBounds(280,45,120,25); 118 119 con.add(but4);//转到 120 but4.setBounds(280,80,120,25); 121 122 but1.addActionListener(this); 123 but2.addActionListener(this); 124 but3.addActionListener(this); 125 but4.addActionListener(this); 126 box1.addItemListener(this); 127 textfield.addActionListener(this); 128 text.addActionListener(this); 129 radiobutton1.addItemListener(this); 130 radiobutton2.addItemListener(this); 131 //注册监听 132 133 setBounds(500,300,420,180); 134 setVisible(false); 135 setTitle("查找"); 136 137 validate(); 138 139 } 140 public void actionPerformed(ActionEvent e) 141 { 142 sub1 = notebookframe.textarea.getText();//得到文本区中的文本 143 sub2 = textfield.getText();//得到文本框中的文本 144 if(!isup) //如果不区分大小写(默认不区分大小写) 145 { 146 sub1 = sub1.toLowerCase();//将sub1转换成小写 147 sub2 = sub2.toLowerCase();//将sub2转换成小写 148 } 149 if(!direction) n = sub1.lastIndexOf(sub2);//在文本区中查找文本框中的内容 150 else n = sub1.indexOf(sub2);//在文本区中查找文本框中的内容 151 if(e.getSource() == but1||e.getSource() == textfield)//查找下一个 152 { 153 154 if(n!=-1) 155 { 156 notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window 157 notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容 158 this.setVisible(false); 159 } 160 else 161 { 162 JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE); 163 } 164 } 165 if(e.getSource() == but2)//取消 166 { 167 this.setVisible(false); 168 } 169 if(e.getSource() == but4)//转到 170 { 171 int i=0,j=1; 172 char ch[]; 173 try{ 174 int raw = Integer.parseInt(textfield.getText());//得到文本框中的文本 175 String s = notebookframe.textarea.getText(); 176 ch = new char[s.length()]; 177 s.getChars(0,s.length()-1,ch,0); 178 179 while(j<raw) 180 { 181 i++; 182 if(ch[i] == '/n') 183 {j++;} 184 if(i == s.length())break; 185 } 186 if(raw == 1) //转到第一行 187 notebookframe.textarea.setCaretPosition(0); 188 189 else 190 notebookframe.textarea.setCaretPosition(i+1);//转到指定行 191 this.setVisible(false); 192 } 193 catch(Exception a){ 194 JOptionPane.showMessageDialog(this,"你输入的位置不对无法到达!","记事本",JOptionPane.WARNING_MESSAGE); 195 } 196 } 197 198 if(e.getSource() == but3)//替换 199 { 200 if(n != -1) 201 { 202 String sub3 = text.getText(); 203 notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容 204 notebookframe.textarea.replaceRange(sub3,n,n+sub2.length());//替换选中位置的文本 205 206 sub1 = notebookframe.textarea.getText();//得到文本区中的文本 207 sub2 = textfield.getText();//得到文本框中的文本 208 if(!isup) //如果不区分大小写(默认不区分大小写) 209 { 210 sub1 = sub1.toLowerCase();//将sub1转换成小写 211 sub2 = sub2.toLowerCase();//将sub2转换成小写 212 } 213 if(!direction) n = sub1.lastIndexOf(sub2);//在文本区中查找最后出现的文本框中的内容 214 else n = sub1.indexOf(sub2);//在文本区中查找最先出现的文本框中的内容 215 notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window 216 if(n!=-1 ) 217 { 218 notebookframe.textarea.select(n,n+sub2.length());//选中查找内容 219 } 220 else 221 JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE); 222 223 } 224 else 225 { 226 JOptionPane.showMessageDialog(this,"所指定的文本没有找到无法替换!","记事本",JOptionPane.WARNING_MESSAGE); 227 } 228 } 229 230 } 231 public void nextShear()//查找下一个 菜单项对应的 232 { 233 234 235 if(n!=-1) 236 { 237 sub1 = notebookframe.textarea.getText().substring(n+sub2.length());//得到文本区的子串 238 if(!isup) //如果不区分大小写(默认不区分大小写) 239 { 240 241 sub1 = sub1.toLowerCase();//将sub1转换成小写 242 sub2 = sub2.toLowerCase();//将sub2转换成小写 243 } 244 245 if(sub1.indexOf(sub2)!=-1) 246 { 247 248 n = n+sub2.length()+sub1.indexOf(sub2);//得到查找内容在文本区中的位置 249 notebookframe.textarea.select(n,n+sub2.length());//选中查找内容 250 } 251 252 253 else 254 JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE); 255 } 256 else 257 JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE); 258 259 260 261 262 } 263 public void itemStateChanged(ItemEvent ee) 264 { 265 if(ee.getSource() == box1) 266 { 267 if(box1.isSelected()) 268 { 269 isup = true ; 270 } 271 else 272 isup = false ; 273 } 274 if(ee.getSource() == radiobutton1) 275 { 276 if(radiobutton1.isSelected()) 277 { 278 direction = true ; 279 } 280 } 281 if(ee.getSource() == radiobutton2) 282 { 283 if(radiobutton2.isSelected()) 284 { 285 direction = false; 286 } 287 } 288 } 289 } 290 291 292 293 294import java.awt.*; 295import java.io.*; 296import java.awt.event.*; 297import javax.swing.JOptionPane; 298/** 299 * NoteBook-OpenClass. 300 * @liujunguang 301 * @version 1.00 09/10/24 302 */ 303 class OpenClass extends Frame 304 { 305 NoteBookFrame notebookframe = null; 306 FileDialog filedialog_open,filedialog_save; 307 File file1,file2; //文件字节流对象 308 FileReader file_reader;//文件字符流对象 309 FileWriter tofile; 310 BufferedReader in ;//文件行读取 写入对象 311 BufferedWriter out; 312 String s; 313 boolean issave = false;//用于判断是否是第一次保存 314 String filesave = null,savename = null; //用于存放文件地址 和文件名 315 String fileopen = null,filename = null;//用于存放打开文件地址 和文件名 316 PrintJob p=null;//声明一个要打印的对象 317 Graphics g=null;//要打印的对象 318 OpenClass(NoteBookFrame p ) 319 { 320 notebookframe = p; 321 filedialog_open = new FileDialog(notebookframe,"打开文件对话框",FileDialog.LOAD); 322 filedialog_save = new FileDialog(notebookframe,"保存文件对话框",FileDialog.SAVE); 323 filedialog_open.setVisible(false); 324 filedialog_save.setVisible(false); 325 //notebookframe = new NoteBookFrame(); 326 filedialog_open.addWindowListener(new WindowAdapter() 327 { public void windowClosing(WindowEvent e) 328 { filedialog_open.setVisible(false); } 329 } 330 );//打开文件对话框适配器 331 filedialog_save.addWindowListener(new WindowAdapter() 332 { public void windowClosing(WindowEvent e) 333 { filedialog_save.setVisible(false); } 334 } 335 );//保存文件对话框适配器 336 } 337 public void New() 338 { 339 if(!notebookframe.textarea.getText().equals("")) 340 { 341 int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION); 342 if(n == JOptionPane.YES_OPTION) 343 { 344 Save(); 345 } 346 } 347 notebookframe.textarea.setText(""); 348 } 349 public void Open() 350 { 351 352 if(!notebookframe.textarea.getText().equals("")) 353 { 354 int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION); 355 if(n == JOptionPane.YES_OPTION) 356 { 357 Save(); 358 } 359 if(n == JOptionPane.NO_OPTION) 360 { 361 notebookframe.textarea.setText(""); 362 } 363 } 364 filedialog_open.setVisible(true); 365 fileopen = filedialog_open.getDirectory();//返回文件对话框中显示的文件所属的目录 366 filename = filedialog_open.getFile();//返回当前文件对话框中显示的文件名的字符串表示 如果不存在就返回NULL 367 if(filename!=null)// 判断打开的文件是否存在 368 { 369 try 370 { 371 file1 = new File(fileopen,filename); 372 if(file1.isFile())//判断文件是否是正常的文件而不是目录 373 { 374 375 notebookframe.textarea.setText(""); 376 file_reader = new FileReader(file1); 377 in = new BufferedReader(file_reader);//每次读取一行 378 while((s = in.readLine())!=null) 379 notebookframe.textarea.append(s+'/n'); 380 in.close(); 381 file_reader.close(); 382 383 } 384 } 385 catch (IOException e2){ System.out.println(filename); } 386 } 387 } 388 public void Save() 389 { 390 if(!issave&&filename == null) 391 { 392 filedialog_save.setVisible(true); 393 filesave = filedialog_save.getDirectory(); 394 savename = filedialog_save.getFile()+".txt"; 395 issave = true; 396 } 397 398 if(filename != null||!savename.equals(null+".txt")) 399 { 400 401 try{ 402 if(filename!= null){filesave = fileopen; savename = filename;} 403 file2 = new File(filesave,savename); 404 tofile = new FileWriter(file2); 405 out = new BufferedWriter(tofile); 406 out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length()); 407 out.close(); 408 tofile.close(); 409 410 } 411 catch (IOException e2){ } 412 } 413 } 414 public void AsSave() 415 { 416 int n=-2; 417 filedialog_save.setVisible(true); 418 String filesave = filedialog_save.getDirectory(); 419 String savename = filedialog_save.getFile()+".txt"; 420 421 if(!savename.equals(null+".txt")) 422 { 423 try{ 424 file2 = new File(filesave,savename); 425 tofile = new FileWriter(file2); 426 out = new BufferedWriter(tofile); 427 out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length()); 428 out.close(); 429 tofile.close(); 430 431 } 432 catch (IOException e2){ } 433 } 434 } 435 public void SetUp() 436 { 437 JOptionPane.showMessageDialog(this,"在打印的时候用到!","警告对话框",JOptionPane.WARNING_MESSAGE); 438 } 439 public void Print() 440 { 441 try{ 442 p = getToolkit().getPrintJob(notebookframe,"ok",null);//创建一个Printfjob 对象 p 443 g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 444 g.translate(120,200);//改变组建的位置 445 notebookframe.textarea.printAll(g); 446 p.end();//释放对象 g 447 } 448 catch(Exception a){ 449 450 } 451 } 452 } 453 454 455 456 457import java.awt.*; 458import java.awt.event.*; 459/** 460 * NoteBook-BookFrame. 461 * 462 * @liujunguang 463 * @version 1.00 09/10/24 464 */ 465 class NoteBookFrame extends Frame implements ActionListener , KeyListener 466 { 467 468 TextArea textarea ; 469 MenuBar menuBar; 470 Menu Open,Edit,Formt,See,Help; 471 MenuItem NewFile,OpenFile,Save,AsSave,SetUp,Print,Exit; 472 MenuItem Repeal,Cut,Copy,Stick,Delete,Lookup,Lookupnext,Exchange,Goto,All,Timedate; 473 MenuItem Rand,FontSize,Modo,MainHelp,AboutBook; 474 OpenClass openclass ; 475 EditClass editclass ; 476 FormateClass formateclass; 477 SeeClass seeclass; 478 HelpClass helpclass; 479 480 public NoteBookFrame() { 481 482 textarea = new TextArea(40,500); 483 484 openclass = new OpenClass(this); 485 editclass = new EditClass(this); 486 formateclass = new FormateClass(this); 487 seeclass = new SeeClass(this); 488 helpclass = new HelpClass(this); 489 490 menuBar = new MenuBar(); 491 //创建菜单条 492 Open = new Menu("打开(F)"); 493 Edit = new Menu("编辑(E)"); 494 Formt = new Menu("格式(O)"); 495 See = new Menu("查看(V)"); 496 Help = new Menu("帮助(H)"); 497 //创建菜单 498 NewFile = new MenuItem("新建"); 499 OpenFile = new MenuItem("打开"); 500 Save = new MenuItem("保存"); 501 AsSave = new MenuItem("另存为(A)... "); 502 SetUp = new MenuItem("页面设置(U) "); 503 Print = new MenuItem("打印(P) ..."); 504 Exit = new MenuItem("退出(X)"); 505 //开始菜单中的各菜单项 506 507 508 Repeal = new MenuItem("撤销(U)"); 509 Cut= new MenuItem("剪切(T)"); 510 Copy= new MenuItem("复制(C)"); 511 Stick= new MenuItem("粘贴(P)"); 512 Delete= new MenuItem("删除(L)Del"); 513 Lookup= new MenuItem("查找(F)"); 514 Lookupnext= new MenuItem("查找下一个(N) F3"); 515 Exchange= new MenuItem("替换(R)"); 516 Goto= new MenuItem("转到(G)"); 517 All= new MenuItem("全选(A)"); 518 Timedate= new MenuItem("时间/日期(D) F5"); 519 //编辑菜单中的各菜单项 520 521 Rand = new MenuItem("自动换行(W) "); 522 FontSize = new MenuItem("字体(F)... "); 523 //格式菜单中的各菜单项 524 525 Modo = new MenuItem("状态栏(S)"); 526 //查看菜单中的各菜单项 527 528 MainHelp = new MenuItem("帮助主题(H) "); 529 AboutBook = new MenuItem("关于记事本(A) "); 530 //帮助菜单中的各菜单项 531 532 Open.add(NewFile); 533 Open.add(OpenFile); 534 Open.add(Save); 535 Open.add(AsSave); 536 Open.addSeparator(); 537 Open.add(SetUp); 538 Open.add(Print); 539 Open.addSeparator();//添加分割线 540 Open.add(Exit); 541 //在打开菜单中加菜单项 542 543 Edit.add(Repeal); 544 Edit.addSeparator(); 545 Edit.add(Cut); 546 Edit.add(Copy); 547 Edit.add(Stick); 548 Edit.add(Delete); 549 Edit.addSeparator(); 550 Edit.add(Lookup); 551 Edit.add(Lookupnext); 552 Edit.add(Exchange); 553 Edit.add(Goto); 554 Edit.addSeparator(); 555 Edit.add(All); 556 Edit.add(Timedate); 557 //在编辑菜单中添加菜单项 558 559 Formt.add(Rand); 560 Formt.add(FontSize); 561 //格式菜单中添加菜单项 562 563 See.add(Modo); 564 //查看菜单中添加菜单项 565 566 Help.add(MainHelp); 567 Help.addSeparator(); 568 Help.add(AboutBook); 569 //帮助菜单中添加菜单项 570 571 menuBar.add(Open); 572 menuBar.add(Edit); 573 menuBar.add(Formt); 574 menuBar.add(See); 575 menuBar.add(Help); 576 //菜单条中添加菜单 577 578 NewFile.setShortcut(new MenuShortcut(KeyEvent.VK_N)); 579 OpenFile.setShortcut(new MenuShortcut(KeyEvent.VK_O)); 580 Save.setShortcut(new MenuShortcut(KeyEvent.VK_S)); 581 Print.setShortcut(new MenuShortcut(KeyEvent.VK_P)); 582 Repeal.setShortcut(new MenuShortcut(KeyEvent.VK_Z)); 583 Cut.setShortcut(new MenuShortcut(KeyEvent.VK_X)); 584 Copy.setShortcut(new MenuShortcut(KeyEvent.VK_C)); 585 Stick.setShortcut(new MenuShortcut(KeyEvent.VK_V)); 586 Lookup.setShortcut(new MenuShortcut(KeyEvent.VK_F)); 587 Exchange.setShortcut(new MenuShortcut(KeyEvent.VK_H)); 588 Goto.setShortcut(new MenuShortcut(KeyEvent.VK_G)); 589 All.setShortcut(new MenuShortcut(KeyEvent.VK_A)); 590 //添加菜单项中的各项的快捷键 591 592 setTitle("记事本"); 593 setMenuBar(menuBar);//窗口中添加菜单条 594 add(textarea); 595 setBounds(500,150,400,400); 596 597 textarea.addKeyListener(this); 598 //注册文本域的键盘事件 599 600 NewFile.addActionListener(this); 601 OpenFile.addActionListener(this); 602 Save.addActionListener(this); 603 AsSave.addActionListener(this); 604 SetUp.addActionListener(this); 605 Print.addActionListener(this); 606 //注册监听打开菜单中的各项 607 608 Repeal.addActionListener(this); 609 Cut.addActionListener(this); 610 Copy.addActionListener(this); 611 Stick.addActionListener(this); 612 Delete.addActionListener(this); 613 Lookup.addActionListener(this); 614 Lookupnext.addActionListener(this); 615 Exchange.addActionListener(this); 616 Goto.addActionListener(this); 617 All.addActionListener(this); 618 Timedate.addActionListener(this); 619 //注册监听编辑菜单中的各项 620 621 Rand.addActionListener(this); 622 FontSize.addActionListener(this); 623 //注册监听格式菜单中的各项 624 625 Modo.addActionListener(this); 626 //注册监听查看菜单中的各项 627 628 MainHelp.addActionListener(this); 629 AboutBook.addActionListener(this); 630 //注册监听帮助菜单中的各项 631 632 // Add window listener. 633 this.addWindowListener 634 ( 635 new WindowAdapter() { 636 public void windowClosing(WindowEvent e) { 637 NoteBookFrame.this.windowClosed(); 638 } 639 } 640 ); 641 642 // Add action listener.for the menu button 643 Exit.addActionListener 644 ( 645 new ActionListener() { 646 public void actionPerformed(ActionEvent e) { 647 NoteBookFrame.this.windowClosed(); 648 } 649 } 650 ); 651 } 652 653 654 /** 655 * Shutdown procedure when run as an application. 656 */ 657 protected void windowClosed() { 658 659 // TODO: Check if it is safe to close the application 660 661 // Exit application. 662 System.exit(0); 663 } 664 public void actionPerformed(ActionEvent e) 665 { 666 if(e.getSource() == NewFile){openclass.New();} 667 if(e.getSource() == OpenFile){openclass.Open();} 668 if(e.getSource() == Save){openclass.Save();} 669 if(e.getSource() == AsSave){openclass.AsSave();} 670 if(e.getSource() == SetUp){openclass.SetUp();} 671 if(e.getSource() == Print){openclass.Print();} 672 if(e.getSource() == Exit){System.out.println("退出");} 673 //打开菜单事件的实现 674 675 if(e.getSource() == Repeal){editclass.Repeal();} 676 if(e.getSource() == Cut){editclass.Cut();} 677 if(e.getSource() == Copy){editclass.Copy();} 678 if(e.getSource() == Stick){editclass.Stick();} 679 if(e.getSource() == Delete){editclass.Delete();} 680 if(e.getSource() == Lookup){editclass.LookUp();} 681 if(e.getSource() == Lookupnext){editclass.LookUpNext();} 682 if(e.getSource() == Exchange){editclass.Exchange();} 683 if(e.getSource() == Goto){editclass.Goto();} 684 if(e.getSource() == All){editclass.All();} 685 if(e.getSource() == Timedate){editclass.TimeDate();} 686 //编辑菜单的实现 687 688 if(e.getSource() == Rand){formateclass.Rand();} 689 if(e.getSource() == FontSize){formateclass.FontSize();} 690 //格式菜单的实现 691 692 if(e.getSource() ==Modo ){seeclass.Modo();} 693 //查看菜单的实现 694 695 if(e.getSource() == MainHelp){helpclass.MainHeip();} 696 if(e.getSource() == AboutBook){helpclass.AboutBook();} 697 //帮助菜单的实现 698 699 } 700 public void keyPressed(KeyEvent key)//处理按下某键时 701 { 702 // 文本区的键盘事件的实现 703 } 704 public void keyTyped(KeyEvent key){}//处理按下后又恢复的事件 705 public void keyReleased(KeyEvent key){} //恢复时要掉用的事件 706 707} 708 709 710 711 712import java.awt.*; 713import javax.swing.JOptionPane;//警告对话框 714/** 715 * NoteBook-OpenClass. 716 * 717 * @liujunguang 718 * @version 1.00 09/10/28 719 */ 720 721class HelpClass extends Frame 722 { 723 NoteBookFrame notebookframe = null; 724 HelpClass(NoteBookFrame p) 725 { 726 notebookframe = p; 727 } 728 public void MainHeip() 729 { 730 JOptionPane.showMessageDialog(this,"记事本帮助文档!","记事本",JOptionPane.WARNING_MESSAGE); 731 } 732 public void AboutBook() 733 { 734 JOptionPane.showMessageDialog(notebookframe,"记事本"+"/n"+" 版本: 1.0.0"+"/n" 735 +" 作者: 米林"+"/n" 736 +" 时间: 2009/11/8","记事本",JOptionPane.WARNING_MESSAGE); 737 } 738 739 } 740 741 742 743import java.awt.*; 744 745/** 746 * NoteBook-OpenClass. 747 * 748 * @liujunguang 749 * @version 1.00 09/10/28 750 */ 751 752class FormateClass extends Frame 753 { 754 NoteBookFrame notebookframe = null; 755 FontFrame font =null ;// 定义字体类的对象 756 Toolkit tool = null; 757 Dimension dim = null; 758 boolean ran = true; 759 int n ; 760 FormateClass(NoteBookFrame p) 761 { 762 notebookframe = p; 763 tool = getToolkit(); 764 dim = tool.getScreenSize(); 765 font = new FontFrame(notebookframe); 766 } 767 public void Rand() 768 { 769 770 if(ran) 771 { 772 notebookframe.textarea.setColumns(1);//得到文本区的列数 773 System.out.println(notebookframe.getWidth()); 774 notebookframe.Rand.setLabel("√自动换行"); 775 ran = false; 776 } 777 //notebookframe.textarea.getRows();// 得到文本区的行数 778 //notebookframe.setBounds(0,0,dim.width,dim.height); 779 780 else 781 { 782 notebookframe.textarea.setColumns(4); 783 System.out.println(dim.width); 784 ran = true; 785 notebookframe.Rand.setLabel("自动换行"); 786 } 787 } 788 public void FontSize() 789 { 790 791 font.setVisible(true); 792 } 793 794 } 795 796 797 798 799import java.awt.*; 800import java.awt.event.*; 801import javax.swing.*; 802import javax.swing.JOptionPane; 803/** 804 * NoteBook-OpenClass. 805 * 806 * @liujunguang 807 * @version 1.00 09/10/28 808 */ 809 810 class FontFrame extends JFrame implements ItemListener,ActionListener 811 { 812 813 JLabel label1,label2,label3,label4; 814 JButton but1,but2;//按钮 815 JTextField text1,text2,text3,text4; 816 List list1,list2,list3;//滚动列表 817 String fontname ;//字体名称 818 int fontstytle = Font.PLAIN;//字体式样 819 int fontsize = 12;//字体的大小 820 NoteBookFrame notebookframe = null; 821 FontFrame(NoteBookFrame p) 822 { 823 notebookframe = p ; 824 825 label1 = new JLabel("字体(F):"); 826 label2 = new JLabel("字形(Y):"); 827 label3 = new JLabel("大小(S):"); 828 label4 = new JLabel("示例:"); 829 830 but1 = new JButton("确 定"); 831 but2 = new JButton("取 消"); 832 833 text1 = new JTextField("Arial"); 834 text2 = new JTextField("常规"); 835 text3 = new JTextField("12"); 836 text4 = new JTextField("AaBbCc"); 837 text4.setHorizontalAlignment(JTextField.CENTER); 838 839 840 list1 = new List(7,false);//可现 7 个,不可多选 841 list2 = new List(7,false); 842 list3 = new List(7,false); 843 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//计算机上字体可用的名称 844 String fontName[] = ge.getAvailableFontFamilyNames(); 845 for(int i = 0 ;i<fontName.length;i++) 846 { 847 list1.add(fontName[i]); 848 } 849 list2.add("常规"); 850 list2.add("粗体"); 851 list2.add("斜体"); 852 list2.add("粗斜体"); 853 854 String fontSize[]={"8","9","10","11","12","13","14","16","18","20","22","24","26","28","36","48","72","初号", 855 "小初","一号","小一","二号","小二","三号","小三","四号","小四","五号","小五","六号","小六","七号","八号"}; 856 for(int i = 0 ;i<fontSize.length;i++) 857 { 858 list3.add(fontSize[i]); 859 } 860 list1.addItemListener(this); 861 list2.addItemListener(this); 862 list3.addItemListener(this); 863 864 Container con = getContentPane(); 865 con.setLayout(null);//设置布局为空 866 867 868 con.add(label1);//字体 869 label1.setBounds(20,10,60,20); 870 con.add(label2);//字形 871 label2.setBounds(180,10,60,20); 872 con.add(label3);//大小 873 label3.setBounds(300,10,60,20); 874 con.add(label4);//示例 875 label4.setBounds(180,250,60,20); 876 //标签的设定 877 con.add(but1); //确定 878 but1.addActionListener(this); 879 but1.setBounds(400,35,70,20); 880 con.add(but2);//取消 881 but2.addActionListener(this); 882 but2.setBounds(400,65,70,20); 883 //按钮的设定 884 con.add(text1); //字体 885 text1.setBounds(20,35,150,20); 886 con.add(text2); //字形 887 text2.setBounds(180,35,110,20); 888 con.add(text3);//大小 889 text3.setBounds(300,35,80,20); 890 con.add(text4);//示例 891 text4.setBounds(180,275,200,60); 892 text4.setEditable(false); 893 //文本框的设定 894 con.add(list1); 895 896 list1.setBounds(20,60,150,180); 897 con.add(list2); 898 list2.setBounds(180,60,110,180); 899 con.add(list3); 900 list3.setBounds(300,60,80,180); 901 //文本域的设定 902 this.setVisible(false); 903 setBounds(100,100,500,400); 904 setTitle("字体"); 905 validate(); 906 907 } 908 909 public void itemStateChanged(ItemEvent e) 910 { 911 912 if(e.getSource()==list1) 913 { 914 fontname = list1.getSelectedItem(); 915 text1.setText(fontname); 916 Font f = new Font(fontname,fontstytle,fontsize); 917 text4.setFont(f); 918 } 919 if(e.getSource()==list2) 920 { 921 int index = list2.getSelectedIndex(); 922 switch(index) 923 { 924 case 0: 925 fontstytle = Font.PLAIN;break; 926 case 1: 927 fontstytle = Font.BOLD; break; 928 case 2: 929 fontstytle = Font.ITALIC;break; 930 case 3: 931 fontstytle = Font.BOLD + Font.ITALIC; break; 932 } 933 text2.setText(list2.getSelectedItem()); 934 Font f = new Font(fontname,fontstytle,fontsize); 935 text4.setFont(f); 936 } 937 if(e.getSource()==list3) 938 { 939 940 int index = list3.getSelectedIndex(); 941 if(index<=16) 942 fontsize = Integer.parseInt(list3.getSelectedItem()); 943 switch(index) 944 { 945 case 17: fontsize = 100;break; 946 case 18: fontsize = 90;break; 947 case 19: fontsize = 80;break; 948 case 20: fontsize = 60;break; 949 case 21: fontsize = 50;break; 950 case 22: fontsize = 40;break; 951 case 23: fontsize = 30;break; 952 case 24: fontsize = 20;break; 953 case 25: fontsize = 18;break; 954 case 26: fontsize = 16;break; 955 case 27: fontsize = 12;break; 956 case 28: fontsize = 11;break; 957 case 29: fontsize = 9;break; 958 case 30: fontsize = 8;break; 959 case 31: fontsize = 7; 960 961 } 962 963 text3.setText(list3.getSelectedItem()); 964 Font f = new Font(fontname,fontstytle,fontsize); 965 text4.setFont(f); 966 } 967 968 } 969 public void actionPerformed(ActionEvent ee) 970 { 971 if(ee.getSource()==but1) 972 { 973 Font f = new Font(fontname,fontstytle,fontsize); 974 notebookframe.textarea.setFont(f); 975 this.setVisible(false); 976 } 977 if(ee.getSource()==but2) 978 { 979 this.setVisible(false); 980 } 981 } 982 983 } 984 985 986 987 988 import java.awt.*; 989 import java.io.*; 990 import java.util.*; 991 import java.awt.datatransfer.*;//剪切板的类 992 import java.text.SimpleDateFormat;//时间格式 993 import javax.swing.JOptionPane;//警告对话框 994 /** 995 * NoteBook-OpenClass. 996 * 997 * @liujunguang 998 * @version 1.00 09/10/28 999 */ 1000 1001 class EditClass extends Frame 1002 { 1003 NoteBookFrame notebookframe = null; 1004 Date nowdate;//定义一个时间对象 1005 Clipboard clipboard = null;//定义一个剪切板对象 1006 SimpleDateFormat dateformat; 1007 SearchFrame search=null; 1008 EditClass(NoteBookFrame p) 1009 { 1010 notebookframe = p; 1011 nowdate = new Date(); 1012 search= new SearchFrame(notebookframe); 1013 clipboard = getToolkit().getSystemClipboard();//获得系统的剪切板 1014 dateformat = new SimpleDateFormat("'本地时间:' yyyy年MM月dd日 E HH时mm分 "); 1015 } 1016 1017 public void Repeal() 1018 { 1019 1020 JOptionPane.showMessageDialog(this,"由于本人能力有限故还没有实现,请谅解!","记事本",JOptionPane.WARNING_MESSAGE); 1021 } 1022 public void Cut() 1023 { 1024 String temp = notebookframe.textarea.getSelectedText();//获得鼠标拖动选取的文本 1025 StringSelection text = new StringSelection(temp);//把待剪切的文本传递给 text 对象 1026 clipboard.setContents(text,null);//将文本放入剪切板中 1027 int start = notebookframe.textarea.getSelectionStart();//获取选中文本的开始位置 1028 int end = notebookframe.textarea.getSelectionEnd();//获取选中文本的结束位置 1029 notebookframe.textarea.replaceRange("",start,end);//选中的区域用""替换 1030 } 1031 public void Copy() 1032 { 1033 String temp = notebookframe.textarea.getSelectedText();//获得鼠标拖动选取的文本 1034 StringSelection text = new StringSelection(temp);//把待剪切的文本传递给 text 对象 1035 clipboard.setContents(text,null);//将文本放入剪切板中 1036 } 1037 public void Stick() 1038 { 1039 Transferable contexts = clipboard.getContents(notebookframe);//获取剪切板中的内容 1040 DataFlavor flavor = DataFlavor.stringFlavor;//剪切板的风格(系统的标准风格) 1041 if(contexts.isDataFlavorSupported(flavor))//判断风格java是否可用 1042 { 1043 try{ 1044 String str = (String)contexts.getTransferData(flavor); 1045 int start = notebookframe.textarea.getSelectionStart();//获取选中文本的开始位置 1046 int end = notebookframe.textarea.getSelectionEnd();//获取选中文本的结束位置 1047 notebookframe.textarea.replaceRange(str,start,end);//替换光标所在位置的文本 1048 1049 } 1050 catch(Exception ee){} 1051 } 1052 1053 } 1054 public void Delete() 1055 { 1056 String temp = notebookframe.textarea.getSelectedText();//获得鼠标拖动选取的文本 1057 int start = notebookframe.textarea.getSelectionStart(); 1058 int end = notebookframe.textarea.getSelectionEnd(); 1059 notebookframe.textarea.replaceRange("",start,end);//选中的区域用""替换 1060 1061 } 1062 public void LookUp() 1063 { 1064 searchfor(); 1065 } 1066 public void LookUpNext() 1067 { 1068 if(!notebookframe.textarea.getText().equals("")) 1069 { 1070 search.nextShear(); 1071 } 1072 else 1073 { 1074 JOptionPane.showMessageDialog(this,"文本内容不为空时才能使用该功能!","记事本",JOptionPane.WARNING_MESSAGE); 1075 } 1076 1077 } 1078 public void Exchange()//替换 1079 { 1080 searchfor(); 1081 } 1082 public void Goto()//转到 1083 { 1084 searchfor(); 1085 } 1086 public void All()//全选 1087 { 1088 notebookframe.textarea.selectAll(); 1089 } 1090 public void TimeDate()//时间的获得 1091 { 1092 notebookframe.textarea.append(dateformat.format(nowdate)); 1093 } 1094 public void searchfor()//为减少代码编写的 查找函数 1095 { 1096 if(!notebookframe.textarea.getText().equals("")) 1097 { 1098 search.setVisible(true); 1099 } 1100 else 1101 { 1102 JOptionPane.showMessageDialog(this,"文本内容不为空时才能使用该功能!","记事本",JOptionPane.WARNING_MESSAGE); 1103 } 1104 } 1105 1106 }