在我的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