Java: Linebreaks in JLabels?

北慕城南 提交于 2019-11-26 18:28:08

问题


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?

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

回答1:


Use HTML in setText, e.g.

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



回答2:


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

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



回答3:


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.

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.

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 MultiLineLabel class that in addition to wrapping text supports vertical and horizontal text alignment.

UPDATE

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




回答4:


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




回答5:


What about using the wrapping feature in a 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);



回答6:


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

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



回答7:


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


来源:https://stackoverflow.com/questions/1842223/java-linebreaks-in-jlabels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!