[英]GridBagLayout readd components
I created an ArrayList
of JLabel
s of size n and placed the JLabel
s inside an JInternalFrame
with the help of a GridBagLayout
manager so that the grid fits my purpose. 我创建了一个大小为n的
JLabel
的ArrayList
,并在GridBagLayout
管理器的帮助下将JLabel
放置在JInternalFrame
,以使网格适合我的目的。
Now I want to replace some of those JLabel
s or even remove them. 现在,我想替换其中的一些
JLabel
,甚至删除它们。 Removing the k-th JLabel
does work well with 删除第k个
JLabel
可以很好地与
innerframe.remove( ListOfLabels.get(k) );
ListOfLabels.remove(k);
(By the way if I'm using only one of those the JLabel
is not removed from the internal frame -- why ? If I remove other objects like Checkboxe
s, it sufficies to use only innerframe.remove(ListOfCBoxes.get(k));
) (顺便说一句,如果我只使用
JLabel
一个,则不会从内部框架中删除-为什么?如果我删除其他对象(如Checkboxe
,仅使用innerframe.remove(ListOfCBoxes.get(k));
)
Even the other JLabel
s stay at their position, what did not work, when I placed the JLabel
s inside the surrounding JFrame
. 当我将
JLabel
放置在周围的JFrame
时,甚至其他JLabel
停留在它们的位置,这JFrame
。 But I'm not able to readd a JLabel
after editing it's content. 但是编辑内容后,我无法读取
JLabel
。 I'm trying 我正在努力
//Initialization
ArrayList<JLabel> ListOfLabels = new ArrayList<JLabel>(n);
GridBagLayout GridBLayout_innerframe = new GridBagLayout();
GridBagConstraints GridBConstraints_innerframe = new GridBagConstraints();
JInternalFrame innerframe = new JInternalFrame();
innerframe.setLayout(GridBLayout_innerframe);
// Creating components of innerframe, arranging them in a grid and adding them.
// This seems to work.
innerframe.remove( ListOfLabels.get(k) );
ListOfLabels.remove(k);
labelk = new JLabel("New content");
ListOfLabels.add(labelk);
GridBConstraints_innerframe.gridy = k ;
GridBLayout_innerframe.setConstraints(ListOfLabels.get(n-1), GridBConstraints_innerframe) ;
innerframe.add(ListOfLabels.get(n-1)) ;
Of course the same constraints were used for the k-th JLabel
before and I did not erase this information. 当然,之前的第k个
JLabel
使用了相同的约束,我没有删除此信息。 I hoped I can overwrite it. 我希望可以覆盖它。
However the result is that the JLabel
s that I removed stay removed and the ones I want to add do not appear. 但是结果是我删除的
JLabel
保持删除状态,而我要添加的JLabel
没有出现。 Even after ''refreshing'' the window. 即使在“刷新”窗口之后。 There is also no error message from Eclipse.
Eclipse也没有错误消息。
Can someone please find my mistake and explain how to readd components into a hopefully already existing grid :/ 有人可以找到我的错误并解释如何将组件读取到希望已经存在的网格中吗:/
You are removing the JLabel
without saying the Component
that you did this. 您要删除
JLabel
而不是说您这样做的Component
。 You need to call the revalidate()
method just after adding/removing a Component
when it's already visible. 您需要在已经可见的
Component
添加/删除之后立即调用revalidate()
方法。
So, in case innerframe
is visible and you called innerframe.remove(...)
, you need to call: 因此,如果
innerframe
是可见的,并且您调用了innerframe.remove(...)
,则需要调用:
innerframe.revalaidate();
Then innerframe
notices that Component
s where added or removed and re-assigns the Component
s that are now in the Component
(calling the LayoutManager
, repainting, ...). 然后
innerframe
注意到Component
S其中添加或删除,并重新分配Component
s表示现在在Component
(调用LayoutManager
,重绘,...)。
It's a bad idea to remove/add components in a complex layout. 删除/添加复杂布局中的组件是一个坏主意。 In such cases I would provide a full relayout: clear the container (use the method
removeAll()
) and add all the required components again. 在这种情况下,我将提供完整的重新布局:清除容器(使用方法
removeAll()
),然后再次添加所有必需的组件。 Of course you must call revalidate()
and repaint()
for the top changed container. 当然,您必须为顶部已更改的容器调用
revalidate()
和repaint()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.