简体   繁体   English

JLabel 中的多行文本

[英]Multiline text in JLabel

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

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);

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

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

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

Type the content (ie, the "text" property field) inside a <html></html> tag.<html></html>标记内键入内容(即“文本”属性字段)。 So you can use <br> or <P> to insert a newline.所以你可以使用<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.就像天上的一颗钻石。

You can also use a JXLabel from the SwingX library .您还可以使用SwingX 库中JXLabel

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

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> .所有这些答案都建议添加到标签文本的开头,并且在 Java 11(或更早版本)文档中,JLabel 中没有一个词表明如果标签文本恰好以<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.你可以得到很大的惊喜,将任意文本包装起来并将其交给 html 布局引擎。

I've upvoted the answer that suggests JTextArea.我赞成建议 JTextArea 的答案。 But I'll note that JTextArea isn't a drop-in replacement;但我会注意到 JTextArea 不是一个简单的替代品; by default it expands to fill rows, which is not how JLabel acts.默认情况下,它会扩展以填充行,这不是 JLabel 的行为方式。 I haven't come up with a solution to that yet.我还没有想出解决办法。

In my case it was enough to split the text at every \\n and then create a JLabel for 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上面使用过

You can use JTextArea and remove editing capabilities to get normal read-only multiline text.您可以使用JTextArea并删除编辑功能来获取普通的只读多行文本。

JTextArea textArea = new JTextArea("line\nline\nline");
textArea.setEditable(false);

JTextArea

setEditable

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);

It is possible to use (basic) CSS in the HTML.可以在 HTML 中使用(基本)CSS


This question was linked from Multiline JLabels - Java .这个问题是从Multiline JLabels-Java链接的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM