Java:JLabels 中的换行符?

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

Java: Linebreaks in JLabels?

javauser-interfaceswingjlabel

提问by Nick Heiner

I'm trying to make a Swing JLabel with multiple lines of text. It's added just fine, but the line breaks don't come through. How do I do this? Alternatively, can I just specify a maximum width for a JLabel and know that the text would wrap, like in a div?

我正在尝试使用多行文本制作 Swing JLabel。它添加得很好,但换行符没有通过。我该怎么做呢?或者,我是否可以只为 JLabel 指定最大宽度并知道文本会换行,就像在 div 中一样?

    private void addLegend() {
        JPanel comparisonPanel = getComparisonPanel();

        //this all displays on one line
        JLabel legend = new JLabel("MMM FFF MMM FFFO O OOM   M MMMM.\nMMM FFF MMM FFFO O OOM   M MMMM.\nMMM FFF MMM FFFO O OOM   M MMMM.\n"); 

        comparisonPanel.add(legend);        
    }

采纳答案by Erich Kitzmueller

Use HTML in setText, e.g.

在 setText 中使用 HTML,例如

myLabel.setText("<html><body>with<br>linebreak</body></html>");

回答by Amir Afghani

You can put HTML inside of a JLabel and use the linebreak tag to achieve this.

您可以将 HTML 放入 JLabel 并使用换行标记来实现此目的。

回答by Samuel Sj?berg

By default, Swing does not wrap text. If you specify a size on the JLabel it will only paint the part of the text that fits and then add "..." to the end.

默认情况下,Swing 不换行文本。如果您在 JLabel 上指定大小,它只会绘制适合的文本部分,然后在末尾添加“...”。

As suggested you can use HTML to enable line wrapping. However, I've actually created a custom Swing UI delegate not long ago to achieve this and even more: MultiLineLabelUI.

根据建议,您可以使用 HTML 来启用换行。但是,不久前我实际上创建了一个自定义 Swing UI 委托来实现这一点,甚至更多:MultiLineLabelUI

It will wrap your text to fit the available space and also respect hard line breaks. If you choose to try it out, it is as simple as:

它将包装您的文本以适应可用空间并尊重硬换行符。如果您选择尝试一下,它很简单:

JLabel label = new JLabel("Text that'll wrap if necessary");
label.setUI(MultiLineLabelUI.labelUI);

Or alternatively use the custom MultiLineLabelclass that in addition to wrapping text supports vertical and horizontal text alignment.

或者,也可以使用自定义MultiLineLabel类,除了文本换行外,还支持垂直和水平文本对齐。

UPDATE

更新

I lost the domain with the original code samples. It can now be viewed on github instead: https://github.com/sasjo/multiline

我丢失了原始代码示例的域。现在可以在 github 上查看:https: //github.com/sasjo/multiline

回答by Stew

What about using the wrapping feature in a JTextArea?

在 JTextArea 中使用包装功能怎么样?

    String text = "some really long string that might need to"+
                  "be wrapped if the window is not wide enough";

    JTextArea multi = new JTextArea(text);
    multi.setWrapStyleWord(true);
    multi.setLineWrap(true);
    multi.setEditable(false);

    JLabel single = new JLabel(text);

    JPanel textpanel = new JPanel(new GridLayout(2,1));
    textpanel.add(multi);
    textpanel.add(single);

    JFrame frame = new JFrame();
    frame.add(textpanel);
    frame.pack();
    frame.setVisible(true);

回答by Robert

I did not manage to specify a maximum width for a label but you can specify a concrete width. By measuring the current width of a JLabel we can only apply the new fixed with if the JLabels's width is higher that our maxWidth:

我没有设法为标签指定最大宽度,但您可以指定具体宽度。通过测量 JLabel 的当前宽度,我们只能在 JLabels 的宽度大于我们的 maxWidth 时应用新的固定宽度:

JLabel label = new JLabel("<html>" + myVeryLongMessage + "<html>");
int maxWidth = 400;
Dimension size = label.getPreferredSize();
if (size.width > maxWidth) {
    // Estimate the number of lines
    int lineCount = (int) Math.ceil(((double) size.width) / maxWidth);
    lineCount += 1; // Add one extra line as reserve
    size.width = maxWidth; // Apply the maximum width
    // Increase the height so that all lines will be visible
    size.height *= lineCount;
    label.setPreferredSize(size);
}

回答by sayem siam

You can get automatic line break if you set the paragraph width in html.

如果您在 html 中设置段落宽度,您可以获得自动换行符。

  label.setText("<html><p style=\"width:100px\">"+paragraph+"</p></html>");

回答by JavaTechnical

Simple,use HTML. Java Swing components though does not provide a 'fantastic' support for the HTML, you can use it for such simple purposes.

简单,使用HTML。Java Swing 组件虽然不提供对 HTML 的“出色”支持,但您可以将其用于如此简单的目的。

label.setText("<html>This is first line.<br/>This is second line.</html>");