JTextPane格式化 [英] JTextPane formatting

查看:156
本文介绍了JTextPane格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTextPane,我想添加行,根据他们的内容,他们有不同的格式。

I have a JTextPane where I want to add lines and depending on their content have them have a different formatting.

目前我有这个

StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);

Style styleBold = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(styleBold, true);
StyleConstants.setFontSize(styleBold, 18);

Style styleNorm = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setFontSize(styleNorm, 15);

for (int i = 0; i < temp.size(); i++) {
    String tmp = temp.get(i);
    if (tmp.substring(0, 2).equals(COMMENT_PREFIX)) {
        String addThis = " - " + tmp.substring(2);

        try {
            document.insertString(document.getLength(), addThis,
                    styleNorm);
        } //CATCH
    } else if (tmp.substring(0, 2).equals(VERSION_PREFIX)) {
        Date d = new Date(System.currentTimeMillis());
        String addThis = "Version: " + tmp.substring(2) + " - "
                + d.toString();
        try {
            document.insertString(document.getLength(), addThis,
                    styleBold);
        } //CATCH
    }
    try {
        document.insertString(document.getLength(), "\n", styleNorm);
    } //CATCH
}

我拿出了catch语句来减少代码大小。

I took out the catch statements to reduce code size.

但是,这会使用 styleNorm 格式化我的整个文本。这是因为它是最后一个名为 Style 并且它们会相互覆盖吗?如果是这样,我该如何解决这个问题?

However, this formats my entire text with the styleNorm. Is this because it's the last called Style and they overwrite eachother? If so, how do I fix this?

推荐答案

还见此处 TextComponentDemo 显示如何应用多个 StyleConstants ,包括字体大小,样式,对齐方式和颜色。样式可以直接应用于 Document ,如 initAttributes()中所示,或者通过<$的操作c $ c> StyledEditorKit ,见此处

Also seen here, TextComponentDemo shows how to apply a number of StyleConstants, including font size, style, alignment and color. The styles may applied either directly to the Document, as shown in initAttributes(), or via the actions of StyledEditorKit, seen here.

附录:下面的示例使用 SimpleAttributeSet 创建三个相关样式。请注意 highAlert 会改变颜色,但会保留从 boldBlue 继承的粗体属性。

Addendum: The example below creates three related styles using SimpleAttributeSet. Note that highAlert alters the color but retains the bold attribute inherited from boldBlue.

import java.awt.Color;
import java.awt.EventQueue;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * @see https://stackoverflow.com/a/15600689/230513
 */
public class Test {

    private void display() throws BadLocationException {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String s = new Date().toString();
        JTextPane jtp = new JTextPane();
        StyledDocument doc = (StyledDocument) jtp.getDocument();

        SimpleAttributeSet normal = new SimpleAttributeSet();
        StyleConstants.setFontFamily(normal, "SansSerif");
        StyleConstants.setFontSize(normal, 16);

        SimpleAttributeSet boldBlue = new SimpleAttributeSet(normal);
        StyleConstants.setBold(boldBlue, true);
        StyleConstants.setForeground(boldBlue, Color.blue);

        SimpleAttributeSet highAlert = new SimpleAttributeSet(boldBlue);
        StyleConstants.setFontSize(highAlert, 18);
        StyleConstants.setItalic(highAlert, true);
        StyleConstants.setForeground(highAlert, Color.red);

        doc.insertString(doc.getLength(), s + "\n", normal);
        doc.insertString(doc.getLength(), s + "\n", boldBlue);
        doc.insertString(doc.getLength(), s + "\n", highAlert);
        f.add(jtp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new Test().display();
                } catch (BadLocationException ex) {
                    ex.printStackTrace(System.err);
                }
            }
        });
    }
}

这篇关于JTextPane格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