[英]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项目中查看具有以下优点的
JXTaskPaneContainer
和JXTaskPane
:
JPanel
is so no extra effort is required to work with them. JPanel
,因此不需要额外的工作就可以使用它们。 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组件演示。
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.