我需要清除我的JPanel
. 这个JPanel
调用mainPane
有一个GridLayout
包含JScrollPane's, and these
JScrollPane s contain custom
JPanel s that override
paintComponent()`。
无论我尝试什么——将列表设置为空,用新对象替换旧实例——只要我调整窗口框架的大小,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()
调用时,resize draw 在布局上绘制,元素不再在里面。
我认为这createContentPane()
会清空所有内容并删除我可能拥有的所有引用,但paintComponent()
仍然可以使用视图绘制滚动条。