将 JEditorPane 中的 HTML 文本包装在 JScrollPane 内
在应用程序中,我使用不可编辑的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅可滚动面板了解该类以及该类如何工作的说明。
See Scrollable Panel for the class and an explanation on how the class works.
如果视口的大小小于其视图组件的首选大小,则会出现 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)