简体   繁体   English

如何使用swing创建可扩展面板?

[英]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: 您可以从SwingX项目中查看具有以下优点的JXTaskPaneContainerJXTaskPane

  • They are components just like a JPanel is so no extra effort is required to work with them. 它们就像JPanel ,因此不需要额外的工作就可以使用它们。
  • 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. 与树/树表不同,您根本不需要使用TreeModelTreeTableModel Also trees and tree tables are components intended to show data in a hierarhical form, not to add components into it. 树和树表也是用于以分层形式显示数据的组件,而不是向其中添加组件。

If you don't like task panes and want to implement something on your own then you have JXCollapsiblePane (also available in SwingX API). 如果您不喜欢任务窗格并希望自己实现某些功能,那么您可以使用JXCollapsiblePane (也可以在SwingX API中使用)。

Check SwingLabs Demos for a complete set of SwingX components demo. 检查SwingLabs演示,获取一整套SwingX组件演示。

Screenshot 截图

在此输入图像描述

Look at JXTreeTable from the SwingX project: 从SwingX项目中查看JXTreeTable: JXTreeTable在行动

You might also look at JXTree, which is similar, and might be better, depending your exact needs. 您可能也会看到类似的JXTree,可能会更好,具体取决于您的确切需求。

Get it from here . 这里获取它。

The component you're looking for is an accordion. 您正在寻找的组件是手风琴。 Have a look at this question: Accordion for Swing? 看看这个问题: 旋转手风琴?

As to your code - you can only see the last one because you're adding them all to a panel with a BorderLayout , with BorderLayout.North as the layout constraint. 至于你的代码 - 你只能看到最后一个,因为你将它们全部添加到一个带有BorderLayout的面板, BorderLayout.North作为布局约束。 Each one removes the previously added panel. 每一个都删除以前添加的面板。 Try switching to another layout - eg BoxLayout . 尝试切换到另一个布局 - 例如BoxLayout

you have to put a grid panel in your layout panel. 你必须在你的布局面板中放置一个网格面板。 so in the "north" part in your layout panel, just add a grip panel of one column and the number of raw you want 所以在布局面板的“北”部分,只需添加一列的抓握面板和所需的原始数量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM