如何创建一个简单的 Paragraph 类
我一直在尝试创建一个可以执行以下操作的类:
• 设置:字体、对齐方式(左、中、右、两端对齐)
• 将文本附加
到文档的有效方法。
文本不需要可选择或可编辑。 我必须能够覆盖文本的绘制/渲染。
我发现 JDK JTextComponent
类很难有效使用,因为这是我到目前为止所拥有的,但它与我想要实现的目标相去甚远:
public class Paragraph extends JTextPane{
public Paragraph(){
this.setFont(Fonts.PARAGRAPH);
this.setOpaque(false);
}
// ridiculously slow
public void append(String s) {
SimpleAttributeSet def = new SimpleAttributeSet();
StyleConstants.setForeground(def, Colors.PARAGRAPH);
Document d = getDocument();
try {
d.insertString(d.getLength(), s, def);
} catch (BadLocationException ble) {
}
}
}
问题:是否有任何库可以节省我重新发明轮子的时间?
如果没有,我该如何扩展 JDK 实现?谢谢
I've been trying to create a class which can do the following:
• Set: Font, Alignment (left, center, right, justified)
• An efficient way to append
text to the document.
The text does not need to be selectable or editable.
I have to be able to override the painting / rendering of the text.
I find that the JDK JTextComponent
classes are difficult to use efficiently, as this is what I have so far but it is far from what I'm trying to achieve:
public class Paragraph extends JTextPane{
public Paragraph(){
this.setFont(Fonts.PARAGRAPH);
this.setOpaque(false);
}
// ridiculously slow
public void append(String s) {
SimpleAttributeSet def = new SimpleAttributeSet();
StyleConstants.setForeground(def, Colors.PARAGRAPH);
Document d = getDocument();
try {
d.insertString(d.getLength(), s, def);
} catch (BadLocationException ble) {
}
}
}
Question: Are there any libraries which could save me time re-inventing the wheel?
If not, how can I go about extending the JDK implementations? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的文档应该是 StyledDocument 实例。然后只需使用 Document 实例的 setParagraphAttributes() 方法即可。
对于多个附加,请使用单独的文档(未设置为 JTextPane 实例。
使用该工具包创建一个新的空 Document 实例。调用所有附加,然后将 setDocument(theDocInstance) 设置为 JTextPane。
Your Document should be StyledDocument instance. Then just use setParagraphAttributes() method of the Document instance.
For multiple appends use a separate document (not set to the JTextPane instance.
Use the kit ot create a new empty Document instance. Call all your appends and then setDocument(theDocInstance) to the JTextPane.