I'm trying to design a part of a bigger GUI that will format some text to look good. It should be able to play with text in a lot of funny ways. Adding borders, underlining, i.e anything I could want to do with text for decorative purposes. Is JTextPane the way to go for this purpose?

In my example below I want decorateTextPane() to display two lines of text with different font. But whenever I call textPane.setFont() I change the font of all existing text in the pane.

public class OuterClass {
    InnerClass inner = new InnerClass();

    private class InnerClass {
        private JTextPane textPane = new JTextPane()

        public InnerClass() {
            StyledDocument doc = textPane.getStyledDocument();
            SimpleAttributeSet center = new SimpleAttributeSet();
            StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
            doc.setParagraphAttributes(0, doc.getLength(), center, false);
        }
    }

    public void decorateTextPane() {
        inner.textPane.setFont(New Font("Times New Roman", Font.PLAIN, 15));
        inner.textPane.setText("First string");
        inner.textPane.setFont(New Font("Calibri", Font.PLAIN, 15));
        inner.textPane.append("\nSecond string"); //my textPane class defines an append method.
    }
}
有帮助吗?

解决方案 2

Defining the useFont() methods and using them before adding the text that should use its specified font is my current approach. It's working and I'm content, but if anyone have any better answers I'm still very interested :-)

public class OuterClass {
InnerClass inner = new InnerClass();

private class InnerClass {
    private JTextPane textPane = new JTextPane();
    private Font firstFont = new Font("Times New Roman", Font.PLAIN, 18);
    private Font secondFont = new Font("Calibri", Font.PLAIN, 14);
    StyledDocument doc = textPane.getStyledDocument();
    SimpleAttributeSet aSet = new SimpleAttributeSet();

    public InnerClass() {
        StyledDocument doc = textPane.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
    }

    public void useFont1() {
        StyleConstants.setFontFamily(aSet, firstFont.getFamily());
        StyleConstants.setFontSize(aSet, firstFont.getSize());
        doc.setParagraphAttributes(0, 0, aSet, false);
    }

    public void useFont2() {
        StyleConstants.setFontFamily(aSet, secondFont.getFamily());
        StyleConstants.setFontSize(aSet, secondFont.getSize());
        doc.setParagraphAttributes(doc.getLength(), 0, aSet, false);
    }
}

public void decorateTextPane() {
    inner.useFont1();
    inner.textPane.setText("First string");
    inner.useFont2();
    inner.textPane.append("\nSecond string"); //my textPane class defines an append method.
}

}

其他提示

You can use a JEditorPane instead of a JTextPane because it is well-suited for the task you want to accomplish. :)

From the answer Java JTextPane Change Font of Selected Text

There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument(). You want to cast that to a StyledDocument if you can, and then you can do things like setCharacterAttributes to a given run of characters.

Examples : http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top