简体   繁体   English

JPanel repaint()调用不起作用

[英]JPanel repaint() call not working

I feel as though the solution to this is very simple and I'm just overlooking something stupid, but I can't seem to make the repaint() method of one of my JPanels work. 我觉得解决这个问题的方法很简单,只是忽略了一些愚蠢的事情,但是我似乎无法使我的JPanels之一的repaint()方法起作用。 The JPanel in question is a member object of another class which handles all the logic behind what is drawn to the JPanel, however, whenever I call repaint() in my thread, I do not see anything drawn, nor do I see my System.out.println() call, which I put in there for debugging purposes. 所讨论的JPanel是另一个类的成员对象,该类处理处理在JPanel上绘制的所有逻辑,但是,每当我在线程中调用repaint()时,都看不到绘制的内容,也看不到系统。 out.println()调用,出于调试目的,我将其放在其中。 I have put the files on Github for convenience. 为了方便起见,我将文件放在Github上。 Here are the three files I think MIGHT have something to do with it. 这是我认为可能与之相关的三个文件。 You can always look at the others if you need to. 如果需要,您可以随时查看其他内容。

SageLife.java SageLife.java

LifeFramework.java LifeFramework.java

Grid.java Grid.java

I've created plenty of JPanels before and have rarely encountered this problem, so I'm just not sure what's causing it. 我之前创建了许多JPanels,很少遇到此问题,因此我不确定是什么原因引起的。

You are overriding paintComponents rather than paintComponent in your Grid JPanel . 您将重写Grid JPanel paintComponents而不是paintComponent This does not follow Swing's paint chain mechanism. 这不遵循Swing的绘制链机制。

@Override
public void paintComponents(Graphics g) {
    super.paintComponents(g);

Note: always go for Swing Timer over Thread for handling UI updates in Swing. 注意:总是使用Thread Swing计时器来处理Swing中的UI更新。

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

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