java 如何使用 Swing 创建可扩展面板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25703056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create expandable panels using swing?
提问by Iter Ator
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);
回答by dic19
You can take a look to JXTaskPaneContainer
and JXTaskPane
from SwingXproject which have these advantages:
你可以去看一下JXTaskPaneContainer
,并JXTaskPane
从SwingX项目,它具有以下优点:
- 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 TreeModelnor TreeTableModelat all. Also trees and tree tables are components intended to show data in a hierarhical form, not to add components into it.
- 它们就像 a
JPanel
is一样是组件,因此使用它们不需要额外的努力。 - 与手风琴不同,您可以随时展开多个面板。
- 它们对折叠/展开事件具有凉爽和平滑的效果。
- 与树/树表不同,您根本不必弄乱TreeModel或TreeTableModel。此外,树和树表是旨在以分层形式显示数据的组件,而不是向其中添加组件。
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 Demosfor a complete set of SwingX components demo.
查看SwingLabs 演示以获取完整的 SwingX 组件演示集。
Screenshot
截屏
回答by chiastic-security
回答by Kalev
The component you're looking for is an accordion. Have a look at this question: Accordion for Swing?
您正在寻找的组件是手风琴。看看这个问题: 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. Each one removes the previously added panel. Try switching to another layout - e.g. BoxLayout
.
至于您的代码 - 您只能看到最后一个,因为您将它们全部添加到带有BorderLayout
,BorderLayout.North
作为布局约束的面板中。每一个都会删除之前添加的面板。尝试切换到另一种布局 - 例如BoxLayout
。
回答by Ruban Anderson
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
您必须在布局面板中放置一个网格面板。因此,在布局面板的“北”部分,只需添加一列的抓握面板和所需的原始数量