1

I have a problem with JLabel and plain text wrapping in it. For plain text wrapping i am using LabelView and i am not very happy about the result it gives to me.

My goal is to get plain text wrapping as in for example JTextArea. I tried WrappedPlainView but it works only with JTextComponent

So my question would be maybe you guys know/or have some advance JLabel views?

note: i do not want to add html to my plain text.

LabelView: http://docs.oracle.com/javase/6/docs/api/javax/swing/text/LabelView.html

WrappedPlainView: http://docs.oracle.com/javase/6/docs/api/javax/swing/text/WrappedPlainView.html

Try this example and look how words are wrapped when frame is resized:

import javax.swing.*;
import java.awt.*;
public class Example extends JFrame {
private static final String LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                                        "Ut in enim velit. Nunc posuere purus ac odio dictum auctor. " +
                                        "Vivamus nec sem mi. Curabitur sed iaculis nibh. Proin vel massa augue. " +
                                        "Aenean laoreet, tellus ut vulputate mollis, justo eros ornare tortor, " +
                                        "vitae venenatis turpis augue id purus. In quis pretium justo. " +
                                        "Quisque interdum sem at augue ultrices molestie. " +
                                        "Nulla consectetur magna nec est malesuada sed ultricies diam gravida. " +
                                        "Curabitur luctus, nulla nec pulvinar fringilla, enim turpis luctus tellus, " +
                                        "non auctor diam ligula quis lectus.";

public Example()
{
    JTextArea textArea = new JTextArea(LONG_TEXT);
    textArea.setWrapStyleWord(true);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setOpaque(false);
    textArea.setEditable(false);

    add(new JScrollPane(textArea), BorderLayout.CENTER);
    setMinimumSize(new Dimension(100, 100));
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
}

public static void main(String []args)
{
    new Example();
}
}

Take a look to this example and you will see the problem with JLabel wrapping words:

import javax.swing.*;
import java.awt.*;
public class Example extends JFrame {
private static final String LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                                        "Ut in enim velit. Nunc posuere purus ac odio dictum auctor. " +
                                        "Vivamus nec sem mi. Curabitur sed iaculis nibh. Proin vel massa augue. " +
                                        "Aenean laoreet, tellus ut vulputate mollis, justo eros ornare tortor, " +
                                        "vitae venenatis turpis augue id purus. In quis pretium justo. " +
                                        "Quisque interdum sem at augue ultrices molestie. " +
                                        "Nulla consectetur magna nec est malesuada sed ultricies diam gravida. " +
                                        "Curabitur luctus, nulla nec pulvinar fringilla, enim turpis luctus tellus, " +
                                        "non auctor diam ligula quis lectus.";

public Example()
{
    JLabel label = new JLabel(LONG_TEXT);
    add(label, BorderLayout.CENTER);
    setMinimumSize(new Dimension(100, 100));
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
}

public static void main(String []args)
{
    new Example();
}
}

So the goal is to get JLabel words wrapping as in JTextArea.

NOTE: in real project i am working with javax.swing.text.View`s and i am using LabelView for plain text.

4

4 回答 4

3

所以我的问题是也许你们知道/或有一些先进的 JLabel 观点,

有两种方法,

  • 使用 JTextComponent

    1. 不可编辑,setEditable()
    2. 更改 setDisabledTextColor()
    3. (仅在需要时)透明度(默认情况下为 JLabel)您可以更改不透明度

.

  • 将 JLabel 与 Html 一起使用(简化并实现了 Html3.2 版本)

  • 邮政和SSCCE

编辑:

注意:在实际项目中,我正在使用 javax.swing.text.View`s,并且我正在使用 LabelView 作为纯文本。

对于JTextComponents并且javax.swing.text.Xxx需要使用 intial PreferredSize,请参阅public JTextArea(String text, int rows, int columns)

添加 1) 使用 JTextComponent

必须有决定

  • 如果包裹在JScrollPane不可见或不可见的 JScrollBar 中(覆盖 MouseScroll)

  • 或简单添加到容器中,我建议使用 BorderLayout 或 GridLayout,然后 JTextArea 也可以使用容器调整大小(看起来像)

  • 可滚动的 JComponents 将被添加到 JScrollPane

.

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class ExampleJTextArea extends JFrame {

    private /*static*/ final String LONG_TEXT = "Lorem ipsum dolor sit amet, "
            + "consectetur adipiscing elit. Ut in enim velit. Nunc posuere "
            + "purus ac odio dictum auctor. Vivamus nec sem mi. Curabitur sed "
            + "iaculis nibh. Proin vel massa augue. Aenean laoreet, tellus ut "
            + "vulputate mollis, justo eros ornare tortor, vitae venenatis "
            + "turpis augue id purus. In quis pretium justo. Quisque interdum "
            + "sem at augue ultrices molestie. Nulla consectetur magna nec est "
            + "malesuada sed ultricies diam gravida. Curabitur luctus, nulla "
            + "nec pulvinar fringilla, enim turpis luctus tellus, non auctor "
            + "diam ligula quis lectus.";

    public ExampleJTextArea() {
        JTextArea textArea = new JTextArea(LONG_TEXT, 10, 25);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setOpaque(false);
        textArea.setEditable(false);
        textArea.setBorder(new EmptyBorder(10,10,2,2));
        add(/*new JScrollPane(*/textArea/*), BorderLayout.CENTER*/);
        //setMinimumSize(new Dimension(100, 100));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ExampleJTextArea exampleJTextArea = new ExampleJTextArea();
            }
        });
    }
}

.

.

添加 2)将 JLabel 与 Html 一起使用(简化并实现了 Html3.2 版本),以及其他变体

于 2013-05-17T09:57:37.143 回答
1

一个非常古老的线程,但万一有人再次遇到这个问题......

这些解决方案似乎都不适用于我(也许是因为包含我的 JLabel 的 JPanel 被 JScrollPane 查看?),而且我无法让 JTextAreas 工作,因为它有某种棘手的错误,它会增长但不会t 收缩。

相反,这是我能找到的在 JLabel 中包装文本的最简单解决方案,由 Tony Fecteau 在此页面上提供:只需 setPreferredSize()在 html 格式的 JLabel 上使用,它就会开始自动增长和缩小!

panel = new JPanel();
JLabel label = new JLabel("<html>" + "long text to display" + "</html>");
label.setPreferredSize(new Dimension(1, 1)); // this is the key part.
panel.add(label, "grow");
于 2016-03-30T18:25:53.207 回答
0

我也同意@mKorbel 的观点,有两种方法可以通过以下方式实现正确的换行JLabel

a) 创建您自己的自定义组件,JLabel在包装时模拟样式。

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

用法

new CustomWrappedLabel("Hey you've just tried multi-line text!");

确保将行数设置为超过 2 行以实现所需的行为。 wrappedLabelObj.setRows(2). 用于pack()正确计算父组件的高度。

b)使用自动处理<html>标签内包装文本的html。

new JLabel("<html>Hey you've just tried html multi-line text version!</html>")

您可以使用<br />换行符。

希望这可以帮助!

于 2013-05-17T10:54:17.353 回答
0

尝试这个

jLabel.setText(String.format("<html><body style=\"text-align: justify;  text-justify: inter-word;\">%s</body></html>",LONGTEXT));
于 2020-05-31T09:11:19.097 回答