修复 JPanel 内 JEditorPane 的大小

发布于 2024-12-12 02:29:17 字数 912 浏览 0 评论 0原文

这与我的上一个问题非常相似,但是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2024-12-19 02:29:17
..."Unfortunately when I display this string it is too long and doesn't wrap to new line!"

通过在 body 元素中使用样式设置宽度,如 这个例子

..."Unfortunately when I display this string it is too long and doesn't wrap to new line!"

Set the width by using a style in the body element, as in this example.

暮色兮凉城 2024-12-19 02:29:17

好吧,您永远不应该在应用程序中使用 setXXXSize 方法。请参阅此线程

只需要考虑几个因素:

  1. 这取决于 LayoutManager 的定位并确定组件的正确大小,因此您不应明确指定它。
  2. 并非所有 LayoutManager 都会处理组件的 PreferredSize 值。
  3. 即使 LayoutManager 处理首选尺寸,显式使用这些方法也是错误的。
  4. 如果您想显式使用尺寸,则仅对包含所有组件的父框架执行此操作。
  5. 如果 LayoutManager 没有以正确的方式放置组件,请使用不同的 LayoutManager 或实现您自己的。

注意:

我知道在某些情况下使用 setXXXmethods 似乎工作得很好。我这样做了很长一段时间才明白这是多么错误。尽量避免这种情况,否则迟早您将需要重构大量代码。

Well, you shold never use setXXXSize methods from your application. See this thread.

Just a few considerations:

  1. It's up to the LayoutManager positioning and establish the right size of a component, so you shouldn't specify it explicitely
  2. Not all LayoutManagers take care of preferredSize values of the components.
  3. Even if the LayoutManager takes care of preferred size, it's wrong to explicitely use those methods
  4. If you want to explicitely use a size, do it only with the parent frame containing all components.
  5. If a LayoutManager doesn't lay components in the right way, use a different LayoutManager or implement your own.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文