如何使用秋千创建可扩展面板? [英] How to create expandable panels using swing?
问题描述
我想创建一个展开和/或折叠面板的列表,如这张图片所示:
I would like to create a list of expandand/collapse panels, like on this image:
我还没有找到任何摆动组件,所以我开始创建类似的东西. 我试图将一个按钮放在另一个按钮下面,以填充可用的宽度,但实际上不起作用.我只能看到最后添加的一个.
I haven't found any swing component for this, so I began to create something like this. I tried to put buttons one under the other, which fill the available width, but doesn't really works. I can only see the last added one.
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JButton("Delphi Projects"), BorderLayout.NORTH);
panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
panel.add(new JButton("Delphi Projects | Delphi Files"), BorderLayout.NORTH);
panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
panel.add(new JButton("Other Files"), BorderLayout.NORTH);
panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
panel.add(new JButton("C++ Builder Projects | C++ Builder Files"), BorderLayout.NORTH);
panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
JScrollPane scroll = new JScrollPane(panel);
推荐答案
You can take a look to JXTaskPaneContainer
and JXTaskPane
from SwingX project which have these advantages:
- 它们是与
JPanel
一样的组件,因此与它们一起工作无需花费额外的精力. - 与手风琴不同,您可以在任意位置扩展多个面板 时间.
- 它们对崩溃/扩展事件具有凉爽和平滑的作用.
- 与树木/树表不同,您不必弄乱 TreeModel 或 TreeTableModel .树和树表也是用于以层级形式显示数据而不向其中添加组件的组件.
- They are components just like a
JPanel
is so no extra effort is required to work with them. - Unlike accordions you can have more than one panel expanded at any time.
- They have a cool and smooth effect on collapsing/expanding events.
- Unlike trees/tree tables you don't have to mess with either TreeModel nor TreeTableModel at all. Also trees and tree tables are components intended to show data in a hierarhical form, not to add components into it.
如果您不喜欢任务窗格并想自己实现某些功能,则可以使用JXCollapsiblePane
(在SwingX API中也可以使用).
If you don't like task panes and want to implement something on your own then you have JXCollapsiblePane
(also available in SwingX API).
检查 SwingLabs演示,以获取完整的SwingX组件演示.
Check SwingLabs Demos for a complete set of SwingX components demo.
这篇关于如何使用秋千创建可扩展面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!