使用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 文档,包括用于探 ...
随机推荐
- fcntl()
fcntl() F_GETFL--------------------------------------------- 将文件状态标志作为函数值返回. 文件状态标志: ...
- c++,初始化列表
类对象的构造顺序是这样的: a.分配内存,调用构造函数时,隐式/显示的初始化各数据成员 b.进入构造函数后在构造函数中执行一般计算 1.初始化类的成员有两种方式,一是使用初始化列表,二是在构造函数体内 ...
- AspNetCore.Hosting
Microsoft.AspNetCore.Hosting 有关Hosting的基础知识 Hosting是一个非常重要,但又很难翻译成中文的概念.翻译成:寄宿,大概能勉强地传达它的意思.我们知道,有一些 ...
- SQL模板和模板实例化
需求:需要得出一个数据源DataTable,我已知SQL和HttpRequest如何,通过SQL模板的方式去实例化匹配HttpRequest中的参数实例化为查询SQL,最后返回DataTable 1. ...
- python模块介绍- xlwt 创建xls文件(excel)
python模块介绍- xlwt 创建xls文件(excel) 2013-06-24磁针石 #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq 37391319 ...
- QString与char*的相互转换
原地址:http://blog.sina.com.cn/s/blog_5c70dfc80100r0nh.html 一.QString转char* QString str; int num=0; s ...
- listbox多选实现上下移动 js版和服务器版
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906021128.as ...
- [Warning] Aborted connection 11203 to db: 'ide' user: 'nuc' host: 'prd01.mb.com' (Got an error writi
PS:一台物理机扯分了3个虚拟机,一个主db,一个主备,一个从备. 切换到0301的时候 Sep 6 09:16:16 prddb0301 mysqld: 130906 9:16:16 [Warn ...
- hdu4709求三角形面积
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- crm使用FetchXml聚合查询
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月08号 */ namespace Net.CRM.FetchXml { using System; using Micr ...