简体   繁体   English

GridBagLayout读取的组件

[英]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的JLabelArrayList ,并在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.

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