本文转自:http://www.blogjava.net/Swing/archive/2007/12/26/128965.html

想做一个文本编辑器的朋友,来这里是找对了!!

下面的代码告诉我们该怎么在文本编辑器中设置字体大小,颜色,加粗,下划线等许多便捷操作~

花了很长的时间找了这么一个资料,真是累煞我了~~!!

差点都要放弃了,最后终于在网络中搜索到了这么一段十分有用、十分有价值的东东!

感谢网络java程序员精英的大公无私,为我们奉献了这么好的代码~~十分感谢!

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JEditorPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextPane;

import javax.swing.UIManager;

import javax.swing.WindowConstants;

import javax.swing.text.AttributeSet;

import javax.swing.text.DefaultStyledDocument;

import javax.swing.text.Document;

import javax.swing.text.EditorKit;

import javax.swing.text.MutableAttributeSet;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

import javax.swing.text.StyledEditorKit;

public class TestFonts extends javax.swing.JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

private JPanel jp1;

private JButton color;

private JTextPane jep;

private JScrollPane jsp;

private JButton font;

/**

* Auto-generated main method to display this JFrame

*/

public static void main(String[] args) {

TestFonts inst = new TestFonts();

inst.setVisible(true);

}

public TestFonts() {

super();

initGUI();

}

