我正在尝试设计一个更大的 GUI 的一部分,它将格式化一些文本以使其看起来不错。它应该能够以很多有趣的方式玩文字。添加边框,下划线,即任何我想对文本做的装饰目的。JTextPane 是实现此目的的方法吗?
在下面的示例中,我想decorateTextPane()
显示两行具有不同字体的文本。但是每当我打电话时,textPane.setFont()
我都会更改窗格中所有现有文本的字体。
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.
}
}