import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; public class TxtEditor extends JFrame implements ActionListener{ String file_name; String file_dir; String tempString; //上次保存后的文件名和地址 String fileName = ""; JPanel x=new JPanel();      JTextArea wen=new JTextArea(20,50);         JMenuItem ziti=new  JMenuItem("字体");   JMenuItem a=new JMenuItem("普通");          //定义菜单项   JMenuItem xin=new  JMenuItem("新建");   JMenuItem open=new JMenuItem("打开");   JMenuItem save=new JMenuItem("保存 ");   JMenuItem lsave=new  JMenuItem("另存为");   JMenuItem tui=new  JMenuItem("退出");      JMenuItem cut=new JMenuItem("剪切 ");   JMenuItem copy=new JMenuItem("复制");   JMenuItem cast=new  JMenuItem("粘贴");   JMenuItem delete=new  JMenuItem("删除 ");     JMenuItem b=new JMenuItem("粗体");   JMenuItem c=new JMenuItem("斜体"); TxtEditor(){ super ("文本编辑器       By  强凯 V1.0"); //小小对话框   setBounds(250,100,700,450);    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);    addWindowListener(new WindowAdapter(){        public void windowClosing(WindowEvent e) {        int   option=   JOptionPane.showConfirmDialog(         TxtEditor.this, "你真的想退出吗... ", "系统和你对话 ",JOptionPane.YES_NO_OPTION);         if(option==JOptionPane.YES_OPTION)     if(e.getWindow()   ==   TxtEditor.this)     {            System.exit(0);            }            else            {            return;                       }     }        });       //热键设置   xin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));   open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));   save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));   cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));   copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));   cast.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));     //定义面板        // x.add(         add(new  JScrollPane (wen));//);//滚动条          wen.setFont(new Font("楷体" , Font.PLAIN ,20));         // wen.setBackground(Color.blue);          // add(x);             //菜单栏的创建   JMenuBar cai=new JMenuBar();   this.setJMenuBar(cai);   cai.setOpaque(true);   JMenu jian=new JMenu("文件");   jian.add(xin);   jian.add(open);   jian.add(save);   jian.add(lsave);   jian.addSeparator( );   jian.add(tui);  cai.add(jian);    JMenu bian= new JMenu("编辑  ");  bian.add(cut);  bian.add(copy);  bian.add(cast);  bian.add(delete);   cai.add(bian);       JMenu geshi = new JMenu ("格式");      JMenu optionsMenu=new JMenu("字体");      geshi.add(optionsMenu);      optionsMenu.add(a);      optionsMenu.add(b);      optionsMenu.add(c);  cai.add(geshi);       //增加监听器 xin.addActionListener(this); open.addActionListener(this); save.addActionListener(this); lsave.addActionListener(this); tui.addActionListener(this); cut.addActionListener(this); copy.addActionListener(this); cast.addActionListener(this); delete.addActionListener(this); ziti.addActionListener(this); a.addActionListener(this); b.addActionListener(this); c.addActionListener(this); // 文本框锁定          //this.setResizable(false); }  //重写方法 public void actionPerformed(ActionEvent e){ String actionCommand=e.getActionCommand(); if(e.getSource()instanceof JMenu); {  if(e.getSource()==xin){ newfile();    }         else if(e.getSource()==open){        openfile();    } else if(e.getSource()==save){  savefile();   }  else if(e.getSource()==lsave){    lsavefile();   }  else if(e.getSource()==cut){    cutfile();    }  else if(e.getSource()==copy){     copyfile();    }  else if(e.getSource()==cast){  castfile();  }  else if(e.getSource()==delete){  deletefile();  }  else if(e.getSource()==a){  afile(); }  else if(e.getSource()==b){  bfile();  }  else if(e.getSource()==c){  cfile();  }  else if("退出".equals(actionCommand)) System.exit(0); } }    // 方法定义    public void newfile(){           savefile();     wen.setText(null);        fileName = "";             }    //打开          public void openfile(){         String fileName = null;          FileDialog df = new FileDialog(this,"打开文件",FileDialog.LOAD);          df.setVisible(true);         //建立新文件          File f = new File( df.getDirectory()+df.getFile() );          //得到文件名          fileName = df.getDirectory()+df.getFile();         //用此文件的长度建立一个字符数组  (特别标注)           char ch[] = new char [(int)f.length()];           //异常处理             try             {              //读出数据,并存入字符数组ch中              BufferedReader bw = new BufferedReader( new FileReader(f) );                   bw.read(ch);                         bw.close();             }             catch( FileNotFoundException fe ){              System.out.println("file not found");              System.exit(0);             }             catch( IOException ie){              System.out.println("IO error");              System.exit(0);             }                         String s =new String (ch);             wen.setText(s);                      }                    //保存        public void savefile(){        if( fileName.equals("") ){     FileDialog df = new FileDialog(this,"保存文件",FileDialog.SAVE);          df.addWindowListener( new WindowAdapter(){      public void windowClosing(WindowEvent ee){       System.exit(0);      }     });     df.setVisible(true);     String s = wen.getText();         try     {      File f = new File( df.getDirectory()+df.getFile());      fileName = df.getDirectory()+df.getFile();      BufferedWriter bw = new BufferedWriter( new FileWriter (f));      bw.write(s , 0 , s.length());      bw.close();     }     catch(FileNotFoundException fe_){      System.out.println("file not found");      System.exit(0);     }     catch( IOException ie_)     {      System.out.println(" IO error");      System.exit(0);     }        }      //如果文件已经保存过     else      {      String s = wen.getText();          try     {      File f = new File( fileName );           BufferedWriter bw = new BufferedWriter( new FileWriter (f));      bw.write(s , 0 , s.length());      bw.close();          }     catch(FileNotFoundException fe_){      System.out.println("file not found");      System.exit(0);     }     catch( IOException ie_)     {      System.out.println(" IO error");      System.exit(0);     }          }                }     //另存为         public void lsavefile(){         FileDialog df = new FileDialog(this,"另存为",FileDialog.SAVE);         df.addWindowListener( new WindowAdapter(){              public void windowClosing(WindowEvent ee){               System.exit(0);              }             });             df.setVisible(true);             String s = wen.getText();             try             {              File f = new File( df.getDirectory()+df.getFile());              BufferedWriter bw = new BufferedWriter( new FileWriter (f));              bw.write(s , 0 , s.length());              bw.close();             }             catch(FileNotFoundException fe_){              System.out.println("file not found");              System.exit(0);             }             catch( IOException ie_)             {              System.out.println(" IO error");              System.exit(0);             }                 }                    //剪切            public void cutfile(){                     tempString = wen.getSelectedText();              StringBuffer tmp = new StringBuffer ( wen.getText());             int start = wen.getSelectionStart();             int len = wen.getSelectedText().length();              tmp.delete( start , start+len);              wen.setText(tmp.toString());                 }   //复制        public void copyfile(){       tempString = wen.getSelectedText();         }          //粘贴        public void castfile(){       StringBuffer tmp = new StringBuffer ( wen.getText());         //得到要粘贴的位置         int start = wen.getSelectionStart();          tmp.insert(start , tempString);        //用新文本设置原文本          wen.setText(tmp.toString());        }                //删除        public void deletefile(){       StringBuffer tmp = new StringBuffer ( wen.getText());       int start = wen.getSelectionStart();        int len = wen.getSelectedText().length();         tmp.delete( start , start+len);         wen.setText(tmp.toString());        } //字体        public void afile(){        wen.setFont(new Font("楷体", Font.PLAIN ,wen.getFont().getSize()) );//普通文字        }                public void bfile(){        wen.setFont(new Font("楷体" , Font.BOLD ,wen.getFont().getSize()) );//粗体文字                     }        public void cfile(){       wen.setFont(new Font("楷体" , Font.ITALIC ,wen.getFont().getSize()) );//斜体文字        } public static void main(String[] args) { TxtEditor w=new TxtEditor(); w.pack(); w.setVisible(true); }

java 文本编辑器 源码_java文本编辑器源码相关推荐

  1. java单机版软件源码_Java五子棋单机版源码分享

    初学JavaGUI编程,就写了一个小游戏–五子棋. 目前只实现了单机版,任何事情都是从简制作的,有问题请大牛们不吝指教啊. 此处的棋盘和棋子都未用图片,全部都是使用Java中的Graphics画出来的 ...

  2. java 文本框输入事件_Java文本框上的ActionEvent事件 | 学步园

    三个概念:事件源, 监视器, 处理事件的接口 事件源 能够产生事件的对象都可以成为事件源,如文本框.按钮.下拉式列表等. 事件源必须是一个对象,而且这个对象必须是 Java认为能够发生事件的对象. 监 ...

  3. java文本框选中事件_Java文本框上的ActionEvent事件

    三个概念:事件源, 监视器, 处理事件的接口 事件源 能够产生事件的对象都可以成为事件源,如文本框.按钮.下拉式列表等. 事件源必须是一个对象,而且这个对象必须是 Java认为能够发生事件的对象. 监 ...

  4. java 转码_JAVA自带转码方式的学习

    最近工作中遇到一个问题,由于海外系统环境默认采用UTF-8格式编码,以支持多语言环境.而国内系统由于只需要支持中英文,故默认采用GBK编码格式.因此当把海外环境生成的交易报告单发送给国内的报表系统时, ...

  5. 手机端java编辑器验证正确_java – 使用编辑器验证表的单元格

    我的JTable有一个密码字段编辑器.我想在用户单击编辑另一个字段时,如果文本长度小于8位,则显示错误消息.我尝试过焦点听众.但它不起作用.请帮帮我,因为我刚刚开始使用 java swing. cla ...

  6. java 线程池 源码_java线程池源码分析

    我们在关闭线程池的时候会使用shutdown()和shutdownNow(),那么问题来了: 这两个方法又什么区别呢? 他们背后的原理是什么呢? 线程池中线程超过了coresize后会怎么操作呢? 为 ...

  7. java join 源码_java并发:join源码分析

    join join join是Thread方法,它的作用是A线程中子线程B在运行之后调用了B.join(),A线程会阻塞直至B线程执行结束 join源码(只有继承Thread类才能使用) 基于open ...

  8. java文本框输入数字_Java 文本框输入数字限制以及输入数字大小限制

    问题: 之前我有一篇博文也写了这个问题,也是使用的字符串来控制文本框的输入数字 不过,那篇博文给的方法只能限制数字输入,以及数字输入长度的限制 今天给出可以限制输入数字大小的方法,例如:希望输入的数字 ...

  9. Java绘画板源码_Java 绘图板 示例源码下载(画板)

    Java 绘图板 示例源码下载(画板) java 2020-8-21 下载地址 https://www.codedown123.com/36795.html package minidrawpad; ...

最新文章

  1. Python爬虫实战糗事百科实例
  2. Flex警告:framework.swc”具有默认样式并且在 library-path 中,表...
  3. 兼容IE8,滚动加载下一页
  4. mongodb数据文件结构——record是内嵌BSON的双向链表,多个record或索引组成extent...
  5. 【观点】避免不必要的代码缩进和嵌套
  6. java校验码的设计_Java动态验证码单线设计的两种方法
  7. ES分组聚合:计算每个tag下的商品数量且某个filed包含指定关键字,分组,平均,每个tags下的平均价格,排序,指定范围区间
  8. 计算机中央处理器cpu_中央处理器(CPU)| 计算机科学组织
  9. 使用PL/SQL删除百万条记录的大表
  10. 1844. 将所有数字用字符替换
  11. 大数据工程师简历_成为大数据工程师所需的技能
  12. ​24小时企业级微信小程序全套开发视频教程
  13. CSS · 单行、多行文本溢出显示省略号
  14. JavaScript分解质因数
  15. 应用wps对证件照进行更改颜色,更换只需三步。
  16. win10关闭最佳分辨率通知
  17. 微信小程序 springboot农产品在线商城系统java 助农电商
  18. python编译器报错:“RecursionError: maximum recursion depth exceeded in comparison”解决方案
  19. windows内核开发学习笔记十五:IRP结构
  20. mysql读取股票数据_读取股票数据存储到本地MySQL数据库(一)

热门文章

  1. 机器学习(二十三)——Beam Search, NLP机器翻译常用评价度量, 模型驱动 vs 数据驱动
  2. Infura Http 客户端 以太坊 交易
  3. 动态导入模块__import__(str) importlib标准库
  4. 为什么Python是最吸金编程语言?
  5. 机房管理系列之工作站
  6. Retrofit2/OkHttp 重写覆盖headers 与 不重写覆盖Headers
  7. 快速看完整部教材,列出你不懂的 5 - 10 个问题
  8. 解决SQL Server 2008安装时提示:重新启动计算机 失败
  9. 从零写一个编译器(十三):代码生成之遍历AST
  10. Leetcode 187.重复的DNA序列