修复 JPanel 内 JEditorPane 的大小
这与我的上一个问题非常相似,但是在 JPanel 中执行此操作时,我尝试在 JPanel(固定大小)内显示 JEditorPane,然后显示 HTML 文本字符串。 (这样我就可以使用卡片布局并切换具有不同显示器的面板)。我可以再次显示所有内容,尽管我传递到 EditorPane 的 HTML 字符串内的文本似乎没有被切断并换行到新行。即它只是延伸到屏幕之外。 看来setSize方法与Pane的大小没有关系?
我正在为不同尺寸的屏幕制作这个应用程序,因此重要的是文本换行并适合屏幕尺寸而不是跑掉!当 EditorPane 直接位于 JFrame 而不是 JPanel 内部时,它可以工作。
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);
非常感谢萨姆
This is very similar to my last question, but doing it in a JPanel I am trying to display a JEditorPane inside a JPanel (fixed size) which then displays a string of HTML text. (This is so I can use a cardlayout and switch panels with different displays). I can get everything displaying again, though it seems that the text inside the HTML string I pass into my EditorPane doesn't cut off and wrap to a new line. i.e it just extends off the screen.
It seems that the setSize method has no bearing on the size of the Pane?
I am making this app for different size screen so it is important that the text wraps to new lines and fits the screen size rather than run off! It works when the EditorPane is directly inside a JFrame but not JPanel.
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);
Many thanks Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过在 body 元素中使用样式设置宽度,如 这个例子。
Set the width by using a style in the body element, as in this example.
好吧,您永远不应该在应用程序中使用 setXXXSize 方法。请参阅此线程。
只需要考虑几个因素:
注意:
我知道在某些情况下使用 setXXXmethods 似乎工作得很好。我这样做了很长一段时间才明白这是多么错误。尽量避免这种情况,否则迟早您将需要重构大量代码。
Well, you shold never use setXXXSize methods from your application. See this thread.
Just a few considerations:
A note:
I know that in certain situations use setXXXmethods seems to work fine. I did that for quite a long time before understanding how much it's wrong. Try to avoid that or, soon or late, you will have a lot of code to refactor.