【问题标题】:Fixing the size of JEditorPane Content修复 JEditorPane 内容的大小
【发布时间】:2011-12-16 02:26:49
【问题描述】:

我试图在 JFrame(固定大小)内显示 JEditorPane,然后显示一串 HTML 文本。我可以显示所有内容,尽管我传递给 EditorPane 的 HTML 字符串中的文本似乎没有切断并换行。即它只是延伸到屏幕之外。 好像setSize方法对Pane的大小没有影响?

我正在为不同尺寸的屏幕制作这个应用程序,因此文本换行并适合屏幕尺寸而不是流失很重要!

    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);

非常感谢山姆

【问题讨论】:

  • 我刚刚尝试将它添加到 JPanel,但我现在又遇到了同样的问题!任何想法,谢谢 Sam

标签: java swing jeditorpane


【解决方案1】:

对我有用......也许你初始化它的方式不同?

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);
    }
}

【讨论】:

  • 干杯人,工作请客!这是我需要添加的容器位:)
【解决方案2】:

我使用了 setPreferredSize 方法而不是 setSize,如下面的代码:

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

这应该适用于任何容器 - 就我而言,我将它与滚动窗格一起使用。

HTH!

【讨论】:

    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2017-12-16
    • 1970-01-01
    • 2016-03-09
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多