2011-12-15 55 views
0

我需要清除我的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()仍然可以使用视图绘制滚动条。

+0

为了更好地帮助您,请发布[SSCCE](http://sscce.org/)。 – 2011-12-15 01:31:14

回答

2

在clearGrid(),你需要调用remove(Component comp)(在你的情况下,SP)或removeAll();

2

在你clearGrid方法,你是不是删除从面板任何东西;您只是获取mainPane内部的引用,然后将该引用设置为null。相反,您需要为每个滚动窗格实际调用mainPane.remove(sP);

此外,而不是该循环,您可以尝试拨打mainPane.removeAll();。请注意,正如文档中所述,您必须事后重新验证面板。

Container.removeAll


下面是SSCCE安德鲁·汤普森前面提到的。在提出问题时,请尝试做这件事,以便人们可以运行它并帮助解决问题。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class PanelClearerExample { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       PanelClearerExample app = new PanelClearerExample(); 
       app.startUp(); 
      } 
     }); 
    } 

    private final String CLEAR_COMMAND = "Clear"; 
    private final String REBUILD_COMMAND = "Rebuild"; 
    private JFrame controlFrame; 
    private int displayCount = 0; 
    private JFrame displayFrame; 
    private JPanel displayPanel = new JPanel(); 

    private void startUp() { 
     displayFrame = new JFrame("Display"); 
     displayFrame.setSize(300, 300); 
     displayFrame.setLocation(300, 0); 
     displayFrame.add(buildDisplay(), BorderLayout.CENTER); 

     controlFrame = new JFrame("Control"); 
     controlFrame.setSize(200, 100); 
     controlFrame.add(buildControl(), BorderLayout.CENTER); 
     controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     displayFrame.setVisible(true); 
     controlFrame.setVisible(true); 
    } 

    private Component buildDisplay() { 
     populateDisplay(); 
     return displayPanel; 
    } 

    private void populateDisplay() { 
     displayCount++; 
     displayPanel.setLayout(new GridLayout(2, 2)); 
     String text = "Display " + displayCount; 
     for (int i = 0; i < 4; i++) { 
      displayPanel.add(new JLabel(text, JLabel.CENTER)); 
     } 
    } 

    private Component buildControl() { 
     JPanel p = new JPanel(new BorderLayout()); 
     final JButton button = new JButton(CLEAR_COMMAND); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       if (CLEAR_COMMAND.equals(ae.getActionCommand())) { 
        clearDisplay(); 
        button.setText(REBUILD_COMMAND); 
        button.setActionCommand(REBUILD_COMMAND); 

       } else { 
        repopulateDisplay(); 
        button.setText(CLEAR_COMMAND); 
        button.setActionCommand(CLEAR_COMMAND); 
       } 
      } 
     }); 
     p.add(button); 

     return p; 
    } 

    private void clearDisplay() { 
     displayPanel.removeAll(); 
     displayPanel.repaint(); 
    } 

    private void repopulateDisplay() { 
     populateDisplay(); 
     displayPanel.revalidate(); 
    } 
} 
+0

我打电话给mainPane.removeAll()稍微低一点?我会尝试每次调用remove(sP)。 – 2011-12-15 01:24:49