Fixing the size of JEditorPane Content
我正在尝试在JFrame(固定大小)中显示JEditorPane,然后显示一串HTML文本。我可以显示所有内容,尽管传递给EditorPane的HTML字符串中的文本似乎没有被截断并换行。即它只是延伸到屏幕之外。
看来setSize方法与Pane的大小没有关系?
我正在为不同大小的屏幕制作此应用程序,因此,将文本换行并适合屏幕大小而不是流失是很重要的!
1 2 3 4 5 6 7 8 9 10 11 | 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("<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>"); bg.add(pane, BorderLayout.CENTER); |
非常感谢Sam
为我工作...也许您以不同的方式初始化了它?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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("<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>"); bg.add(pane, BorderLayout.CENTER); } } |
我使用方法setPreferredSize而不是setSize,如下面的代码所示:
1 2 3 4 5 | JEditorPane jEditorPane = new JEditorPane(); jEditorPane.setEditable(false); jEditorPane.setContentType("text/html"); jEditorPane.setText(toolTipText); jEditorPane.setPreferredSize(new Dimension(800, 600)); |
这适用于任何容器-在我的情况下,我将其与滚动窗格一起使用。
HTH!