我需要清除我的JPanel
。这JPanel
调用mainPane
有一个GridLayout
它包含JScrollPane's, and these
JScrollPane s contain custom
JPanel s that override
paintComponent()`。删除对象后即使重绘对象
无论我尝试什么 - 将列表设置为null,用新对象替换旧实例 - 只要我调整窗口框架的大小,视图的paintComponent()
get被调用,并且它再次在mainPane
内绘制视图。
public void createContentPane()
{
gridLay = new GridLayout(GRID_ROWS, GRID_COLUMNS);
graphicViewList = new ArrayList<View>();
for (int i = 0; i < graphicViewList.size(); i++) {
View v = graphicViewList.get(i);
v = null;
}
mainPane = new JPanel();
clearGrid();
mainPane.removeAll();
mainPane.validate();
graphicViewList.clear();
imageModel.deleteObservers();
mainPane.setLayout(gridLay);
this.getContentPane().remove(mainPane);
this.getContentPane().add(mainPane, BorderLayout.CENTER);
mainPane.revalidate();
mainPane.repaint();
}
public void createViews()
{
int idx = 0;
graphicViewList.clear();
while(idx < NUM_PERSPECTIVE) //iterator?
{
if(idx == 0)
{
graphicViewList.add(new ThumbnailView(this, imageModel));
mainPane.add(new JScrollPane(graphicViewList.get(idx)));
idx++;
continue;
}
graphicViewList.add(new ImageView(this, imageModel));
mainPane.add(new JScrollPane(graphicViewList.get(idx)));
idx++;
}
for (int i = 0; i < graphicViewList.size(); i++)
graphicViewList.get(i).removeFocus();
this.getContentPane().add(mainPane, BorderLayout.CENTER);
}
private void clearGrid()
{
for (int i = 0; i < mainPane.getComponentCount(); i++) {
JScrollPane sP =(JScrollPane) mainPane.getComponent(i);
sP = null;
}
}
createContentPane()
是矫枉过正我知道,我只是绝望。所以基本上第一个函数和第二个函数在GUI构造函数中被调用。当我拨打电话createContentPane()
来替换旧的UI(具有相同的结构,只是不同的内容)时,只要我调整容器的大小,就会重新绘制内容。唯一的区别是在第二个createContentPane()
调用调整大小绘制绘制布局和元素不在里面了。
我认为createContentPane()
会清空所有内容并删除我可能拥有的所有引用,但paintComponent()
仍然可以使用视图绘制滚动条。
为了更好地帮助您,请发布[SSCCE](http://sscce.org/)。 – 2011-12-15 01:31:14