如何修复 JScrollPane 中 JEditorPane 的高度?
How do I fix the height of a JEditorPane inside a JScrollPane?
我想将 JScrollPane 中的 JEditorPane 的高度限制为最大值,但仍允许宽度扩展以适应内容。
我的理由是我想要一行文本来嵌入组件。我真的想要类似带有可嵌入组件的 JTextField 的东西,但由于 JTextField 不能这样做,我正在使用 JEditorPane(JTextPane 也可以)。
我正在尝试模仿桌面 Swing 应用程序中的 Whosebug 标记行为。因此,我将在 JEditorPane 中嵌入可点击的 "tags",它们将全部显示在一行中,就像 SO 一样。
就目前而言,JEditorPane 会垂直扩展,但不会使用此 SSCCE 水平扩展:
import javax.swing.*;
import java.awt.BorderLayout;
public class Tags {
/*
* This is the method I want to modify to implement this behavior:
*/
public static JComponent buildTagsComponent() {
JScrollPane scrollPane = new JScrollPane();
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
return scrollPane;
}
/*
* The remainder of this code is just to make the example complete.
*/
public static JFrame buildFrame() {
JFrame frame = new JFrame("Tags example");
JPanel panel = new JPanel();
JPanel tagsPanel = new JPanel();
JLabel tagsLabel = new JLabel("Tags:");
JLabel mainContent = new JLabel("Main Content Goes Here");
tagsPanel.add(tagsLabel);
tagsPanel.add(buildTagsComponent());
panel.setLayout(new BorderLayout());
panel.add(tagsPanel,BorderLayout.SOUTH);
panel.add(mainContent,BorderLayout.CENTER);
frame.setContentPane(panel);
return frame;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
JFrame frame = buildFrame();
frame.pack();
frame.setVisible(true);
}
}
);
}
}
另请注意,我计划禁用滚动条并需要使用手势滚动或使用光标键移动光标位置。当我为滚动条策略添加 "NEVER" 时,这只会让事情根本无法滚动。我现在不是在寻找滚动问题的解决方案,我只是希望答案考虑到我将水平和垂直的滚动条策略设置为 NEVER。
我已经尝试将高度和宽度(最小值、首选值和最大值)分别显式设置为 12 和 Integer.MAX_VALUE
。
更新
经过一些研究,我认为我正在寻找的解决方案与自动换行有关。我希望 JEditorPane 在没有换行符(换行符)时不换行段落。
在 JScrollPane 初始化中试试这个:
JScrollPane scrollBar = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
目前,由于布局管理器,编辑器窗格不会水平调整大小。 JPanel
的默认值是 FlowLayout
,它按固定的首选大小调整组件大小。
试试 BoxLayout
:
JPanel tagsPanel = new JPanel();
tagsPanel.setLayout(new BoxLayout(tagsPanel, BoxLayout.LINE_AXIS));
// then add components
额外提示:要改善面板的外观,您可以添加不可见的组件(将 space 放在其他组件之间),and/or 空边框(与其他组件创建边距)组件:
tagsPanel.add(tagsLabel);
tagsPanel.add(Box.createRigidArea(new Dimension(10,0)));
tagsPanel.add(buildTagsComponent());
tagsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
关于换行的问题(在JEditorPane
中不能轻易关闭,不像JTextArea
),提出了一个解决方案here (referenced from this post)。
基本上您必须扩展 StyledEditorKit
并将其设置为您的 JEditorPane
。
关于 JScrollPane
的行为,也许解决方案是使用 AS_NEEDED
默认设置,但 customize the UI of the scroll bars 使它们的大小等于零。这样你就可以在没有滚动条的情况下进行滚动。
我想将 JScrollPane 中的 JEditorPane 的高度限制为最大值,但仍允许宽度扩展以适应内容。
我的理由是我想要一行文本来嵌入组件。我真的想要类似带有可嵌入组件的 JTextField 的东西,但由于 JTextField 不能这样做,我正在使用 JEditorPane(JTextPane 也可以)。
我正在尝试模仿桌面 Swing 应用程序中的 Whosebug 标记行为。因此,我将在 JEditorPane 中嵌入可点击的 "tags",它们将全部显示在一行中,就像 SO 一样。
就目前而言,JEditorPane 会垂直扩展,但不会使用此 SSCCE 水平扩展:
import javax.swing.*;
import java.awt.BorderLayout;
public class Tags {
/*
* This is the method I want to modify to implement this behavior:
*/
public static JComponent buildTagsComponent() {
JScrollPane scrollPane = new JScrollPane();
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
return scrollPane;
}
/*
* The remainder of this code is just to make the example complete.
*/
public static JFrame buildFrame() {
JFrame frame = new JFrame("Tags example");
JPanel panel = new JPanel();
JPanel tagsPanel = new JPanel();
JLabel tagsLabel = new JLabel("Tags:");
JLabel mainContent = new JLabel("Main Content Goes Here");
tagsPanel.add(tagsLabel);
tagsPanel.add(buildTagsComponent());
panel.setLayout(new BorderLayout());
panel.add(tagsPanel,BorderLayout.SOUTH);
panel.add(mainContent,BorderLayout.CENTER);
frame.setContentPane(panel);
return frame;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
JFrame frame = buildFrame();
frame.pack();
frame.setVisible(true);
}
}
);
}
}
另请注意,我计划禁用滚动条并需要使用手势滚动或使用光标键移动光标位置。当我为滚动条策略添加 "NEVER" 时,这只会让事情根本无法滚动。我现在不是在寻找滚动问题的解决方案,我只是希望答案考虑到我将水平和垂直的滚动条策略设置为 NEVER。
我已经尝试将高度和宽度(最小值、首选值和最大值)分别显式设置为 12 和 Integer.MAX_VALUE
。
更新
经过一些研究,我认为我正在寻找的解决方案与自动换行有关。我希望 JEditorPane 在没有换行符(换行符)时不换行段落。
在 JScrollPane 初始化中试试这个:
JScrollPane scrollBar = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
目前,由于布局管理器,编辑器窗格不会水平调整大小。 JPanel
的默认值是 FlowLayout
,它按固定的首选大小调整组件大小。
试试 BoxLayout
:
JPanel tagsPanel = new JPanel();
tagsPanel.setLayout(new BoxLayout(tagsPanel, BoxLayout.LINE_AXIS));
// then add components
额外提示:要改善面板的外观,您可以添加不可见的组件(将 space 放在其他组件之间),and/or 空边框(与其他组件创建边距)组件:
tagsPanel.add(tagsLabel);
tagsPanel.add(Box.createRigidArea(new Dimension(10,0)));
tagsPanel.add(buildTagsComponent());
tagsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
关于换行的问题(在
JEditorPane
中不能轻易关闭,不像JTextArea
),提出了一个解决方案here (referenced from this post)。
基本上您必须扩展 StyledEditorKit
并将其设置为您的 JEditorPane
。
关于
JScrollPane
的行为,也许解决方案是使用 AS_NEEDED
默认设置,但 customize the UI of the scroll bars 使它们的大小等于零。这样你就可以在没有滚动条的情况下进行滚动。