多行 JLabels - Java

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

Multiline JLabels - Java

javaswingjlabel

提问by Michael Clark

I want JLabeltext in multiline format otherwise text will be too long. How can we do this in Java?

我想要JLabel多行格式的文本,否则文本会太长。我们如何在 Java 中做到这一点?

回答by SingleShot

If you don't mind wrapping your label text in an htmltag, 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:

如果您不介意将标签文本包装在html标签中,当容器的宽度太窄而无法容纳所有内容时,JLabel 将自动对其进行自动换行。例如,尝试将其添加到 GUI,然后将 GUI 调整为太窄 - 它会包装:

new JLabel("<html>This is a really long line that I want to wrap around.</html>");

回答by Michael Clark

I recommend creating your own custom component that emulates the JLabel style while wrapping:

我建议创建您自己的自定义组件,在包装时模拟 JLabel 样式:

import javax.swing.JTextArea;

public class TextNote extends JTextArea {
    public TextNote(String text) {
        super(text);
        setBackground(null);
        setEditable(false);
        setBorder(null);
        setLineWrap(true);
        setWrapStyleWord(true);
        setFocusable(false);
    }
}

Then you just have to call:

然后你只需要调用:

new TextNote("Here is multiline content.");

Make sure that you set the amount of rows (textNote.setRows(2)) if you want to pack()to calculate the height of the parent component correctly.

textNote.setRows(2)如果要pack()正确计算父组件的高度,请确保设置行数 ( ) 。

回答by Nettogrof

I suggest to use a JTextArea instead of a JLabel

我建议使用 JTextArea 而不是 JLabel

and on your JTextArea you can use the method .setWrapStyleWord(true) to change line at the end of a word.

在您的 JTextArea 上,您可以使用 .setWrapStyleWord(true) 方法更改单词末尾的行。

回答by Andrew Thompson

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

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

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Jignesh Gothadiya

MultiLine Label with auto adjust height. Wrap text in Label

具有自动调整高度的多行标签。在标签中换行

private void wrapLabelText(JLabel label, String text) {
    FontMetrics fm = label.getFontMetrics(label.getFont());
    PlainDocument doc = new PlainDocument();
    Segment segment = new Segment();
    try {
        doc.insertString(0, text, null);
    } catch (BadLocationException e) {

    }

    StringBuffer sb = new StringBuffer("<html>");
    int noOfLine = 0;
    for (int i = 0; i < text.length();) {
        try {
            doc.getText(i, text.length() - i, segment);
        } catch (BadLocationException e) {
            throw new Error("Can't get line text");
        }
        int breakpoint = Utilities.getBreakLocation(segment, fm, 0, this.width - pointerSignWidth - insets.left - insets.right, null, 0);
        sb.append(text.substring(i, i + breakpoint));
        sb.append("<br/>");
        i += breakpoint;

        noOfLine++;
    }
    sb.append("</html>");
    label.setText(sb.toString());

    labelHeight = noOfLine * fm.getHeight();
    setSize();
}

Thanks, Jignesh Gothadiya

谢谢, Jignesh Gothadiya