Java:JLabels 中的换行符? [英] Java: Linebreaks in JLabels?

查看:23
本文介绍了Java:JLabels 中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.
MMM FFF MMM FFFO O OOM   M MMMM.
MMM FFF MMM FFFO O OOM   M MMMM.
"); 

        comparisonPanel.add(legend);        
    }

推荐答案

在 setText 中使用 HTML,例如

Use HTML in setText, e.g.

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

这篇关于Java:JLabels 中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