利用swing组件JEditText实现复制,粘贴,剪切功能,JEditText组测MouseListener,实现JPopupMenu右键弹出菜单,依次为JMenuItem,openButton,saveButton注册ActionListener,实现点击事件。

Word.java:

import javax.swing.*;
import javax.swing.JComponent.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Component.*;
//文本编辑器,打开.保存.右键复制粘贴剪切功能
//java.awt 与 java.awt.event 不是一个类
//
public class Word implements MouseListener,ActionListener{JFrame frame;JPopupMenu popMenu;JPanel panel;JEditorPane editText;JButton openButton;JButton saveButton;JMenuItem paste;JMenuItem copy;JMenuItem cut;JMenuBar menuBar;FileDialog fileDialog; String path;//要保存/打开的文件路径String name;//要保存/打开的文件名
//  JMenuItem delete;public static void main(String[] args){Word word=new Word();
}
//初始化界面public Word(){frame=new JFrame();panel=new JPanel();editText=new JEditorPane();popMenu=new JPopupMenu();Font font=new Font("宋体",Font.BOLD,20);openButton=new JButton("open");openButton.setFont(font);saveButton=new JButton("save");saveButton.setFont(font);panel.setLayout(null);Color color=new Color(191,210,200);panel.setBackground(color);paste=new JMenuItem("paste");copy=new JMenuItem("copy");cut=new JMenuItem("cut");
//      delete=new JMenuItem("delete");
//try{//      String pathString = "/home/zx/邯郸-刘书锋太极体.ttf";//              Font editFont = Font.createFont(Font.BOLD, new File(pathString));
//      editText.setFont(editFont);
//}catch(FontFormatException|IOException fe){//      fe.printStackTrace();
//}
//      Font editFont=new Font ("宋体",Font.BOLD,20);editText.setBackground(color);editText.setBounds(0,40,780,760);editText.setFont(font);editText.addMouseListener(this);popMenu.add(paste);popMenu.add(copy);popMenu.add(cut);openButton.addActionListener(this);saveButton.addActionListener(this);
//      popMenu.add(delete);paste.addActionListener(this);cut.addActionListener(this);copy.addActionListener(this);
//      delete.addActionListener(this);//button 设置大小太小会导致显示不出来文字openButton.setBounds(0,10,80,30);saveButton.setBounds(700,10,80,30);saveButton.setBackground(color);openButton.setBackground(color);panel.add(editText);panel.add(openButton);panel.add(saveButton);panel.add(popMenu);frame.setSize(780,800);frame.getContentPane().add(panel);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
//粘贴public void paste(){System.out.println("paste");editText.paste();
}
//复制public void copy(){System.out.println("copy");editText.copy();
}
//剪切public void cut(){System.out.println("cut");editText.cut();}
//打开文件public void open(){System.out.println("Open file!");fileDialog=new FileDialog(frame,"打开文件",FileDialog.LOAD);fileDialog.setVisible(true);path=fileDialog.getDirectory();name=fileDialog.getFile();read();
}
//保存文件public void save(){System.out.println("Save File!");fileDialog=new FileDialog(frame,"保存文件",FileDialog.SAVE);fileDialog.setVisible(true);path=fileDialog.getDirectory();name=fileDialog.getFile();write();}
//读文件public void read(){try{File read=new File(path,name);BufferedReader reader=new BufferedReader(new FileReader(read));//string 类无append()方法StringBuilder builder=new StringBuilder();String line;while((line=reader.readLine())!=null){builder.append(line+"\n");System.out.println(line);
}editText.setText(builder.toString());reader.close();//File类无close()
}catch(IOException e){e.printStackTrace();
//作用域到括号截至  read 找不到
//  read.close();
}}
//写文件public void write(){try{File writeFile=new File(path,name);BufferedWriter write=new BufferedWriter(new FileWriter(writeFile));write.write(editText.getText());write.close();
}catch(IOException e){e.printStackTrace();
//      writeFile.close();
}}
//鼠标点击事件public void mouseClicked(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mousePressed(MouseEvent e){if(e.getButton()==MouseEvent.BUTTON3)popMenu.show(e.getComponent(),e.getX(),e.getY());
}public void mouseReleased(MouseEvent e){}
//MouseEvent 有什么方法可以返回调用组件的String 或代号?
//用ActionEvent的话不能屏蔽右键选择
//  class SpecialListener implements MouseListener{public void actionPerformed(ActionEvent e){System.out.println("我要开始比较啦!");//      if(e.getButton()==MouseEvent.BUTTON3){String which=e.getActionCommand();switch(which){case "paste":paste();break;case "copy":copy();break;case "cut":cut();break;case "open":open();break;case "save":save();}
}}
后续功能:改变字体,插入图片

