简体   繁体   English

如何在新绘制之前清除 Graphics/JPanel

[英]How to clear Graphics/JPanel before new draw

There are a lot of similar questions but I didn't find a solution in them so here is my problem:有很多类似的问题,但我没有在其中找到解决方案,所以这是我的问题:

In my JPanel I have a visual proof which I update to develop my proof, everything is good here.在我的 JPanel 中,我有一个视觉证明,我更新它以开发我的证明,这里一切都很好。 But I have a button which allows me to restart my proof.但是我有一个按钮可以让我重新开始我的证明。 Instead of having my JPanel with the first line of my proof I have effectively my first line but I also see my old proof alternately (it's blinking).我没有让我的 JPanel 与我的证明的第一行有效,而是我的第一行有效,但我也交替看到我的旧证明(它在闪烁)。 I dont't understand why.我不明白为什么。

My JPanel contains several components, not only my JPanel so I don't think that use removeAll() is a good idea.我的 JPanel 包含几个组件,不仅是我的 JPanel,所以我认为使用removeAll()不是一个好主意。 As you will see, I tried revalidate() and repaint() method but it doesn't work.如您所见,我尝试了revalidate()repaint()方法,但它不起作用。

I would like to pass from我想从在此处输入图像描述 to在此处输入图像描述 but actually I see both.但实际上我两者都看到了。 Why?为什么?

Here is some extracts of my code:这是我的代码的一些摘录:

Here is the action when I push the button "debut".这是我按下“debut”按钮时的动作。 Only the end is useful for you, I use revalidate and repaint methods but nothing works.只有结尾对你有用,我使用 revalidate 和 repaint 方法但没有任何效果。 Pann is my class which extends JPanel. 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();
    }         
}); 

And here is my paint method for my JPanel.这是我的 JPanel 的绘制方法。 I show you it because I use the super constructor and I think it's important.我向您展示它是因为我使用了超级构造函数,并且我认为它很重要。

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 is my method which draw my proof, I think it's not necessary to show you this function. Dessiner 是我的证明方法,我认为没有必要给你看这个 function。

Maybe you can tell me why it doesn't work?也许你可以告诉我为什么它不起作用? Why I see both proof in the same time?为什么我同时看到两个证据? And maybe you know what I have to do?也许你知道我必须做什么? Thank you for your help and tell me if you need more code or other.感谢您的帮助,如果您需要更多代码或其他代码,请告诉我。

Finally I used getContentPane().removeAll();最后我使用getContentPane().removeAll(); and then I added all my components:然后我添加了所有组件:

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

I'm open to any other solutions.我对任何其他解决方案持开放态度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM