如何实现一个支持换行和换行的面板?
我只会向该面板添加文本标签和换行符。
标签应从左向右流动,如果满足以下条件,则换行到下一个“行”:
需要。 换行符会导致跳转到下一行。
我还想让面板垂直滚动。
该解决方案应该在 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 JXHyperlink
s (from SwingX),
i.e. the panel contains clickable labels. This is the reason I cannot just use
JTextArea
.
发布评论
评论(4)
虽然它可能不是您正在寻找的解决方案,但从您的要求来看,自定义
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
> 可以告诉自定义布局管理器移动到下一行。 也许实现可以是这样的:上面的假设
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 aContainer
(such asPanel
), it should be possible to have aPanel
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 forPanel
) 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 aContainer
by using theadd()
method.For example, have a class constant
Component
in the custom Layout Manager, such as (a hypothetical)LineBreakLayout.LINE_BREAK
, and adding that to theContainer
can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:The above hypothetical
LineBreakLayout
will then render the firstLabel
in one line and the secondLabel
in the second line.我找到了
JTextPane
,之前由于某种原因我忽略了它。 这堂课满足了我的需要。不过还是谢谢你的帮助。 :)
I found
JTextPane
, which I had overlooked before for some reason. This class does what I need.Thanks for your help though. :)
替代文本 http://img187.imageshack.us/img187/3238/wraprn0.png< /a>
此示例不是来自面板(即容器),而是来自 JLabel(旨在显示内容)。
您可以在内容中使用 HTML 并使用
每次休息时。 您应该根据组件调整大小的规则以编程方式计算中断。
这是代码:
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:
更新:我错过了超链接支持的请求。 不知道如何在不使用 EditorPane 的情况下做到这一点。
JTextArea 完全按照您的描述进行操作。
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.