将 JEditorPane 中的 HTML 文本包装在 JScrollPane 内

发布于 2024-10-19 21:28:52 字数 2288 浏览 5 评论 0原文

在应用程序中,我使用不可编辑的 JEditorPanes 作为一种通用 UI 小部件,它可以显示一些复杂的内容(HTML 就可以了)、换行文本行并捕获鼠标单击。不确定 JEditorPane 是否是一个不错的选择,因此请随意提出替代方案。

以下示例代码工作得相当好:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Main {

  private static JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    for (int i = 0; i < 3; i++) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      editorPane.setContentType("text/html");

      String text =
        "This is <b>item #" + i + "</b>." +
        " It's got text on it that should be wrapped."
      ;

      editorPane.setText(text);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.gridx = 0;
      constraints.gridy = i;
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 1.0;
      constraints.insets.bottom = 5;

      panel.add(editorPane, constraints);
    }

    return panel;
  }


  public static void main(String[] args) {
    JPanel panel = createPanel();

    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setLocation(200, 200);

    // Change this to switch between examples
    boolean useScrollPane = false;

    if (useScrollPane) {
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setViewportView(panel);
      frame.add(scrollPane);
    }
    else {
      frame.add(panel);
    }

    frame.setVisible(true);
  }

}

并生成以下内容:

但是,我可能有大量这些和可以使用垂直滚动条。

因此,我将整个内容放入 JScrollPane 中(在示例代码中将 useScrollPane 变量更改为 true 以查看此版本)。

如果缩小窗口高度,这会给我一个垂直滚动条,但问题是现在文本不再换行:

所以问题是:如何才能同时获得文本换行和垂直滚动条?

正如你所看到的,我禁用了水平滚动条,但这并没有多大帮助。

附言。我对 Swing 没有太多经验,所以如果你在这段代码中看到一些初学者的 WTF,请指出:)

In an application, I'm using uneditable JEditorPanes as a sort of a generic UI widget that can display somewhat complex content (HTML will do the trick), wrap text lines and catch mouse clicks. Not sure if JEditorPane is a good choice for this, so feel free to suggest alternatives.

The following sample code works fairly well:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Main {

  private static JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    for (int i = 0; i < 3; i++) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      editorPane.setContentType("text/html");

      String text =
        "This is <b>item #" + i + "</b>." +
        " It's got text on it that should be wrapped."
      ;

      editorPane.setText(text);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.gridx = 0;
      constraints.gridy = i;
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 1.0;
      constraints.insets.bottom = 5;

      panel.add(editorPane, constraints);
    }

    return panel;
  }


  public static void main(String[] args) {
    JPanel panel = createPanel();

    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setLocation(200, 200);

    // Change this to switch between examples
    boolean useScrollPane = false;

    if (useScrollPane) {
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setViewportView(panel);
      frame.add(scrollPane);
    }
    else {
      frame.add(panel);
    }

    frame.setVisible(true);
  }

}

and produces the following:

However, I may have a large number of these and could use a vertical scrollbar.

So I put the whole thing in a JScrollPane (change the useScrollPane variable to true in the sample code to see this version).

This gives me a vertical scrollbar if I shrink the window height, but the problem is that now the text is no longer wrapped:

So the question is: how can I get both the text wrapping and the vertical scrollbar?

As you can see, I disabled the horizontal scrollbar, but it didn't help much.

PS. I don't have much experience with Swing, so if you see some beginner WTFs in this code, please point them out :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

毅然前行 2024-10-26 21:28:52
// JPanel panel = new JPanel();
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

请参阅可滚动面板了解该类以及该类如何工作的说明。

// JPanel panel = new JPanel();
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

See Scrollable Panel for the class and an explanation on how the class works.

惜醉颜 2024-10-26 21:28:52

如果视口的大小小于其视图组件的首选大小,则会出现 JScrollPane 的滚动条。设置滚动窗格中面板的首选大小,或设置每个编辑器窗格的首选大小:面板的首选大小将是编辑器窗格的首选大小的组合(首选高度和首选宽度的最大值)

The scrollbars of a JScrollPane appear if the size of the viewport become smaller than the preferred size of its view component. Set the preferred size of the panel in the scroll pane, or set the preferred size of each of the editor panes : the preferred size of the panel will be the composition of the preferred sizes of the editor panes (sum of the preferred height, and max of the preferred widths)

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