Java Swing 中具有换行和换行功能的面板

发布于 2024-07-12 20:02:33 字数 278 浏览 6 评论 0 原文

如何实现一个支持换行和换行的面板? 我只会向该面板添加文本标签和换行符。 标签应从左向右流动,如果满足以下条件,则换行到下一个“行”: 需要。 换行符会导致跳转到下一行。 我还想让面板垂直滚动。

该解决方案应该在 Java 5 中运行。可以使用 SwingX。

澄清:文本标签实际上是 JXHyperlink(来自 SwingX), 即面板包含可单击的标签。 这就是我不能只使用的原因 JTextArea

How to implement a panel that would support line wrapping and line breaking?
I would only add textual labels and line breaks to this panel.
The labels should flow from left to right, wrapping to the next "line" if
needed. The line breaks would cause a jump to the next line.
I would also like to make the panel vertically scrollable.

The solution should work in Java 5. SwingX can be used.

Clarification: The textual labels are actually JXHyperlinks (from SwingX),
i.e. the panel contains clickable labels. This is the reason I cannot just use
JTextArea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

時窥 2024-07-19 20:03:09

虽然它可能不是您正在寻找的解决方案,但从您的要求来看,自定义 LayoutManager 似乎能够实现您所追求的目标。 通过设计和分配一个允许换行的自定义布局管理器 容器(例如Panel),应该可以有一个允许换行的Panel

在容器内布局组件文章来自 < a href="http://java.sun.com/docs/books/tutorial/index.html" rel="nofollow noreferrer">Java 教程 将提供有关布局管理器如何在 Java 中工作的一般信息,特别是创建自定义布局管理器将提供有关如何制作自定义布局管理器以应用于Container的信息。

FlowLayout 的行为Panel 的默认布局管理器)似乎与您可能追求的行为相当接近。 为换行符添加功能似乎是缺失的部分。

建议:也许自定义布局管理器可以通过使用表示换行符的 Component 来添加换行符,该组件可以添加到 Container 中 使用 add() 方法。

例如,在自定义布局管理器中有一个类常量 Component,例如(假设的)LineBreakLayout.LINE_BREAK,并将其添加到 Container > 可以告诉自定义布局管理器移动到下一行。 也许实现可以是这样的:

Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));

上面的假设 LineBreakLayout 将在一行中渲染第一个 Label 和第二个 Label 在第二行。

Although it may not be a solution you're in search of, but from the requirements you have, it seems like a custom LayoutManager may be able to achieve what you are after. By designing and assigning a custom Layout Manager which allows line breaks to a Container (such as Panel), it should be possible to have a Panel which allows line breaks.

The Laying Out Components Within a Container article from The Java Tutorials will provide general information on how Layout Managers work in Java, and in particular, the Creating a Custom Layout Manager will provide information on how to make a custom Layout Manager to apply to an Container.

The behavior of the FlowLayout (the default Layout Manager for Panel) seems fairly close to the behavior you may be after. Adding functionality to line break seems like the missing piece.

Suggestion: Perhaps the custom Layout Manager can have the ability to add a line break by having a Component that represents a line break, which can be added to a Container by using the add() method.

For example, have a class constant Component in the custom Layout Manager, such as (a hypothetical) LineBreakLayout.LINE_BREAK, and adding that to the Container can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:

Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));

The above hypothetical LineBreakLayout will then render the first Label in one line and the second Label in the second line.

玩物 2024-07-19 20:03:02

我找到了 JTextPane,之前由于某种原因我忽略了它。 这堂课满足了我的需要。

不过还是谢谢你的帮助。 :)

I found JTextPane, which I had overlooked before for some reason. This class does what I need.

Thanks for your help though. :)

时光瘦了 2024-07-19 20:02:57

替代文本 http://img187.imageshack.us/img187/3238/wraprn0.png< /a>

此示例不是来自面板(即容器),而是来自 JLabel(旨在显示内容)。

您可以在内容中使用 HTML 并使用
每次休息时。 应该根据组件调整大小的规则以编程方式计算中断。

这是代码:

import javax.swing.*;
import java.awt.*;

public class Wrap {


    public static void main( String [] args ) { 

        JFrame frame = new JFrame("Wrap test");

        String text =  "<html>This<br>is<br>a<br>multiline<br>label</html>";


        frame.add( new JLabel( text ) );

        frame.pack();

        frame.setVisible( true );

    }
}

alt text http://img187.imageshack.us/img187/3238/wraprn0.png

This sample is not from a panel, that is a container, but from a JLabel, that is intended to show content.

You could use HTML in your content and use a <br> on each break. You should programmatically calculate the breaks according with your rules on component resize.

Here's the code:

import javax.swing.*;
import java.awt.*;

public class Wrap {


    public static void main( String [] args ) { 

        JFrame frame = new JFrame("Wrap test");

        String text =  "<html>This<br>is<br>a<br>multiline<br>label</html>";


        frame.add( new JLabel( text ) );

        frame.pack();

        frame.setVisible( true );

    }
}
青衫儰鉨ミ守葔 2024-07-19 20:02:53

更新:我错过了超链接支持的请求。 不知道如何在不使用 EditorPane 的情况下做到这一点。

JTextArea 完全按照您的描述进行操作。

JTextArea textArea = new JTextArea();
JScrollPanel sPane = new JScrollPane(textArea);

UPDATE: I missed the request for hyperlink support. Don't know how to do that w/o using the EditorPane.

JTextArea does exactly what you've described.

JTextArea textArea = new JTextArea();
JScrollPanel sPane = new JScrollPane(textArea);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文