多行 JLabels - Java
我想要多行格式的 JLabel
文本,否则文本会太长。我们如何在 Java 中做到这一点?
I want JLabel
text in multiline format otherwise text will be too long. How can we do this in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不介意将标签文本包装在
html
标记中,则当容器的宽度太窄而无法容纳全部内容时,JLabel 会自动将其自动换行。例如,尝试将其添加到 GUI,然后将 GUI 的大小调整为太窄 - 它会换行:If you don't mind wrapping your label text in an
html
tag, the JLabel will automatically word wrap it when its container's width is too narrow to hold it all. For example try adding this to a GUI and then resize the GUI to be too narrow - it will wrap:我建议创建您自己的自定义组件,在换行时模拟 JLabel 样式:
然后您只需调用:
如果您想要,请确保设置行数 (
textNote.setRows(2)
)pack()
正确计算父组件的高度。I recommend creating your own custom component that emulates the JLabel style while wrapping:
Then you just have to call:
Make sure that you set the amount of rows (
textNote.setRows(2)
) if you want topack()
to calculate the height of the parent component correctly.我建议使用 JTextArea 而不是 JLabel
,并且在 JTextArea 上您可以使用方法 .setWrapStyleWord(true) 来更改单词末尾的行。
I suggest to use a JTextArea instead of a JLabel
and on your JTextArea you can use the method .setWrapStyleWord(true) to change line at the end of a word.
可以在 HTML 中使用(基本)CSS。
It is possible to use (basic) CSS in the HTML.
具有自动调整高度的多行标签。
将文本换行到标签中
,谢谢,
吉涅什·戈塔迪亚
MultiLine Label with auto adjust height.
Wrap text in Label
Thanks,
Jignesh Gothadiya