在我的Swing应用程序中,用户将样式文本输入到使用RTFEditorKit(HTML也是可能性)的JTextPane中。将RTF/HTML字符串绘制成自定义的swing组件
然后,我需要在自定义组件中的特定坐标上渲染许多这些样式的笔记。
我认为View.paint方法在这里会很有帮助,但我无法创建一个可用的View对象。
我有以下方法:
public View createView() throws IOException, BadLocationException {
RTFEditorKit kit = new RTFEditorKit();
final Document document = kit.createDefaultDocument();
kit.read(new ByteArrayInputStream(text.getBytes("UTF-8")), document, 0);
return kit.getViewFactory().create(document.getDefaultRootElement());
}
这将返回具有以下属性的javax.swing.text.BoxView中:
majorAxis = 1
majorSpan = 0
minorSpan = 0
majorReqValid = false
minorReqValid = false
majorRequest = null
minorRequest = null
majorAllocValid = false
majorOffsets = {int[0]@2321}
majorSpans = {int[0]@2322}
minorAllocValid = false
minorOffsets = {int[0]@2323}
minorSpans = {int[0]@2324}
tempRect = {[email protected]}"java.awt.Rectangle[x=0,y=0,width=0,height=0]"
children = {javax.swing.text.View[1]@2326}
nchildren = 0
left = 0
right = 0
top = 0
bottom = 0
childAlloc = {[email protected]}"java.awt.Rectangle[x=0,y=0,width=0,height=0]"
parent = null
elem = {[email protected]}"BranchElement(section) 0,35\n"
注意,父= null并且nchildren = 0。这意味着没有什么真正有用的。我可以通过调用JTextPane.getUI().paint
来破解一些东西,但文本窗格需要可见,而这种感觉就像是错误的方式。
有什么方法可以在不渲染实际的JTextPane的情况下获得RTF内容的可视化表示?
感谢您的链接,这看起来更适合拍摄已在屏幕上可见的组件快照。我想使用“加盖”方法在自定义组件的各个位置呈现样式化文本块。 – 2009-10-05 16:05:45
我不知道什么是冲压评估。也许我不明白这个问题,但想法是你可以从一个不可见的组件创建一个图像。然后,您可以将子图像呈现在自定义组件上。关键是你不需要一个可见的组件来创建图像。 – camickr 2009-10-05 17:43:24