`
Emy
  • 浏览: 67332 次
  • 性别: Icon_minigender_2
  • 来自: 合肥
社区版块
存档分类
最新评论

java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置,很全!!

阅读更多
想做一个文本编辑器的朋友,来这里是找对了!!
下面的代码告诉我们该怎么在文本编辑器中设置字体大小,颜色,加粗,下划线等许多便捷操作~
花了很长的时间找了这么一个资料,真是累煞我了~~!!
差点都要放弃了,最后终于在网络中搜索到了这么一段十分有用、十分有价值的东东!
感谢网络java程序员精英的大公无私,为我们奉献了这么好的代码~~十分感谢!
若转载,请注明我在下面注明的转载出处,十分感谢~~~~
本文转自:http://www.blogjava.net/Swing/archive/2007/12/26/128965.html



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 NewJFrame extends javax.swing.JFrame implements ActionListener {
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) {
  NewJFrame inst = new NewJFrame();
  inst.setVisible(true);
}

public NewJFrame() {
  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);
分享到:
评论

相关推荐

    高性能网页JavaScriptCanvas电子表格系统源码.zip

    功能包含:撤销 & 反撤销,格式刷,文本格式,字体设置,字体大小,字体加粗,斜体字,下划线,删除线,文字颜色,单元格颜色单元格边框字体倾斜边框倾斜背景倾斜合并单元格水平对齐自动换行 冻结单元格单元格函数...

    ItmouseEditor_v1.0文本编辑器(强)!!!

    字体、字号、背景色、文字颜色、加粗、下划线、斜体、删除线、下标、上标、左对齐,右对齐、居中,剪取、复制、粘贴、删除、撤消、恢复、编号列表、项目列表、添加缩进、减少缩进、清除格式、全选、取消全选、对象...

    Qt:记事本Demo

    ),已经实现新建、打开、保存、另存为、生成PDF、打印预览、撤销、恢复、复制、粘贴、剪切、全选、改变字体、颜色、大小、加粗、斜体、下划线、查找、插入当期时间、左右对齐居中、关于作者,工具箱有插入表格、图片...

    计算机应用基础立体化教程-(12).pptx

    其常规设置的内容有字体、字号、字大小、黑体(B)、斜体(I)、加下划线(U)、上标、下标;文字效果有发光、加亮、底纹、带圈、加框、加拼音、清除格式、更改大小写。 计算机应用基础立体化教程-(12)全文共131页,当前为...

    web在线编辑器,类似word

    字体,字号,段落格式,加粗,斜体,下划线,上标,下标,删除线,左对齐,居中,右对齐,全选,剪切,复制,粘贴,撤消,恢复,编号,项目符号,减少缩进量,增加缩进量,替换,插入超级链接,去掉超级链接,插入...

    用ASP做的网页文本编辑器

    字体,字号,段落格式,加粗,斜体,下划线,上标,下标,删除线,左对齐,居中,右对齐,全选,剪切,复制,粘贴,撤消,恢复,编号,项目符号,减少缩进量,增加缩进量,替换,插入超级链接,去掉超级链接,插入...

    阿赛HTML网页在线编辑器 v8版.rar

    功能:不支持图片上传功能,但可插入网络图片之类的,支持样式、字体、字号、颜色、背景颜色、加粗、斜体、下划线、上标字、下标字、编号、对齐、缩进、链接、Word粘贴、Html清理、参考线、自动排版、查找替换、内容...

    asp.net mvc 在线编辑器

    字体,字号,段落格式,加粗,斜体,下划线,上标,下标,删除线,左对齐,居中,右对齐,全选,剪切,复制,粘贴,撤消,恢复,编号,项目符号,减少缩进量,增加缩进量,替换,插入超级链接,去掉超级链接,插入...

    MFC简易RTF编辑器

    用MFC做的简易RTF编辑器,基于RichEdit,模仿Word,现在已经实现Word的基本功能,有设置字号、字体、文本颜色、背景颜色、对齐方式、加粗、斜体、超链接、下划线、删除线、上标、下标、撤销、恢复、复制、粘贴(保留...

    CSS的一些必记属性整理

    Text  color 设置text的颜色 text-align 设置text... font-family 设置字体 font-size 设置字体的大小 font-style 设置字体为斜体还是正常显示 font-weight 加粗字体 Background  background-color 设置背景的颜

    阿赛HTML在线编辑器 v10

    功能:样式、字体、字号、颜色、背景颜色、加粗、斜体、下划线、立体字、发光字、上标字、下标字、古书排版、编号、对齐、缩进、链接、Word粘贴、Html清理、参考线、自动排版、查找替换、内容分页、插入代码、插入...

    HtmlEditor090621.rar

    字体色| 背景色| 加粗| 斜体| 下划线| 删除线| 上标| 下标| 增加缩进| 减少缩进| 靠左对齐| 居中对齐| 靠右对齐| |全选| 复制| 剪切| 粘贴| 撤消| 另存为| 插入超连接| 插入图片| 插入表格| |

    阿赛HTML在线编辑器 v8.zip

    样式、字体、字号、颜色、背景颜色、加粗、斜体、下划线、立体字、发光字、上标字、下标字、古书排版、编号、对齐、缩进、链接、Word粘贴、Html清理、参考线、自动排版、查找替换、内容分页、插入代码、插入引用、...

    阿赛在线HTML编辑器Ver2008

    字体,字号,段落格式,加粗,斜体,下划线,上标,下标,删除线,左对齐,居中,右对齐,全选,剪切,复制,粘贴,撤消,恢复,编号,项目符号,减少缩进量,增加缩进量,替换,插入超级链接,去掉超级链接,插入...

    软件学院秘书部office培训教程.docx

    字体修改:在开始选项卡中,有字体选项,在此处可以进行文字的颜色、字体、大小、加粗(CTRL+B)、斜体(CTRL+I)、添加下划线(CTRL+U)、添加删除线、设置上标下标等设置,对应有各自的按钮。 软件学院秘书部office...

    WoCo-Editor:WoCo记事本是具有Python应用程序编程接口(API)的跨平台文字处理器。 它本身提供了许多功能,这些功能可用于编辑文本

    字体大小选择 使您的文字加粗 使您的文字为斜体 在文字下划线 字体颜色选择器 右对齐文字 对齐文字中心 左对齐文字 从诸如粗体,斜体和带下划线的文本中删除格式。 语音助手 维基百科助手 使用打印文件命令打印文件...

    ios文字编辑器功能

    字体大小设置(Font size); 7. 文字背景颜色(Text background color); 8. 文字覆盖颜色(Text foregroud color); 9. 文字对齐排版(Text alignment); 10. 段落缩进排版(Paragraph Indent/Outdent) 等等...

    c#文档编辑器

    包含全部源码; 该文档编辑器,基本功能: 文件操作: 新建,打开,保存,退出; //支持rtf文件 编辑操作: 复制,剪切,粘贴,全选;...格式操作: 字体,颜色,左对齐,右对齐,居中对齐,加粗,斜体,下划线 附加功能:插入图片

    Android-Rich-text-Editor.zip

    Android-Rich-text-Editor.zip,目前支持的样式: 加粗 斜体 下划线 删除线 ...左对齐 ...字体大小 插入视频 插入网络图片 插入分割线 所有样式均支持导出HTML文件 加载HTML内容并继续编辑或显示

    .NET在线文本编辑器-ItmouseEditor v1.0 源代码

    字体、字号、背景色、文字颜色、加粗、下划线、斜体、删除线、下标、上标、左对齐,右对齐、居中,剪取、复制、粘贴、删除、撤消、恢复、编号列表、项目列表、添加缩进、减少缩进、清除格式、全选、取消全选、对象...

Global site tag (gtag.js) - Google Analytics