如何在新开奖前清除 Graphics/JPanel

How to clear Graphics/JPanel before new draw

有很多类似的问题,但我没有在其中找到解决方案,所以这是我的问题:

在我的 JPanel 中,我有一个视觉证明,我更新它来开发我的证明,这里一切都很好。但是我有一个按钮可以让我重新开始我的证明。我没有让我的 JPanel 显示我的证明的第一行,而是实际上我的第一行,但我也交替看到我的旧证明(它在闪烁)。我不明白为什么。

我的 JPanel 包含多个组件,不仅是我的 JPanel,所以我认为使用 removeAll() 不是一个好主意。正如您将看到的,我尝试了 revalidate()repaint() 方法,但它不起作用。

我想从 to 过去,但实际上我都看到了。为什么?

这是我的代码的一些摘录:

这是我按下“首次亮相”按钮时的动作。只有最后对你有用,我使用重新验证和重绘方法但没有任何效果。 Pann 是我的 class,它扩展了 JPanel。

//Come back to the first step of the proof
debut.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        //I delete my old proof on my objects

        pann.revalidate();
        pann.repaint();
    }         
}); 

这是我的 JPanel 的绘制方法。我向您展示它是因为我使用了超级构造函数并且我认为它很重要。

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setFont(new Font("serif", Font.PLAIN, 20)); 

    if (arbre != null) 
        dessiner(arbre, g, new Dimension(0,0), 0, 'c', 0, 0);
}

Dessiner是我的证明方法,我觉得没必要给你看这个功能。

也许你能告诉我为什么它不起作用?为什么我同时看到两个证据?也许你知道我必须做什么? 感谢您的帮助,如果您需要更多代码或其他,请告诉我。

最后我使用了 getContentPane().removeAll(); 然后我添加了我所有的组件 :

getContentPane().removeAll();
//Modifications on pann
getContentPane().add(pann);
getContentPane().add(regles);
getContentPane().add(boutons);
getContentPane().add(pformule);
getContentPane().add(sequent);

我愿意接受任何其他解决方案。