多行 JLabels - Java

发布于 2024-08-04 15:20:05 字数 65 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半葬歌 2024-08-11 15:20:05

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

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

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:

new JLabel("<html>This is a really long line that I want to wrap around.</html>");
一笔一画续写前缘 2024-08-11 15:20:05

我建议创建您自己的自定义组件,在换行时模拟 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);
    }
}

然后您只需调用:

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

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

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

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.

负佳期 2024-08-11 15:20:05

我建议使用 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.

顾冷 2024-08-11 15:20:05

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

在此处输入图像描述
在此处输入图像描述

It is possible to use (basic) CSS in the HTML.

enter image description here
enter image description here

单身情人 2024-08-11 15:20:05

具有自动调整高度的多行标签。
将文本换行到标签中

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

,谢谢,
吉涅什·戈塔迪亚

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文