简体   繁体   English

JPanel(Java)中组件的顺序

[英]Order of components in a JPanel (Java)

How can I decide the order of components when using a JPanel? 使用JPanel时如何确定组件的顺序? My code looks something like this: 我的代码如下所示:

panel = new JPanel();

panel.add(a);
panel.add(b);
panel.add(c);

[...] [...]

panel.remove(b);

[...] [...]

panel.add(b);

Now, b is added at the end (right). 现在,将b添加到末尾(右)。 How can I add b again in the middle? 如何在中间再次添加b?

You can use 您可以使用

add(Component comp, int index) 

to insert a component at a given index. 在给定索引处插入组件。

假设您知道要在其中放置组件的索引,则可以使用

add(b, index)

You can add a component at index: 您可以在索引处添加组件:

panel.add(b, [index]); panel.add(b,[index]);

Note that the index is 0-based. 请注意,索引是从0开始的。

Use index. 使用索引。 Also, you may like to look at MigLayout, a library. 另外,您可能想看看MigLayout,一个库。 The best layout manager IMO, very easy to use. 最好的IMO布局管理器,非常易于使用。

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

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