我需要清除我的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