private void initGUI() {

try {

BorderLayout thisLayout = new BorderLayout();

getContentPane().setLayout(thisLayout);

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

{

jp1 = new JPanel();

getContentPane().add(jp1, BorderLayout.NORTH);

{

font = new JButton();

font.addActionListener(this);

jp1.add(font);

font.setText("font");

}

{

color = new JButton();

jp1.add(color);

color.addActionListener(this);

color.setText("color");

}

}

{

jsp = new JScrollPane();

getContentPane().add(jsp, BorderLayout.CENTER);

{

jep = new JTextPane();

jsp.setViewportView(jep);

jep.setDocument(new DefaultStyledDocument());

}

}

pack();

setSize(400, 300);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void setFontSize(JEditorPane editor, int size) {

if (editor != null) {

if ((size > 0) && (size < 512)) {

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setFontSize(attr, size);

setCharacterAttributes(editor, attr, false);

} else {

UIManager.getLookAndFeel().provideErrorFeedback(editor);

}

}

}

public static void setForeground(JEditorPane editor, Color fg) {

if (editor != null) {

if (fg != null) {

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setForeground(attr, fg);

setCharacterAttributes(editor, attr, false);

} else {

UIManager.getLookAndFeel().provideErrorFeedback(editor);

}

}

}

public static final void setCharacterAttributes(JEditorPane editor,

AttributeSet attr, boolean replace) {

int p0 = editor.getSelectionStart();

int p1 = editor.getSelectionEnd();

if (p0 != p1) {

StyledDocument doc = getStyledDocument(editor);

doc.setCharacterAttributes(p0, p1 - p0, attr, replace);

}

StyledEditorKit k = getStyledEditorKit(editor);

MutableAttributeSet inputAttributes = k.getInputAttributes();

if (replace) {

inputAttributes.removeAttributes(inputAttributes);

}

inputAttributes.addAttributes(attr);

}

protected static final StyledDocument getStyledDocument(JEditorPane e) {

Document d = e.getDocument();

if (d instanceof StyledDocument) {

return (StyledDocument) d;

}

throw new IllegalArgumentException("document must be StyledDocument");

}

protected static final StyledEditorKit getStyledEditorKit(JEditorPane e) {

EditorKit k = e.getEditorKit();

if (k instanceof StyledEditorKit) {

return (StyledEditorKit) k;

}

throw new IllegalArgumentException("EditorKit must be StyledEditorKit");

}

public void actionPerformed(ActionEvent e) {

Object obj = e.getSource();

if (obj == font) {

JEditorPane editor = jep;

setFontSize(editor, 20);

}

if (obj == color) {

JEditorPane editor = jep;

setForeground(editor, Color.red);

}

}

}

其他操作如下:

1、对字体的操作

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setFontFamily(attr, family);

setCharacterAttributes(editor, attr, false);

family为字体

2、对字体大小的操作

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setFontSize(attr, size);

setCharacterAttributes(editor, attr, false);

size为字号

3、是否是粗体的操作

StyledEditorKit kit = getStyledEditorKit(editor);

MutableAttributeSet attr = kit.getInputAttributes();

boolean bold = (StyleConstants.isBold(attr)) ? false : true;

SimpleAttributeSet sas = new SimpleAttributeSet();

StyleConstants.setBold(sas, bold);

setCharacterAttributes(editor, sas, false);

4、是否是斜体的操作

StyledEditorKit kit = getStyledEditorKit(editor);

MutableAttributeSet attr = kit.getInputAttributes();

boolean italic = (StyleConstants.isItalic(attr)) ? false : true;

SimpleAttributeSet sas = new SimpleAttributeSet();

StyleConstants.setItalic(sas, italic);

setCharacterAttributes(editor, sas, false);

5、是否有下划线的操作

StyledEditorKit kit = getStyledEditorKit(editor);

MutableAttributeSet attr = kit.getInputAttributes();

boolean underline = (StyleConstants.isUnderline(attr)) ? false

: true;

SimpleAttributeSet sas = new SimpleAttributeSet();

StyleConstants.setUnderline(sas, underline);

setCharacterAttributes(editor, sas, false);

6、左中右对齐的处理

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setAlignment(attr, a);

setParagraphAttributes(editor, attr, false);

public static final void setParagraphAttributes(JEditorPane editor,

AttributeSet attr, boolean replace) {

int p0 = editor.getSelectionStart();

int p1 = editor.getSelectionEnd();

StyledDocument doc = getStyledDocument(editor);

doc.setParagraphAttributes(p0, p1 - p0, attr, replace);

}

a:0:左,1:中,2:右

7、文本字体颜色的设置

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setForeground(attr, fg);

setCharacterAttributes(editor, attr, false);

fg:为color

8、文本背景颜色的设置

MutableAttributeSet attr = new SimpleAttributeSet();

StyleConstants.setBackground(attr, bg);

setCharacterAttributes(editor, attr, false);

java font 字体加粗_java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置...相关推荐

  1. 学习整理Fabric.js 实现文本文字加粗、下划线、斜体、竖排、字体对齐代码

    原图 效果图 代码 index.html <!DOCTYPE HTML> <html> <head><meta charset="utf-8&quo ...

  2. div html 下边加横线_css字体下边横线 html超链接更改颜色和去掉下划线

    怎样用CSS样式在文字下面加下划线 一般有两种方法: 一.通过CSS下划线代码:text-decoration:underline来设置文字下划线. 实例演示如下: 实例代码如下: 此时页面效果如下: ...

  3. pythonxlwt行居中_python3-xlwt-Excel设置(字体大小、颜色、对齐方式、换行、合并单元格、边框、背景、下划线、斜体、加粗)...

    # coding:utf-8 import patterns as patterns import xlwt import time i = 0 book = xlwt.Workbook(encodi ...

  4. HTML常用字体样式设置(加粗、下划线、斜体)

    有的时候,仅仅是价格要求,前面的符号小一点,后面的数字大一点,就没必要用SpannableString.用HTML简单处理即可 效果图: 代码实现: //加粗:<strong>222< ...

  5. java font字体有哪些_java字体有哪些

    java字体有:1.Serif是有衬线:2.[Sans-serif]是无衬线:3.Monospaced是等宽:4.Dialog是对话框:5.DialogInput是对话框输入. Java 使用逻辑字体 ...

  6. java 字体有哪些_java字体有哪些

    java字体有:1.Serif是有衬线:2.[Sans-serif]是无衬线:3.Monospaced是等宽:4.Dialog是对话框:5.DialogInput是对话框输入. Java 使用逻辑字体 ...

  7. java ios rsa解密乱码_java与IOS之间的RSA加解密

    很简单的一个需求,ipad端给密码RSA加密,传到java后台,解密.RSA加密算法是基于一个密钥对的,分为公钥和私钥,一般情况公钥加密,私钥解密,但也可私钥加密,公钥解密.还可以验签,就是先用私钥对 ...

  8. php语言字体下划线,css中怎么设置字体下划线

    css中设置字体下划线的方法:1.使用"text-decoration:underline;"设置下划线样式:2.通过"border-bottom"属性设置下划 ...

  9. java 从xml读变量_java – 如何从XML属性文件加载变量?

    我目前正在从Ant中的XML文件加载属性.但是,我想在for循环中执行当前的ant任务,同时每次为同一组属性加载新的XML属性文件. 我知道ant-contrib的var任务的存在,它允许我覆盖属性. ...

最新文章

  1. 在maven项目中使用Junit进行单元测试
  2. zookeeper 分布式锁原理
  3. InChatter系统开源聊天模块前奏曲
  4. Apache SkyWalking 为.NET Core带来开箱即用的分布式追踪和应用性能监控
  5. 设计模式的Java 8 Lambda表达式–命令设计模式
  6. 盘点 Greenplum 数据库的十大特点
  7. 微软MSDN订阅用户已可提前手工下载Windows 10安装包
  8. 尚医通:开局项目介绍
  9. wso2 mysql_WSO2
  10. 中医药天池大数据竞赛——中医文献问题生成挑战(二)
  11. Linux(Ubuntu)中对音频批量转换格式MP3转WAV/PCM转WAV
  12. 配置全局使用的scss样式公用样式函数(后台框架整体颜色改变)
  13. RN style的常用布局页面属性
  14. 工业机器人产业链展板_赵德明调研六大新产业十大产业链发展情况
  15. does not specify a Swift version and none of the targets (`packager`) integrating it have the `SWIFT
  16. python--Flask学习(七)--利用Flask中的werkzeug.security模块加密
  17. 如何看linux系统中有没有安装cuda,Linux系统CUDA10.2+CUDNN安装教程
  18. android 震动的实现,android实现震动和声音
  19. Cesium的坐标拾取详解
  20. 靠智慧教育“飞升”,科大讯飞前路漫漫

热门文章

  1. 程序员是什么又代表这多少角色?你想过吗?
  2. 机器学习模型设计准则:“无免费午餐”定理和“奥卡姆剃刀”原则
  3. python画红色等边三角形面积公式_等边三角形面积公式?
  4. excel2016 for mac 二维表转一维表
  5. Qt美化之基础控件美化
  6. 英文文献翻译格式整理器
  7. 网络硬件三剑客集线器交换机路由器
  8. java过滤ios表情,JS前端去掉emoji表情和Java后台处理emoji表情方法
  9. 苹果cms怎么更换模板logo详细教程
  10. vtk中画几何图形存储为vtk文件并在窗口显示