2011-10-26 30 views
2

我試圖在JFrame(固定大小)內顯示一個JEditorPane,然後顯示一個HTML文本字符串。我可以顯示所有內容,但看起來,我傳遞給我的EditorPane的HTML字符串中的文本不會被切斷並換行。即它只是從屏幕上延伸出來。 看來,setSize方法對窗格大小沒有影響?修復JEditorPane內容的大小

我正在爲不同大小的屏幕製作此應用程序,所以重要的是文字換行並適合屏幕大小而不是跑掉!

JEditorPane pane = new JEditorPane(); 
    pane.setEditable(false); 
    HTMLDocument htmlDoc = new HTMLDocument() ; 
    HTMLEditorKit editorKit = new HTMLEditorKit() ; 
    pane.setEditorKit (editorKit) ; 
    pane.setSize(size); 
    pane.setMinimumSize(size); 
    pane.setMaximumSize(size); 
    pane.setOpaque(true); 
    pane.setText("<b><font face=\"Arial\" size=\"50\" align=\"center\" > Unfortunately when I display this string it is too long and doesn't wrap to new line!</font></b>"); 
    bg.add(pane, BorderLayout.CENTER); 

非常感謝薩姆

+0

我剛剛嘗試將其添加到JPanel中,現在我又遇到了同樣的問題!任何想法,感謝Sam –

回答

3

對我的作品......也許你不同的初始化呢?

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Dimension; 

import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.text.html.HTMLDocument; 
import javax.swing.text.html.HTMLEditorKit; 

public class JEditorPaneTest extends JFrame { 
    public static void main(String[] args) { 
     JEditorPaneTest t= new JEditorPaneTest(); 
     t.setSize(500,500); 
     Container bg = t.getContentPane(); 
     t.createJEditorPane(bg, bg.getSize()); 
     t.setVisible(true); 
    } 

    public void createJEditorPane(Container bg, Dimension size) { 
     JEditorPane pane = new JEditorPane(); 
     pane.setEditable(false); 
     HTMLDocument htmlDoc = new HTMLDocument(); 
     HTMLEditorKit editorKit = new HTMLEditorKit(); 
     pane.setEditorKit(editorKit); 
     pane.setSize(size); 
     pane.setMinimumSize(size); 
     pane.setMaximumSize(size); 
     pane.setOpaque(true); 
     pane.setText("<b><font face=\"Arial\" size=\"50\" align=\"center\" > Unfortunately when I display this string it is too long and doesn't wrap to new line!</font></b>"); 
     bg.add(pane, BorderLayout.CENTER); 
    } 
} 
+1

乾杯的人,工作的一種享受!,這是我需要添加的容器位:) –

+0

任何想法如何讓它在JPanel內工作? –

0

我所用的方法了setPreferredSize代替的setSize,像下面的代碼:

 JEditorPane jEditorPane = new JEditorPane(); 
     jEditorPane.setEditable(false); 
     jEditorPane.setContentType("text/html"); 
     jEditorPane.setText(toolTipText); 
     jEditorPane.setPreferredSize(new Dimension(800, 600)); 

這應該與任何容器中工作 - 在我的情況,我有一個滾動窗格中使用它。

HTH!