java 将 HTML 文本包装在 JScrollPane 内的 JEditorPane 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5174070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wrapping HTML text in a JEditorPane inside a JScrollPane
提问by mjomble
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.
在一个应用程序中,我使用不可编辑的 JEditorPanes 作为一种通用的 UI 小部件,它可以显示一些复杂的内容(HTML 可以解决这个问题)、换行文本行并捕捉鼠标点击。不确定 JEditorPane 是否是一个不错的选择,所以请随意提出替代方案。
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).
所以我把整个东西放在一个 JScrollPane 中(将示例代码中的useScrollPane
变量更改true
为查看此版本)。
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 :)
附注。我对 Swing 没有太多经验,所以如果你在这段代码中看到一些初学者的 WTF,请指出它们:)
回答by camickr
// JPanel panel = new JPanel();
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );
See Scrollable Panelfor the class and an explanation on how the class works.
请参阅类的可滚动面板以及有关该类如何工作的说明。
回答by JB Nizet
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)
如果视口的大小变得小于其视图组件的首选大小,则会出现 JScrollPane 的滚动条。在滚动窗格中设置面板的首选大小,或设置每个编辑器窗格的首选大小:面板的首选大小将由编辑器窗格的首选大小(首选高度和首选宽度的最大值)