【发布时间】:2020-01-05 14:48:46
【问题描述】:
我有一个JLabel,我想包含一个可以跨越多行的文本,并在窗口改变形状时调整大小。
我已经查过了,大多数人似乎都建议将标签文本包装在 HTML 中。然而,这并没有为我创造新的路线。
我的标签位于JPanel 中,我怀疑问题可能是我的面板的边框配置错误,因此标签文本只是继续超出面板边框。
这是标签在状态面板中的外观
面板的设置如下:
private final JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.setBackground(Color.white);
statusPanel.add(latestOrdreLabel);
this.add(statusPanel, new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
那么标签是这样设置的:
private final JLabel latestOrdreLabelResult = new JLabel();
String latestOrdreStatus = getBean().getLatestOrdreStatus(etelOrderInterface.getOrderId());
latestOrdreLabelResult.setText("<html>"+latestOrdreStatus+"</html>");
statusPanel.add(latestOrdreLabelResult);
【问题讨论】:
-
latestOrdreStatus的内容是什么?是否包含<br>tags? -
参见this answer,与@jhamon 链接的问答不同,它不依赖于为每一行手动插入
<br>元素。一般提示:为了尽快获得更好的帮助,edit 添加minimal reproducible example 或Short, Self Contained, Correct Example。 -
顺便说一句 - 请注意,当标签的宽度变大或变小时,noither 链接的答案将自动重新换行文本。对于这样做的东西,请尝试启用自动换行的
JTextArea,或JEditoPane。 -
感谢@AndrewThompson 虽然我之前尝试过这两个选项,但我没有为 Jlabel 设置宽度,并且在查看 JTextarea 时没有启用自动换行。这正是我想要的