首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTextPane格式

JTextPane格式
EN

Stack Overflow用户
提问于 2013-03-24 15:09:50
回答 1查看 5.3K关注 0票数 0

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

目前我有这个

代码语言:javascript
复制
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语句。

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

EN

回答 1

Stack Overflow用户

发布于 2013-03-24 16:01:07

也见过hereTextComponentDemo展示了如何应用许多StyleConstants,包括字体大小、样式、对齐和颜色。样式可以直接应用于Document (如initAttributes()中所示),也可以通过StyledEditorKit的操作(查看here )应用。

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

代码语言:javascript
复制
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);
                }
            }
        });
    }
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15600100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档