Java JLabel 中的多行文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/685521/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:54:32  来源:igfitidea点击:

Multiline text in JLabel

javajlabel

提问by Jessy

How can I make the text of a JLabel extend onto another line?

如何使 JLabel 的文本扩展到另一行?

采纳答案by tddmonkey

You can do it by putting HTML in the code, so:

您可以通过在代码中放入 HTML 来实现,因此:

JFrame frame = new JFrame();
frame.setLayout(new GridLayout());
JLabel label = new JLabel("<html>First line<br>Second line</html>");
frame.add(label);
frame.pack();
frame.setVisible(true);

回答by Andrew Thompson

It is possible to use (basic) CSSin the HTML.

可以在 HTML 中使用(基本)CSS



This question was linked from Multiline JLabels - Java.

这个问题是从Multiline JLabels-Java链接的。

回答by tObi

if you want your jLabel Text to resize automaticly for example in a stretchable gridbaglayout its enough just to put its text in html tags like so:

如果您希望 jLabel 文本自动调整大小,例如在可拉伸的 gridbaglayout 中,只需将其文本放在 html 标签中即可,如下所示:

JLabel label = new JLabel("<html>First line and maybe second line</html>");

回答by Web Think

Type the content (i.e., the "text" property field) inside a <html></html>tag. So you can use <br>or<P>to insert a newline.

<html></html>标签内键入内容(即“文本”属性字段)。因此您可以使用<br><P>插入换行符。

For example:

例如:

String labelContent = "<html>Twinkle, twinkle, little star,<BR>How I wonder what you are.<BR>Up above the world so high,<BR>Like a diamond in the sky.</html>";

It will display as follows:

它将显示如下:

Twinkle, twinkle, little star,
How I wonder what you are.
Up above the world so high,
Like a diamond in the sky.

一闪一闪,小星星,
我怎么想知道你是什么。
在如此高的世界之上,
就像天空中的钻石。

回答by Nits

why you are giving complex things...you can just do it by putting "\n" instead of html tags

为什么你要给出复杂的东西......你可以通过放置“\n”而不是html标签来做到这一点

回答by user2287966

You can also use a JXLabelfrom the SwingX library.

你也可以使用一个JXLabel该SwingX库

JXLabel multiline = new JXLabel("this is a \nMultiline Text");
multiline.setLineWrap(true);

回答by Harun ERGUL

String labelText ="<html>Name :"+name+"<br>Surname :"+surname+"<br>Gender :"+gender+"</html>";
JLabel label=new JLabel(labelText);
label.setVisible(true);
label.setBounds(10, 10,300, 100);
dialog.add(label);

回答by Seseel Lybon

I have used JTextArea for multiline JLabels.

我已经将 JTextArea 用于多行 JLabel。

JTextArea textarea = new JTextArea ("1\n2\n3\n"+"4\n");

http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html

http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html

回答by Dediqated

In my case it was enough to split the text at every \nand then create a JLabelfor every line:

在我的情况下,将文本拆分为每一行就足够了\n,然后JLabel为每一行创建一个:

JPanel panel = new JPanel(new GridLayout(0,1));
String[] lines = message.split("\n");
for (String line : lines) {
    JLabel label = new JLabel(line);
    panel.add(label);
}

I used above in a JOptionPane.showMessageDialog

我在上面使用过 JOptionPane.showMessageDialog

回答by Scott M

This is horrifying. All these answers suggesting adding to the start of the label text, and there is not one word in the Java 11 (or earlier) documentation for JLabel to suggest that the text of a label is handled differently if it happens to start with <html>. Who says that works everywhere and always will? And you can get big, big surprises wrapping arbitrary text in and handing it to an html layout engine.

这太可怕了。所有这些答案都建议添加到标签文本的开头,并且在 Java 11(或更早版本)文档中,JLabel 中没有一个词表明如果标签文本碰巧以<html>. 谁说这在任何地方都有效并且永远有效?你可以得到很大的惊喜,将任意文本包装起来并将其交给 html 布局引擎。

I've upvoted the answer that suggests JTextArea. But I'll note that JTextArea isn't a drop-in replacement; by default it expands to fill rows, which is not how JLabel acts. I haven't come up with a solution to that yet.

我赞成建议 JTextArea 的答案。但我会注意到 JTextArea 不是一个简单的替代品;默认情况下,它会扩展以填充行,这不是 JLabel 的行为方式。我还没有想出解决办法。