JList文本对齐方式 [英] JList text alignment

查看:191
本文介绍了JList文本对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JList ,其中包含我想要显示两个值的项目。有没有办法让它显示一个字符串名称,然后有一个右对齐的字符串来显示一个值。看起来像这样:

I have a JList with items that I want to show two values. Is there a way to have it show a string name and then have a right justified string to show a value. Looking something like this:

Title__________________120
Title2_________________135

是否可以将两个字符串传递给一个项目,并在左边显示第一个字符串,在右边显示第二个字符串?

Is it possible to pass in two string to an item and have the first string display on the left and the second one on the right?

推荐答案

当然,实现自定义渲染器。您可以使用 BorderLayout 作为渲染组件返回 JPanel ,其中LHS文本位于中WEST ,以及 EAST 中的RHS文本。

Sure, implement a custom renderer. You might return a JPanel with BorderLayout as the rendering component, with the LHS text in the WEST, and the RHS text in the EAST.

另一种方法是推HTML进入默认渲染器( JLabel ),使用横跨100%宽度的HTML表。虽然自定义渲染器可能是一个更好的选择,原因有很多(例如,不假设默认渲染器的类型是标签)。

Another way is to shove HTML into the default renderer (a JLabel), using an HTML table that stretches across 100% of the width. Though the custom renderer would be a better choice for a number of reasons (e.g. not presuming the type of the default renderer is a label).

BTW - 也许你应该考虑使用 JTable 来实现这种功能。不需要黑客或自定义类。

BTW - perhaps you should consider using a JTable for this kind of functionality. No hacks or custom classes needed.


.. jtable是否允许选择项目?

..does the jtable allow selecting items?

当然!以下是直接从如何使用表中获取的示例教程。选择简。

Of course! Here is an example taken directly from How to Use Tables in the tutorial. 'Jane' is selected.

表格设置得更加努力,但是值得付出努力。

A table is a little more effort to set up and get right, but it is well worth the effort.


JTable是否会像JList一样执行..

Would a JTable perform just as a JList ..

不,表最终会提供更多功能。但是它所做的事情也可以做,它以同样的方式为用户工作。

No, the table ultimately provides more functionality. But the things it does which a list can also do, work (for the user) in much the same way.

这篇关于JList文本对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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