2011-10-26 28 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!