java实现文本编辑器相关推荐

  1. JAVA写文本编辑器(四)

    其实写到这里,我已经差不多断气了... 常规套路,这里是前三篇的传送门,需要的同学可以看一下:JAVA写文本编辑器(三) JAVA写文本编辑器(二)JAVA写文本编辑器(一) 现在我们只差最后一部分了 ...

  2. 25、Java Swing文本编辑器的实现

    最简单的文本编辑器要数 Windows 的记事本了.在学习了本章知识之后,本次案例将完成一个简单的文本编辑器程序.它可以打开文本文件,并输出文本文件的位置和内容.其中用到了菜单.工具栏以及选项卡等组件 ...

  3. java 富文本编辑器的标签处理数据 从数据库中取出 并返回前台

    总结: 1.使用 HtmlUtils.htmlEscape ()方法对 字符串中的标签转义 2.使用 removeHtmlTag() 得数据 具体步骤如下: 我的数据库的存储格式被转义过了,这个因人而 ...

  4. JAVA写文本编辑器(一)

    作业题,花了整整两天终于搞出来了,今天给大家整理一下思路. 这里用swing ,awt写的. 我们大概要做成一个电脑的记事本那样的一个编辑器.可以调整字体,字号,颜色.能够打开.保存文件,新建窗口,点 ...

  5. java html文本编辑器,基于Java WebHTML在线文本编辑器解决方案.doc

    基于Java WebHTML在线文本编辑器解决方案 基于Java WebHTML在线文本编辑器解决方案 摘要:FckEditor作为众多优秀HTML在线文本编辑器之一,以其支持多语言的优势而受到众多国 ...

  6. Swing学习25:Java Swing文本编辑器的实现

    最简单的文本编辑器要数 Windows 的记事本了.在学习了本章知识之后,本次案例将完成一个简单的文本编辑器程序.它可以打开文本文件,并输出文本文件的位置和内容.其中用到了菜单.工具栏以及选项卡等组件 ...

  7. java 文本编辑器_「java文本编辑器」用Java实现文本编辑器 - seo实验室

    java文本编辑器 源码里有注释:import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; ...

  8. java富文本编辑器KindEditor

    在页面写一个编辑框: <textarea name="content" class="form-control" id="content&quo ...

  9. java富文本编辑器wangEditor-3.1.1的使用流程

    wangEditor-3.1.1官方: http://www.wangeditor.com/ 使用文档:https://www.kancloud.cn/wangfupeng/wangeditor3/3 ...

  10. 我做的一个java简单文本编辑器代码

    下面是我的一个简单java程序代码,可以练习下. 有的功能没弄完,有兴趣的可以完成他,对学java 很有帮助. package swing; import java.awt.*; import jav ...

最新文章

  1. c语言和gtk实现的游戏,Ubuntu下用glade和GTK+开发C语言界面程序(一)
  2. matlab复数方程的根,matlab解一元三次方程,得到的都是复数根。
  3. Spring整合Redis做数据缓存(Windows环境)
  4. Go语言交叉编译工具gox
  5. Solr(二)创建索引和查询索引的基本应用
  6. 通过源码成功启动odoo 10.0
  7. PHP全文索引 讯搜xunsearch
  8. tensorflow have no attribute enable_eager_execution() 问题解决
  9. 全能视频播放器:OmniPlayer for Mac(1.4.6)
  10. 计算机大一总结,大一计算机学习心得总结.doc
  11. 【Python】reset_index函数
  12. python增删改查mysql_python之mysql的增删改查
  13. 获取手机联系人信息(姓名 电话号码)
  14. AntDesign表格中下拉菜单
  15. 在Windows10上通过Virtualbox安装Ubuntu操作系统教程
  16. [math]判断一个点是否在多边形内的方法
  17. linux装机必备软件
  18. YT8511芯片手册 解析|CSDN创作打卡
  19. 神舟g7 ct7vk 显卡性能大幅下降的问题
  20. 面向对象分析与设计——设计模式初探

热门文章

  1. windows10pygame下载安装问题描述
  2. 项目日报模板_知名房企的精细化施工管理,从项目日常到施工图,全方位涵盖...
  3. java常用设计模式详解及应用
  4. python提取停用词_python文本处理 数据挖掘 停用词检索
  5. linux安装文泉驿字体,centos 安装文泉驿字体教程,centos文泉驿字体下载字体(URL:h...
  6. “华为杯”第十八届中国研究生数学建模竞赛历程及感悟
  7. 关于各操作系统对UVC协议支持的说明
  8. uefiboot 文件_UEFI启动文件bootx64.efi的定制
  9. 《Redis开发与运维》学习第六章
  10. 企业全面运营管理沙盘模拟心得_企业经营沙盘模拟心得总结【精选】