Java 如何在 JLabel 中自动换行文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26420428/
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
How to word wrap text in JLabel?
提问by Nadeem
How can text like "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" which exceeds the width of the JLabel be wrapped? I have tried enclosing the text into html tags but no luck. Please give your suggestions.
超过 JLabel 宽度的“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”之类的文字如何包装?我曾尝试将文本包含在 html 标签中,但没有成功。请提出您的建议。
采纳答案by splungebob
A common approach is to not use a JLabel
and instead use a JTextArea
with word-wrap and line-wrap turned on. You could then decorate the JTextArea to make it look like a JLabel (border, background color, etc.). [Edited to include line-wrap for completeness per DSquare's comment]
一种常见的方法是不使用 aJLabel
而是使用 aJTextArea
并打开自动换行和换行。然后,您可以装饰 JTextArea 以使其看起来像 JLabel(边框、背景颜色等)。[根据 DSquare 的评论编辑以包含换行以确保完整性]
Another approach is to use HTML in your label, as seen here. The caveats there are
另一种方法是在标签中使用 HTML,如下所示。有注意事项
You may have to take care of certain characters that HTML may interpret/convert from plain text
Calling
myLabel.getText()
will now contain HTML (with possibly escaped and/or converted characters due to #1
您可能需要处理 HTML 可能从纯文本解释/转换的某些字符
调用
myLabel.getText()
现在将包含 HTML(由于 #1 可能带有转义和/或转换的字符
EDIT:Here's an example for the JTextArea approach:
编辑:这是 JTextArea 方法的示例:
import javax.swing.*;
public class JLabelLongTextDemo implements Runnable
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new JLabelLongTextDemo());
}
public void run()
{
JLabel label = new JLabel("Hello");
String text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
// String text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " +
// "quick brown fox jumped over the lazy dog.";
JTextArea textArea = new JTextArea(2, 20);
textArea.setText(text);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(UIManager.getColor("Label.background"));
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label, BorderLayout.NORTH);
frame.getContentPane().add(textArea, BorderLayout.CENTER);
frame.setSize(100,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
回答by Ronald
Use JLabel with the html properties to justify the text.
使用带有 html 属性的 JLabel 来对齐文本。
JLabel txtTitulo = new JLabel(
String.format("<html><body style=\"text-align: justify; text-justify: inter-word;\">%s</body></html>","Long text example aaaaaa bbbbbb aaaaaaaaaaaaaaaaaaaa.");
);
Doc: https://www.w3schools.com/cssref/css3_pr_text-justify.asp
文档:https: //www.w3schools.com/cssref/css3_pr_text-justify.asp