【问题标题】:JLabel wrapping plain text as JTextAreaJLabel 将纯文本包装为 JTextArea
【发布时间】:2013-05-17 09:37:05
【问题描述】:

我对@9​​87654324@ 和其中包含的纯文本有疑问。对于纯文本换行,我使用LabelView,我对它给我的结果不太满意。

我的目标是让纯文本换行,例如JTextArea。我试过WrappedPlainView,但它只适用于JTextComponent

所以我的问题是也许你们知道/或有一些提前JLabel 意见?

注意:我不想将 html 添加到我的纯文本中。

标签视图: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

试试这个例子,看看调整框架大小时单词是如何换行的:

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

看看这个例子,你会发现 JLabel 换行的问题:

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

所以我们的目标是让 JLabel 单词在 JTextArea 中换行。

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

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖 SSCCE。 ;)
  • @Mode 在您对问题进行编辑之后,给您建议并不容易。请考虑 Andrew 的评论并准备一份 SSCCE。还要展示和描述您不喜欢 LabelView 的哪些方面以及您想要实现的目标。
  • @Mode 在我的第一次真实尝试中查看我的直升机视图

标签: java swing jlabel word-wrap


【解决方案1】:

所以我的问题是也许你们知道/或有一些进展 JLabel 视图,

有两种方法,

  • 使用 JTextComponent

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

.

  • 将 JLabel 与 Html 结合使用(缩减并实现了 Html3.2 版本)

  • 发布和 SSCCE

编辑:

注意:在实际项目中,我正在使用 javax.swing.text.View 和我 我将 LabelView 用于纯文本。

JTextComponentsjavax.swing.text.Xxx需要使用初始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) use JLabel with Html (reduced and implemented up_to version Html3.2), and another variations

【讨论】:

  • :-) 什么不起作用,这就是将其称为第一个的原因。尝试:-)
  • 这个例子只显示了我想要得到的。使用视图我“没有 JComponents”。我有视图,例如 LabelView,它被添加到 Shape 以表示文本。
  • 从结束到开始的时间,1.忘记了javax.swing.text。 2. 忘记了 JLabel,JTextComponent,3. 现在测试(在这个论坛上几次)在 SwingUtilities 中实现的方法(用于内部计算),从计算和布局开始 4. 然后是时候决定哪个 JComponent 更好,
  • 简而言之,我需要 WrappedPlainView + LabelView 的组合。
  • 因为 WrappedPlainView 可以使用多行,因为 JTextArea LabelView 可以使用 StyledDocument..
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

    我也同意@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 自动处理 &lt;html&gt; 标记内的包装文本。

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

    您可以使用&lt;br /&gt; 换行。

    希望这会有所帮助!

    【讨论】:

    • 您还有其他选择以及选项 (a)。
    【解决方案4】:

    试试这个

    jLabel.setText(String.format("<html><body style=\"text-align: justify;  text-justify: inter-word;\">%s</body></html>",LONGTEXT));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多