练习~记事本

功能类似win自带的记事本,只摘录重要功能,更加轻量级(好像没什么用,记事本本来就很轻量。。),增加剪贴板编辑,这个功能有时还是很方便的。。

值得注意的问题是写出硬盘时要用Writer的newLine()方法,使用平台自己的行分隔符,手动添加行分隔符经常出错。

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.datatransfer.*;
  4 import java.awt.event.*;
  5 import java.io.*;
  6 
  7 public class notepad {
  8     
  9     //主方法
 10     public static void main(String [] args){
 11         notepad no = new notepad();
 12         no.noteui();
 13     }
 14     
 15     //创建界面
 16     public JFrame jf = null;
 17     public JTextArea ta = null;
 18     public JMenuItem linew = null;
 19     public void noteui(){
 20         //窗口
 21         jf = new JFrame("记事本");
 22         jf.setSize(600, 500);
 23         jf.setVisible(true);
 24         //窗口居中显示
 25         jf.setLocationRelativeTo(null);
 26         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 27                 
 28         //菜单栏
 29         JMenuBar menubar = new JMenuBar();
 30         jf.add(menubar, BorderLayout.NORTH);
 31         JMenu file = new JMenu("文件");
 32         file.setFont(new Font("微软雅黑", Font.PLAIN, 15));
 33         JMenu edit = new JMenu("编辑");
 34         edit.setFont(new Font("微软雅黑", Font.PLAIN, 15));
 35         JMenu format = new JMenu("格式");
 36         format.setFont(new Font("微软雅黑", Font.PLAIN, 15));
 37         JMenuItem clipB = new JMenuItem("剪贴板");
 38         clipB.setFont(new Font("微软雅黑", Font.PLAIN, 15));
 39         menubar.add(file);
 40         menubar.add(edit);
 41         menubar.add(format);
 42         menubar.add(clipB);
 43         
 44         //文件下拉菜单
 45         JMenuItem open = new JMenuItem("打开");
 46         JMenuItem save = new JMenuItem("保存");
 47         JMenuItem close = new JMenuItem("关闭");
 48         file.add(open);
 49         file.add(save);
 50         file.add(close);
 51         
 52         //编辑下拉菜单
 53         JMenuItem cut = new JMenuItem("剪切");
 54         JMenuItem copy = new JMenuItem("复制");
 55         JMenuItem paste = new JMenuItem("粘贴");
 56         JMenuItem find = new JMenuItem("查找");
 57         JMenuItem replace = new JMenuItem("替换");
 58         edit.add(cut);
 59         edit.add(copy);
 60         edit.add(paste);
 61         edit.add(find);
 62         edit.add(replace);
 63         
 64         //格式下拉菜单
 65         JMenuItem font = new JMenuItem("字体");
 66         linew = new JMenuItem("自动换行 ×");
 67         format.add(font);
 68         format.add(linew);
 69         
 70         //文本框
 71         ta = new JTextArea();
 72         ta.setFont(new Font(zt, Font.PLAIN, zh));
 73         JScrollPane jt = new JScrollPane(ta);
 74         jf.add(jt, BorderLayout.CENTER);
 75         ta.requestFocus();
 76         
 77         //实现方法
 78         open.addActionListener(new ActionListener(){
 79             public void actionPerformed(ActionEvent e) {
 80                 ifopen();
 81              }
 82         });
 83         
 84         save.addActionListener(new ActionListener(){
 85             public void actionPerformed(ActionEvent e) {
 86                 savem();
 87              }
 88         });
 89         
 90         close.addActionListener(new ActionListener(){
 91             public void actionPerformed(ActionEvent e) {
 92                 closem();
 93              }
 94         });
 95         
 96         cut.addActionListener(new ActionListener(){
 97             public void actionPerformed(ActionEvent e) {
 98                 cutm();
 99              }
100         });
101         
102         copy.addActionListener(new ActionListener(){
103             public void actionPerformed(ActionEvent e) {
104                 copym();
105              }
106         });
107         
108         paste.addActionListener(new ActionListener(){
109             public void actionPerformed(ActionEvent e) {
110                 pastem();
111              }
112         });
113         
114         find.addActionListener(new ActionListener(){
115             public void actionPerformed(ActionEvent e) {
116                 findm();
117              }
118         });
119         
120         replace.addActionListener(new ActionListener(){
121             public void actionPerformed(ActionEvent e) {
122                 replacem();
123              }
124         });
125         
126         font.addActionListener(new ActionListener(){
127             public void actionPerformed(ActionEvent e) {
128                 fontm();
129              }
130         });
131         
132         clipB.addActionListener(new ActionListener(){
133             public void actionPerformed(ActionEvent e) {
134                 clipBm();
135              }
136         });
137         
138         linew.addActionListener(new ActionListener(){
139             public void actionPerformed(ActionEvent e) {
140                 linewm();
141              }
142         });
143         
144     }
145 
146     public void ifopen() {
147         if("".equals(wjm)){
148             openm();
149         }else{
150             int option = JOptionPane.showConfirmDialog(null,
151                     "是否放弃编辑该文本,打开新文本?", "正在编辑" + wjm, JOptionPane.YES_NO_OPTION,
152                     JOptionPane.QUESTION_MESSAGE, null);
153             switch (option) {
154                 case JOptionPane.YES_NO_OPTION: {
155                     openm();
156                     break;
157                 }
158             }
159         }
160     }
161 
162     //打开方法
163     public JFrame openf = null; 
164     public JTextField owji = null;
165     public JLabel otl = null;
166     public String wjm = "";
167     public String nrold = "";
168     public void openm(){
169         openf = new JFrame("打开");
170         openf.setVisible(true);
171         openf.setSize(500, 250);
172         openf.setLocationRelativeTo(null);
173         JPanel op = new JPanel();
174         openf.add(op);
175         Box fbox = Box.createVerticalBox();
176         op.add(fbox);
177         
178         fbox.add(Box.createVerticalStrut(40));
179         
180         JPanel owjmp = new JPanel();
181         owjmp.setOpaque(false);
182         JLabel owt = new JLabel("文件名或目录:");
183         owt.setFont(new Font("华文中宋", Font.PLAIN, 18));
184         owji = new JTextField(20);
185         owji.setFont(new Font("微软雅黑", Font.PLAIN, 15));
186         JLabel owdt = new JLabel(".txt");
187         owdt.setFont(new Font("微软雅黑", Font.PLAIN, 15));
188         owjmp.add(owt);
189         owjmp.add(owji);
190         owjmp.add(owdt);
191         fbox.add(owjmp);
192         
193         fbox.add(Box.createVerticalStrut(20));
194         
195         JPanel obp = new JPanel();
196         JButton odb = new JButton("打 开");
197         odb.setFont(new Font("微软雅黑", Font.PLAIN, 15));    
198         JLabel obt = new JLabel("    ");
199         obt.setFont(new Font("华文中宋", Font.PLAIN, 15));
200         JButton oqb = new JButton("创 建");
201         oqb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
202         obp.add(odb);
203         obp.add(obt);
204         obp.add(oqb);
205         fbox.add(obp);
206         
207         fbox.add(Box.createVerticalStrut(20));
208         
209         JPanel otp =new JPanel();
210         otp.setOpaque(false);
211         otl = new JLabel();
212         otl.setFont(new Font("微软雅黑", Font.PLAIN, 15));
213         otp.add(otl);
214         fbox.add(otp);
215         
216         odb.addActionListener(new ActionListener(){
217             public void actionPerformed(ActionEvent e) {
218                 BufferedReader br1 = null;
219                 BufferedReader br2 = null;
220                 try{
221                     String name = owji.getText();
222                     if("".equals(name)){
223                         otl.setText("请输入文本的目录或当前目录下的文本名");
224                     }else{
225                         name = name + ".txt";
226                         if(name.indexOf("\\") != -1){
227                             //若为绝对目录,更改分隔符
228                             name = name.replaceAll("\\\\", "/");
229                         }
230                         File book = new File(name);
231                         if(book.exists()){
232                             //文件存在时读入编辑器
233                             br1 = new BufferedReader(new FileReader(book));
234                             int line = 0;
235                             //判断有多少行
236                             while(true){
237                                 if(br1.readLine() != null){
238                                     line++;
239                                 }else{
240                                     br1.close();
241                                     break;
242                                 }
243                             }
244                             br2 = new BufferedReader(new FileReader(book));
245                             //将文本内容转换为 String 类型
246                             nrold = "";
247                             for(int i=0; i < line; i++){
248                                 nrold =  nrold + br2.readLine() + "\n";
249                             }
250                             //str = str.substring(0,str.length()-2);
251                             br2.close();
252                             ta.setText(nrold);
253                             nrnew = nrold;
254                             //关闭窗口,但不结束程序
255                             openf.dispose();
256                             wjm = name;
257                             jf.setTitle("记事本——正在编辑:" + wjm);
258                         }else{
259                             //文件不存在时提示
260                             otl.setText("找不到文本,请检查输入是否有误,或者“创建”该文本");
261                         }
262                     }
263                     
264                 }catch(IOException e1){
265                     JOptionPane.showMessageDialog(null,
266                             e, "错误", JOptionPane.ERROR_MESSAGE);
267                 }finally{
268                     try{
269                         if(br1 != null){ br1.close(); }
270                         if(br2 != null){ br2.close(); }
271                     }catch(IOException e1){
272                         System.out.println(e1);
273                     }
274                 }
275              }
276         });
277         
278         oqb.addActionListener(new ActionListener(){
279             public void actionPerformed(ActionEvent e) {
280                 String name = owji.getText();
281                 if("".equals(name)){
282                     otl.setText("请输入新文本的目录或文本名");
283                 }else{
284                     name = name + ".txt";
285                     if(name.indexOf("\\") != -1){
286                         //若为绝对目录,更改分隔符
287                         name = name.replaceAll("\\\\", "/");
288                     }
289                     File book = new File(name);
290                     if(book.exists()){
291                         //文件存在时提示重写
292                         int option = JOptionPane.showConfirmDialog(null,
293                                 "该文本已经存在,是否重写该文本?", "创建", JOptionPane.YES_NO_OPTION,
294                                 JOptionPane.INFORMATION_MESSAGE, null);
295                         switch (option) {
296                             case JOptionPane.YES_NO_OPTION: {
297                                 wjm = name;
298                                 build();
299                                 break;
300                             }
301                         }
302                     }else{
303                         wjm = name;
304                         build();
305                     }
306                 }
307              }
308         });
309     }
310     
311     //创建方法
312     public void build(){
313         jf.setTitle("记事本——正在编辑:" + wjm);
314         nrnew = ta.getText();
315         nrold = "";
316         openf.dispose();
317         if(s1 == 1){
318             savem();
319         }
320     }
321     
322     //保存方法
323     public int s1 = 0;
324     public String nrnew = "";
325     public void savem(){
326         BufferedWriter bw = null;
327         try {
328             if ("".equals(wjm)) {
329                 //消息提示框
330                 int option = JOptionPane.showConfirmDialog(null,
331                         "没有保存路径及文件名,是否现在设置保存路径及文件名?", "保存", JOptionPane.YES_NO_OPTION,
332                         JOptionPane.INFORMATION_MESSAGE, null);
333                 switch (option) {
334                     case JOptionPane.YES_NO_OPTION: {
335                         openm();
336                         s1 = 1;
337                         otl.setText("请按“创建”添加保存路径及文件名");
338                         break;
339                     }
340                     //case JOptionPane.NO_OPTION:
341                         //    System.exit(0);
342                 }
343             }else{
344                 nrnew = ta.getText();
345                 String[] sf = nrnew.split("\n");
346                 bw = new BufferedWriter(new FileWriter(wjm));
347                 for (int i = 0; i < sf.length; i++) {
348                     bw.write(sf[i]);
349                     bw.newLine();
350                 }
351                 nrold = nrnew;
352                 s1 = 0;
353             }
354         } catch (IOException e) {
355             JOptionPane.showMessageDialog(null,
356                     e, "错误", JOptionPane.ERROR_MESSAGE);
357         } finally{
358             try{
359                 if(bw != null){ bw.close(); }
360             }catch(IOException e1){
361                 System.out.println(e1);
362             }
363         }
364     }
365     
366     //关闭方法
367     public void closem(){
368         nrnew = ta.getText();
369         if (nrold.equals(nrnew)) {
370             if("".equals(wjm)){
371                 System.exit(0);
372             }else{
373                 wjm = "";
374                 jf.setTitle("记事本");
375                 nrnew = nrold = "";
376                 ta.setText(nrold);
377             }
378         } else {
379             int option = JOptionPane.showConfirmDialog(null,
380                     "文件已修改,是否保存?", "保存文件?", JOptionPane.YES_NO_OPTION,
381                     JOptionPane.QUESTION_MESSAGE, null);
382             switch (option) {
383                 case JOptionPane.YES_NO_OPTION: {
384                     savem();
385                     if (!("".equals(wjm))) {
386                         closem();
387                     }
388                     break;
389                 }
390                 case JOptionPane.NO_OPTION: {
391                     nrold = nrnew;
392                     closem();
393                 }
394             }
395         }
396     }
397     
398     /**不使用系统剪贴板*/
399     /*
400     //剪切方法
401     String clipb= null;
402     public void cutm(){
403         //获取被选中的文本域 
404         clipb = ta.getSelectedText();
405         ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
406     }
407     
408     //复制方法
409     public void copym(){
410         clipb = ta.getSelectedText();
411     }
412     
413     //粘贴方法
414     public void pastem(){
415         ta.replaceRange(clipb,ta.getSelectionStart(),ta.getSelectionEnd());
416     }
417     
418     //剪贴板编辑
419     public JFrame jtf = null;
420     public JTextArea jtb = null;
421     public void clipBm() {
422         jtf = new JFrame("剪贴板");
423         jtf.setSize(500, 300);
424         jtf.setVisible(true);
425         jtf.setLocationRelativeTo(null);
426         
427         jtb = new JTextArea();
428         jtb.setFont(new Font("微软雅黑", Font.PLAIN, 18));
429         jtf.add(jtb, BorderLayout.CENTER);
430         jtb.setText(clipb);
431         
432         JPanel jbp = new JPanel();
433         JButton jxb = new JButton("修 改");
434         jxb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
435         jbp.add(jxb);
436         jtf.add(jbp, BorderLayout.SOUTH);
437         
438         jxb.addActionListener(new ActionListener(){
439             public void actionPerformed(ActionEvent e) {
440                 clipb = jtb.getText();
441                 JOptionPane.showMessageDialog(null, "修改成功!", "剪贴板", JOptionPane.INFORMATION_MESSAGE);
442             }
443         });
444     }*/
445     
446     /**使用系统剪贴板*/
447     //剪切方法
448     //获得系统粘帖板
449     public Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
450     public void cutm(){
451         copym();
452         ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
453     }
454     
455     //复制方法
456     public void copym(){
457         String text = ta.getSelectedText();
458         //StringSelection : 实现传输 String 所需能力的 Transferable
459         StringSelection selection=new StringSelection(text);
460         //将剪贴板的当前内容设置到指定的 transferable 对象
461         clipBoard.setContents(selection,null); 
462     }
463     
464     //粘贴方法
465     public String clipb= null;
466     public void pastem(){
467         try {
468             //返回表示剪贴板当前内容的 transferable 对象
469             Transferable clipT = clipBoard.getContents(this);
470             if (clipT != null){
471                 // 检查内容是否是文本类型
472                 if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)){
473                     clipb = (String)clipT.getTransferData(DataFlavor.stringFlavor);
474                 }
475             }else{
476                 clipb = null;
477             }
478             //将文本中选中的部分替换为剪贴板内容
479             ta.replaceRange(clipb,ta.getSelectionStart(),ta.getSelectionEnd());
480         } catch (Exception e) {
481             JOptionPane.showMessageDialog(null,
482                     e, "错误", JOptionPane.ERROR_MESSAGE);
483         }
484     }
485     
486     //剪贴板编辑(*-*)made by feng
487     public JFrame jtf = null;
488     public JTextArea jtb = null;
489     public void clipBm() {
490         jtf = new JFrame("剪贴板");
491         jtf.setSize(500, 300);
492         jtf.setVisible(true);
493         jtf.setLocationRelativeTo(null);
494         
495         jtb = new JTextArea();
496         jtb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
497         jtf.add(jtb, BorderLayout.CENTER);
498         try {
499             //返回表示剪贴板当前内容的 transferable 对象
500             Transferable clipT = clipBoard.getContents(this);
501             if (clipT != null){
502                 // 检查内容是否是文本类型 
503                 if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)){
504                     clipb = (String)clipT.getTransferData(DataFlavor.stringFlavor);
505                 }
506             }else{
507                 clipb = null;
508             }
509         } catch (Exception e) {
510             JOptionPane.showMessageDialog(null,
511                     e, "错误", JOptionPane.ERROR_MESSAGE);
512         }
513         jtb.setText(clipb);
514         
515         JPanel jbp = new JPanel();
516         JButton jxb = new JButton("修 改");
517         jxb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
518         jbp.add(jxb);
519         jtf.add(jbp, BorderLayout.SOUTH);
520         
521         jxb.addActionListener(new ActionListener(){
522             public void actionPerformed(ActionEvent e) {
523                 clipb = jtb.getText();
524                 StringSelection selection=new StringSelection(jtb.getText());
525                 //获得系统粘帖板
526                 clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
527                 //将剪贴板的当前内容设置到指定的 transferable 对象
528                 clipBoard.setContents(selection,null); 
529                 JOptionPane.showMessageDialog(null, "修改成功!", "剪贴板", JOptionPane.INFORMATION_MESSAGE);
530             }
531         });
532     }
533     
534     
535     //查找方法
536     public JFrame findf = null;
537     public JTextField chai = null;
538     public JLabel ftl = null;
539     public void findm(){
540         findf = new JFrame("查 找");
541         findf.setVisible(true);
542         findf.setSize(400, 220);
543         findf.setLocationRelativeTo(null);
544         JPanel fp = new JPanel();
545         findf.add(fp);
546         Box fbox = Box.createVerticalBox();
547         fp.add(fbox);
548         
549         fbox.add(Box.createVerticalStrut(25));
550         
551         JPanel chap = new JPanel();
552         chap.setOpaque(false);
553         JLabel chat = new JLabel("查找内容:");
554         chat.setFont(new Font("华文中宋", Font.PLAIN, 18));
555         chai = new JTextField(15);
556         chai.setFont(new Font("微软雅黑", Font.PLAIN, 15));
557         chap.add(chat);
558         chap.add(chai);
559         fbox.add(chap);
560         
561         fbox.add(Box.createVerticalStrut(15));
562         
563         JPanel fbp = new JPanel();
564         JButton fsb = new JButton("上一个");
565         fsb.setFont(new Font("微软雅黑", Font.PLAIN, 15));    
566         JLabel fbt = new JLabel("    ");
567         fbt.setFont(new Font("华文中宋", Font.PLAIN, 15));
568         JButton fxb = new JButton("下一个");
569         fxb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
570         fbp.add(fsb);
571         fbp.add(fbt);
572         fbp.add(fxb);
573         fbox.add(fbp);
574         
575         fbox.add(Box.createVerticalStrut(15));
576         
577         JPanel ftp =new JPanel();
578         ftp.setOpaque(false);
579         ftl = new JLabel();
580         ftl.setFont(new Font("微软雅黑", Font.PLAIN, 15));
581         ftl.setForeground(Color.red);
582         ftp.add(ftl);
583         fbox.add(ftp);
584         
585         fsb.addActionListener(new ActionListener(){
586             public void actionPerformed(ActionEvent e) {
587                 finds();
588             }
589         });
590         
591         fxb.addActionListener(new ActionListener(){
592             public void actionPerformed(ActionEvent e) {
593                 findx();
594             }
595         });
596         
597     }
598     
599     //查找上一处方法
600     public void finds(){
601         String text = ta.getText();
602         String findt = chai.getText();
603         if ("".equals(findt)) {
604             ftl.setText("输入不能为空");
605         }else{
606             if (text.indexOf(findt) == -1) {
607                 ftl.setText("找不到指定的内容");
608             } else {
609                 int sl = 0;
610                 int z = 0;
611                 //计算出现次数
612                 while (true) {
613                     if (text.indexOf(findt, z) != -1) {
614                         z = text.indexOf(findt, z) + 1;
615                         sl++;
616                     } else {
617                         break;
618                     }
619                 }
620                 int n = zb - 1;
621                 //查找上一处
622                 if (text.lastIndexOf(findt, n) != -1) {
623                     zb = text.lastIndexOf(findt, n);
624                     //设置查找内容处于选中状态
625                     ta.setSelectionStart(zb);
626                     ta.setSelectionEnd(zb + findt.length());
627                     cs--;
628                     ftl.setText("共找到" + sl + "处,第" + cs + "处");
629                 } else {
630                     zb = ta.getText().length();
631                     cs = sl + 1;
632                     finds();
633                 }
634             }
635         }
636     }
637     
638     //查找下一处方法
639     public int zb = 0;
640     public int cs = 0;
641     public void findx(){
642         String text = ta.getText();
643         String findt = chai.getText();
644         if ("".equals(findt)) {
645             ftl.setText("输入不能为空");
646         }else{
647             if (text.indexOf(findt) == -1) {
648                 ftl.setText("找不到指定的内容");
649             } else {
650                 int sl = 0;
651                 int z = 0;
652                 //计算出现次数
653                 while (true) {
654                     if (text.indexOf(findt, z) != -1) {
655                         z = text.indexOf(findt, z) + 1;
656                         sl++;
657                     } else {
658                         break;
659                     }
660                 }
661                 int n = zb + 1;
662                 //查找下一处
663                 if (text.indexOf(findt, n) != -1) {
664                     zb = text.indexOf(findt, n);
665                     //设置查找内容处于选中状态
666                     ta.setSelectionStart(zb);
667                     ta.setSelectionEnd(zb + findt.length());
668                     cs++;
669                     ftl.setText("共找到" + sl + "处,第" + cs + "处");
670                 } else {
671                     zb = -1;
672                     cs = 0;
673                     findx();
674                 }
675             }
676         }
677     }
678     
679     //替换方法
680     public JFrame repf = null;
681     public JTextField rfsi = null;
682     public JTextField rrsi = null;
683     public JLabel rtl = null;
684     public void replacem(){
685         repf = new JFrame("替 换");
686         repf.setVisible(true);
687         repf.setSize(400, 280);
688         repf.setLocationRelativeTo(null);
689         JPanel rp = new JPanel();
690         repf.add(rp);
691         Box rbox = Box.createVerticalBox();
692         rp.add(rbox);
693         
694         rbox.add(Box.createVerticalStrut(30));
695           
696         JPanel rfp = new JPanel();
697         rfp.setOpaque(false);
698         JLabel rft = new JLabel(" 查 找 :");
699         rft.setFont(new Font("华文中宋", Font.PLAIN, 20));
700         rfsi = new JTextField(8);
701         rfsi.setFont(new Font("微软雅黑", Font.PLAIN, 18));
702         rfsi.setText(ta.getSelectedText());
703         rfp.add(rft);
704         rfp.add(rfsi);
705         rbox.add(rfp);
706           
707         JPanel rrp = new JPanel();
708         JLabel rrt = new JLabel("替换为:");
709         rrt.setFont(new Font("华文中宋", Font.PLAIN, 20));
710         rrsi = new JTextField(8);
711         rrsi.setFont(new Font("微软雅黑", Font.PLAIN, 18));
712         rrp.add(rrt);
713         rrp.add(rrsi);
714         rbox.add(rrp);
715         rrsi.requestFocus();
716           
717         rbox.add(Box.createVerticalStrut(20));
718           
719         JPanel rbp =new JPanel();
720         rbp.setOpaque(false);
721         JButton rxb = new JButton("下一个");
722         rxb.setFont(new Font("微软雅黑", Font.PLAIN, 15));            
723         rbp.add(rxb);
724         JLabel rbt = new JLabel(" ");
725         rbt.setFont(new Font("华文中宋", Font.PLAIN, 15));
726         rbp.add(rbt);
727         JButton rtb = new JButton("替 换");
728         rtb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
729         rbp.add(rtb);
730         JLabel rbt1 = new JLabel(" ");
731         rbt1.setFont(new Font("华文中宋", Font.PLAIN, 15));
732         rbp.add(rbt1);
733         JButton rqb = new JButton("全部替换");
734         rqb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
735         rbp.add(rqb);
736         rbox.add(rbp);
737 
738         rbox.add(Box.createVerticalStrut(20));
739         
740         JPanel rtp =new JPanel();
741         rtp.setOpaque(false);
742         rtl = new JLabel();
743         rtl.setFont(new Font("微软雅黑", Font.PLAIN, 18));
744         rtp.add(rtl);
745         rbox.add(rtp);
746         
747         rxb.addActionListener(new ActionListener(){
748             public void actionPerformed(ActionEvent e) {
749                 rfind();
750             }
751         });
752         
753         rtb.addActionListener(new ActionListener(){
754             public void actionPerformed(ActionEvent e) {
755                 String text = ta.getSelectedText();
756                 if(text == null){
757                     rtl.setText("未查找到内容");
758                 }else{
759                     ta.replaceRange(rrsi.getText(), ta.getSelectionStart(), ta.getSelectionEnd());
760                     rtl.setText("替换完成");
761                 }
762             }
763         });
764         
765         rqb.addActionListener(new ActionListener(){
766             public void actionPerformed(ActionEvent e) {
767                 String t = ta.getText();
768                 t = t.replace(rfsi.getText(), rrsi.getText());
769                 ta.setText(t);
770                 JOptionPane.showMessageDialog(null, "替换完成!", "全部替换", JOptionPane.INFORMATION_MESSAGE);
771             }
772         });
773     }
774     
775     //替换中的查找方法
776     public void rfind(){
777         String text = ta.getText();
778         String findt = rfsi.getText();
779         if ("".equals(findt)) {
780             rtl.setText("输入不能为空");
781         }else{
782             if (text.indexOf(findt) == -1) {
783                 rtl.setText("找不到指定的内容");
784             } else {
785                 int sl = 0;
786                 int z = 0;
787                 //计算出现次数
788                 while (true) {
789                     if (text.indexOf(findt, z) != -1) {
790                         z = text.indexOf(findt, z) + 1;
791                         sl++;
792                     } else {
793                         break;
794                     }
795                 }
796                 int n = zb + 1;
797                 //查找下一处
798                 if (text.indexOf(findt, n) != -1) {
799                     zb = text.indexOf(findt, n);
800                     //设置查找内容处于选中状态
801                     ta.setSelectionStart(zb);
802                     ta.setSelectionEnd(zb + findt.length());
803                     cs++;
804                     rtl.setText("共找到" + sl + "处,第" + cs + "处");
805                 } else {
806                     zb = -1;
807                     cs = 0;
808                     rfind();
809                 }
810             }
811         }
812     }
813 
814     //字体方法《楓~》
815     public JFrame fontf = null;
816     public JTextField fzsi = null;
817     public JTextField fdsi = null;
818     public String zt = "华文中宋";
819     public int zh = 20;
820     public JRadioButton zxp = null;
821     public JRadioButton zxb = null;
822     public JLabel fztt = null;
823     public void fontm(){
824         fontf = new JFrame("字体");
825         fontf.setVisible(true);
826         fontf.setSize(400, 400);
827         fontf.setLocationRelativeTo(null);
828         JPanel fp = new JPanel();
829         fontf.add(fp);
830         Box fbox = Box.createVerticalBox();
831         fp.add(fbox);
832         
833         fbox.add(Box.createVerticalStrut(30));
834           
835         JPanel fzp = new JPanel();
836         fzp.setOpaque(false);
837         JLabel fzt = new JLabel(" 字 体 :");
838         fzt.setFont(new Font("华文中宋", Font.PLAIN, 20));
839         fzsi = new JTextField(8);
840         fzsi.setText(zt);
841         fzsi.setFont(new Font("微软雅黑", Font.PLAIN, 18));
842         fzp.add(fzt);
843         fzp.add(fzsi);
844         fbox.add(fzp);
845           
846         JPanel fdp = new JPanel();
847         JLabel fdt = new JLabel(" 字 号 :");
848         fdt.setFont(new Font("华文中宋", Font.PLAIN, 20));
849         fdsi = new JTextField(8);
850         fdsi.setText(zh + "");
851         fdsi.setFont(new Font("微软雅黑", Font.PLAIN, 18));
852         fdp.add(fdt);
853         fdp.add(fdsi);
854         fbox.add(fdp);
855         
856         JPanel fzxp = new JPanel();
857         fzxp.setOpaque(false);
858         JLabel fzxt = new JLabel(" 字 型 :");
859         fzxt.setFont(new Font("华文中宋", Font.PLAIN, 20));
860         zxp = new JRadioButton("常 规", true);
861         zxp.setFont(new Font("微软雅黑", Font.PLAIN, 18));
862         zxb = new JRadioButton("加 粗", false);
863         zxb.setFont(new Font("微软雅黑", Font.PLAIN, 18));
864         ButtonGroup zxg = new ButtonGroup();
865         zxg.add(zxp);
866         zxg.add(zxb);
867         fzxp.add(fzxt);
868         fzxp.add(zxp);
869         fzxp.add(zxb);
870         fbox.add(fzxp);
871           
872         fbox.add(Box.createVerticalStrut(20));
873           
874         JPanel fbp =new JPanel();
875         fbp.setOpaque(false);
876         JButton fqb = new JButton("确  认");
877         fqb.setFont(new Font("微软雅黑", Font.PLAIN, 15));            
878         fbp.add(fqb);
879         JLabel fbt = new JLabel("    ");
880         fbt.setFont(new Font("华文中宋", Font.PLAIN, 15));
881         fbp.add(fbt);
882         JButton ffb = new JButton("重  置");
883         ffb.setFont(new Font("微软雅黑", Font.PLAIN, 15));
884         fbp.add(ffb);
885         fbox.add(fbp);
886         
887         fbox.add(Box.createVerticalStrut(20));
888         
889         JPanel ftsp =new JPanel();
890         ftsp.setOpaque(false);
891         fztt = new JLabel("示例:                        ");
892         fztt.setFont(new Font("华文中宋", Font.PLAIN, 18));
893         ftsp.add(fztt);
894         fbox.add(ftsp);
895         
896         fbox.add(Box.createVerticalStrut(5));
897           
898         JPanel ftp =new JPanel();
899         ftp.setOpaque(false);
900         fztt = new JLabel("Made by 枫~");
901         fztt.setFont(new Font(zt, Font.PLAIN, zh));
902         ftp.add(fztt);
903         fbox.add(ftp);
904         
905         //监听
906         fqb.addActionListener(new ActionListener(){
907             public void actionPerformed(ActionEvent e) {
908                 zt = fzsi.getText();
909                 zh = Integer.valueOf(fdsi.getText());
910                 if(zxp.isSelected()){
911                     fztt.setFont(new Font(zt, Font.PLAIN, zh));
912                     ta.setFont(new Font(zt, Font.PLAIN, zh));
913                 }else{
914                     fztt.setFont(new Font(zt, Font.BOLD, zh));
915                     ta.setFont(new Font(zt, Font.BOLD, zh));
916                 }
917             }
918         });
919           
920         ffb.addActionListener(new ActionListener(){
921             public void actionPerformed(ActionEvent e) {
922                 zt = "华文中宋";
923                 zh = 20;
924                 fzsi.setText(zt);
925                 fdsi.setText(zh + "");
926                 zxp.setSelected(true);
927                 fztt.setFont(new Font(zt, Font.PLAIN, zh));
928                 ta.setFont(new Font(zt, Font.PLAIN, zh));
929             }
930         });
931     }
932     
933     //自动换行方法
934     public int h = 0;
935     public void linewm(){
936         if(h == 0){
937             ta.setLineWrap(true);
938             linew.setText("自动换行 √");
939             h = 1;
940         }else{
941             ta.setLineWrap(false);
942             linew.setText("自动换行 ×");
943             h = 0;
944         }
945     }
946     
947 }

 

posted @ 2017-04-26 02:27  枫林晚月  阅读(259)  评论(0编辑  收藏  举报