使用Swing实现简易而不简单的文档编辑器
本文通过Swing来实现文档简易而不简单的文档编辑器,该文档编辑器的功能包括:
- 设置字体样式:粗体,斜体,下划线,可扩展
- 设置字体:宋体,黑体,可扩展
- 设置字号:12,14,18,20,30,40, 可扩展
- 设置字体颜色:红色,蓝色,绿色,黄色,黑色,可扩展
- 设置字体背景颜色:淡蓝,淡黄,淡绿,灰色,无色,可扩展
- 插入图片
- StyledEditorKit.BoldAction() 粗体
- StyledEditorKit.UnderlineAction(); 下划线
- StyledEditorKit.ItalicAction(); 斜体
- StyledEditorKit.FontFamilyAction("宋体", "宋体") 宋体
- tyledEditorKit.FontSizeAction(“12”, 12) 字体大小
- StyledEditorKit.ForegroundAction("Black",Color.black); 字体颜色
JTextPane docTextPane = new JTextPane();
final JComboBox fontModelCb = new JComboBox();// 字体样式下拉框,包括粗体,下划线和斜体
fontModelCb.setModel(new DefaultComboBoxModel(new String[] {
"\u7C97\u4F53", "\u4E0B\u5212\u7EBF", "\u659C\u4F53" }));
fontModelCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
System.out.println("fontModelCb itemStateChanged:"
+ sel.toString());
if (sel.equals("\u7C97\u4F53")) {
ac = new StyledEditorKit.BoldAction();
ac.putValue(Action.NAME, "Bold");
} else if (sel.equals("\u4E0B\u5212\u7EBF")) {
ac = new StyledEditorKit.UnderlineAction();
ac.putValue(Action.NAME, "Underline");
} else {
ac = new StyledEditorKit.ItalicAction();
ac.putValue(Action.NAME, "Italic");
}
fontModelCb.setAction(ac);
}
}); final JComboBox fontTypeCb = new JComboBox(); //设置字体下拉框,包括宋体和黑体
fontTypeCb.setModel(new DefaultComboBoxModel(new String[] {
"\u5B8B\u4F53", "\u9ED1\u4F53" }));
fontTypeCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
System.out.println("fontTypeCb itemStateChanged:"
+ sel.toString());
if (sel.equals("\u5B8B\u4F53")) {
ac = new StyledEditorKit.FontFamilyAction("宋体", "宋体");
} else {
ac = new StyledEditorKit.FontFamilyAction("黑体", "黑体"); }
fontTypeCb.setAction(ac);
}
}); final JComboBox fontSizeCb = new JComboBox();// 设置字体下拉框
fontSizeCb.setModel(new DefaultComboBoxModel(new String[] { "12", "14",
"18", "20", "30", "40" }));
fontSizeCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
fontSizeCb.setAction(new StyledEditorKit.FontSizeAction(sel
.toString(), Integer.parseInt(sel.toString())));
}
}); final JComboBox fontColorCb = new JComboBox(); //设置字体颜色下拉框
fontColorCb.setModel(new DefaultComboBoxModel(new String[] {
"\u9ED1\u8272", "\u7EA2\u8272", "\u84DD\u8272", "\u9EC4\u8272",
"\u7EFF\u8272" }));
fontColorCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
if (sel.equals("\u9ED1\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Black",
Color.black);
} else if (sel.equals("\u7EA2\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Red", Color.red);
} else if (sel.equals("\u84DD\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Green",
Color.green);
} else if (sel.equals("\u9EC4\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Yellow",
Color.yellow);
} else {
ac = new StyledEditorKit.ForegroundAction("Blue",
Color.blue);
}
fontColorCb.setAction(ac);
}
}); final JComboBox fontBgColorCb = new JComboBox(); //设置字体背景下拉框
fontBgColorCb.setModel(new DefaultComboBoxModel(new String[] {
"\u65E0\u8272", "\u7070\u8272", "\u6DE1\u7EA2", "\u6DE1\u9EC4",
"\u6DE1\u84DD", "\u6DE1\u7EFF" }));
fontBgColorCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null; System.out.println("fontBgColorCb:" + sel.toString());
if (sel.equals("\u7070\u8272")) {// 灰色
ac = new DocBackgroundAction("LightBlack", new Color(200,
200, 200));
} else if (sel.equals("\u6DE1\u7EA2")) {// 淡红
ac = new DocBackgroundAction("LightRed", new Color(255,
200, 200));
} else if (sel.equals("\u6DE1\u9EC4")) { // 淡黄
ac = new DocBackgroundAction("LightGreen", new Color(255,
255, 200));
} else if (sel.equals("\u6DE1\u84DD")) {// 淡蓝
ac = new DocBackgroundAction("YLightYellow", new Color(200,
200, 255));
} else if (sel.equals("\u6DE1\u7EFF")) {// 淡绿
ac = new DocBackgroundAction("LightBlue", new Color(200,
255, 200));
} if (ac != null) {
fontBgColorCb.setAction(ac);
}
}
}); JButton insertImageBt = new JButton("\u63D2\u5165\u56FE\u7247"); //插入图片按钮
insertImageBt.setAction(new DocImageAction("插入图片", docTextPane));
public class DocBackgroundAction extends StyledTextAction {
private static final long serialVersionUID = 1L;
public DocBackgroundAction(String nm, Color bg) {
super(nm);
this.bg = bg;
}
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
Color fg = this.bg;
if ((e != null) && (e.getSource() == editor)) {
String s = e.getActionCommand();
try {
fg = Color.decode(s);
} catch (NumberFormatException nfe) {
}
}
if (fg != null) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, fg);
setCharacterAttributes(editor, attr, false);
} else {
UIManager.getLookAndFeel().provideErrorFeedback(editor);
}
}
}
private Color bg;
}
DocImageAction类继承了StyledTextAction,在JtextPane中插入图片的方式其实非常简单使用panel.insertIcon方法即可,如下
public class DocImageAction extends StyledTextAction {
private static final long serialVersionUID = 1L;
public DocImageAction(String nm, JTextPane panl) {
super(nm);
this.panl = panl;
}
public void actionPerformed(ActionEvent e) {
JFileChooser f = new JFileChooser(); // 查找文件
f.showOpenDialog(null);
System.out.println(f.getSelectedFile());
ImageIcon icon = createImageIcon(f.getSelectedFile(), "a cute pig");
JTextPane editor = this.panl;
if (editor != null) {
System.out.println("I am in here");
StyledDocument doc = getStyledDocument(editor);
editor.setCaretPosition(doc.getLength()); // 设置插入位置
editor.insertIcon(icon); // 插入图片
}
}
private JTextPane panl;
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = DocImageAction.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}使用Swing实现简易而不简单的文档编辑器的更多相关文章
- [.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office
打造一个很简单的文档转换器 - 使用组件 Spire.Office [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6024827.html 序 之前,& ...
- 用mkdocs在gitee码云上建立一个简单的文档博客
利用mkdocs建立简单的文档博客 一.概述 MkDocs 是一个用于创建项目文档的 快速, 简单 , 完美华丽 的静态站点生成器. 文档源码使用 Markdown 来撰写, 用一个 YAML 文件作 ...
- [Qt及Qt Quick开发实战精解] 第1章 多文档编辑器
这一章的例子是对<Qt Creator快速人门>基础应用篇各章节知识的综合应用, 也是一个规范的实例程序.之所以说其规范,是因为在这个程序中,我们对菜单什么时候可用/什么时候不可用.关 ...
- 基于slate构建文档编辑器
基于slate构建文档编辑器 slate.js是一个完全可定制的框架,用于构建富文本编辑器,在这里我们使用slate.js构建专注于文档编辑的富文本编辑器. 描述 Github | Editor DE ...
- Linux_文档编辑器_简介
1. vi 2. vim 3. ubuntu 有一个 自己的图形化的 文档编辑器,用起来比较方便: gedit 4. 5.
- PowerDesigner(九)-模型文档编辑器(生成项目文档)(转)
模型文档编辑器 PowerDesigner的模型文档(Model Report)是基于模型的,面向项目的概览文档,提供了灵活,丰富的模型文档编辑界面,实现了设计,修改和输出模型文档的全过程. 模型文 ...
- Web页面引入文档编辑器报风险
Web页面引入文档编辑器会报风险,则需要以下操作: <system.web> <httpRuntime requestValidationMode="2.0" / ...
- 在线HTML文档编辑器使用入门之图片上传与图片管理的实现
在线HTML文档编辑器使用入门之图片上传与图片管理的实现: 官方网址: http://kindeditor.net/demo.php 开发步骤: 1.开发中只需要导入选中的文件(通常在 webapp ...
- 在asp.net core2.1中添加中间件以扩展Swashbuckle.AspNetCore3.0支持简单的文档访问权限控制
Swashbuckle.AspNetCore3.0 介绍 一个使用 ASP.NET Core 构建的 API 的 Swagger 工具.直接从您的路由,控制器和模型生成漂亮的 API 文档,包括用于探 ...
随机推荐
- Ormlite自定义db的位置和自动更新问题
先说说以下为测试代码,有点乱,大家讲究着看.以下例子都是采用的ormlite的框架. 第一步,自定义数据库的位置: 建议一个类DatabaseHelper 继承 OrmLiteSqliteOpenHe ...
- TNetHttpClient支持异步访问(Delphi 10.1 Berlin,红鱼儿的博客)
Delphi 10.1进一步改进自Delphi 10带来的Http访问控件TNetHttpClient,支持异步访问,同时增加ConnectionTimeout及ResponseTimeout两个超时 ...
- 终于懂了:Delphi消息的Result完全是生造出来的,不是Windows消息自带的(Delphi对Windows编程体系的改造越大,学习收获就越大)——消息是否继续传递就看这个Result
Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型, 而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据的含义也不一样. t ...
- uva 620 Cellular Structure
题目连接:620 - Cellular Structure 题目大意:给出一个细胞群, 判断该细胞的可能是由哪一种生长方式的到的, 输出该生长方式的最后一种生长种类, "SIMPLE&quo ...
- 【解决方法】System.IO.FileNotFoundException
错误日志 See the end of this message for details on invoking just-in-time (JIT) debugging instead of thi ...
- C语言中的enum(枚举)使用方法
近期在写数据结构的广义表时候用到了这个概念,在学习C语言的时候没有太注意们这里学一下. 我在网上结合了非常多资料,这里自己总结一下. 首先说.JAVA和C++中都有枚举类型. 假设一个变量你须要几种可 ...
- 学习算法-基数排序(radix sort)卡片分类(card sort) C++数组实现
基数排序称为卡片分类,这是一个比较早的时间越多,排名方法. 现代计算机出现之前,它已被用于排序老式打孔卡. 说下基数排序的思想.前面我有写一个桶式排序,基数排序的思想是桶式排序的推广. 桶式排序:ht ...
- vim: vim快捷键
0. 搜索字符串: 精确匹配查找单词 如果你输入 "/the",你也可能找到 "there". 要找到以 "the" 结尾的单词,可以用:/ ...
- 设置MyEclipse中代码的换行长度
1.打开Preferences -> Java -> Code Style -> Formatter. 2.选择Edit -> Line Wrapping -> Max ...
- Linux - 文件基本操作管理
文件基本操作管理 复制文件和目录 格式: Cp 源文件(文件夹) 新目标文件名(文件夹) 相同目录下,指定文件名. 不同目录下,不需要指定文件名. 参数: –r:递归复制整个目录树. –v:再复制 ...