删除后,对象会重新绘制

时间:2011-12-15 00:37:46

标签: java swing repaint paintcomponent

我需要清除JPanel。这个名为JPanel的{​​{1}}有一个mainPane,其中包含GridLayout JScrollPane JScrollPane's, and these JPanel s contain custom paintComponent()`。

无论我尝试什么 - 将列表设置为null,用新对象替换旧实例 - 只要我调整窗口框架的大小,视图的s that override就会被调用,它会在paintComponent()内绘制视图{1}}再次。

mainPane
我知道,{p> 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; } } 有点矫枉过正,我只是绝望了。所以基本上第一个函数和第二个函数在GUI构造函数中调用。当我调用createContentPane()来替换旧的UI(具有相同的结构,只是不同的内容)时,只要我调整容器的大小,就会重新绘制内容。唯一的区别是,在第二次createContentPane()调用时,调整大小绘制将覆盖布局,而元素不再位于内部。

我认为createContentPane()会清空所有内容并删除我可能拥有的所有引用,但createContentPane()仍然可以管理带有视图的滚动条。

2 个答案:

答案 0 :(得分:2)

在clearGrid()中,您需要调用remove(Component comp)(在您的情况下,sP)或removeAll();

答案 1 :(得分:2)

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

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

Container.removeAll


这是Andrew Thompson之前提到过的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();
    }
}