Java 固定窗格内容的大小
我试图在JFrame(固定大小)中显示一个JEditorPane,然后显示一个HTML文本字符串。我可以显示所有内容,尽管我传递到EditorPane的HTML字符串中的文本似乎没有截断并换行。i、 它只是从屏幕上延伸出来。 似乎setSize方法对窗格的大小没有影响 我正在为不同大小的屏幕制作此应用程序,因此重要的是,文本要换行并适合屏幕大小,而不是跑掉Java 固定窗格内容的大小,java,swing,jeditorpane,Java,Swing,Jeditorpane,我试图在JFrame(固定大小)中显示一个JEditorPane,然后显示一个HTML文本字符串。我可以显示所有内容,尽管我传递到EditorPane的HTML字符串中的文本似乎没有截断并换行。i、 它只是从屏幕上延伸出来。 似乎setSize方法对窗格的大小没有影响 我正在为不同大小的屏幕制作此应用程序,因此重要的是,文本要换行并适合屏幕大小,而不是跑掉 JEditorPane pane = new JEditorPane(); pane.setEditable(false);
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);
JEditorPane=new JEditorPane();
pane.setEditable(false);
HTMLDocument htmlDoc=新的HTMLDocument();
HTMLEditorKit editorKit=新的HTMLEditorKit();
pane.setEditorKit(editorKit);
窗格。设置大小(大小);
窗格。设置最小尺寸(尺寸);
窗格。设置最大尺寸(尺寸);
窗格。设置不透明(真);
setText(“不幸的是,当我显示这个字符串时,它太长了,不能换行!”);
添加(窗格,边框布局,中间);
非常感谢Sam为我工作。。。也许你的初始化方式不同
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);
}
}
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.Dimension;
导入javax.swing.JEditorPane;
导入javax.swing.JFrame;
导入javax.swing.text.html.HTMLDocument;
导入javax.swing.text.html.HTMLEditorKit;
公共类JEditorPaneTest扩展JFrame{
公共静态void main(字符串[]args){
JEditorPaneTest t=新的JEditorPaneTest();
t、 设置大小(500500);
容器bg=t.getContentPane();
t、 createJEditorPane(bg,bg.getSize());
t、 setVisible(真);
}
公共void CreateEditorPane(容器背景,尺寸){
JEditorPane=新的JEditorPane();
pane.setEditable(false);
HTMLDocument htmlDoc=新HTMLDocument();
HTMLEditorKit editorKit=新的HTMLEditorKit();
窗格。setEditorKit(editorKit);
窗格。设置大小(大小);
窗格。设置最小尺寸(尺寸);
窗格。设置最大尺寸(尺寸);
窗格。设置不透明(真);
setText(“不幸的是,当我显示这个字符串时,它太长了,不能换行!”);
添加(窗格,边框布局,中间);
}
}
我使用了setPreferredSize而不是setSize的方法,如下代码所示:
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
jEditorPane.setContentType("text/html");
jEditorPane.setText(toolTipText);
jEditorPane.setPreferredSize(new Dimension(800, 600));
这应该适用于任何容器-在我的例子中,我将其与滚动窗格一起使用
嗯 干杯,伙计,好好享受吧!,这是我需要添加的容器位:)我刚刚尝试将其添加到JPanel,现在又遇到了同样的问题!有什么想法吗,谢谢山姆