简体   繁体   English

如何检查谁在Swing中提交repaint()事件?

[英]How to check who submitting repaint() event in Swing?

I have written a custom component that extends JPanel and overridden its paint() method. 我编写了一个自定义组件,该组件扩展了JPanel并覆盖了其paint()方法。 Now I can see that this method is called once per 10 milliseconds when the component is displaying. 现在,我看到在显示组件时,此方法每10毫秒调用一次。 Nothing changes in the component but paint() is still called. 组件中没有任何变化,但仍调用paint()。 I have several calls repaint() but none of them is called actually. 我有几个调用repaint(),但实际上没有一个调用。 How to know what is causing such frequent updates? 如何知道导致如此频繁更新的原因?

UPDATE! UPDATE! There was "bug" in my code. 我的代码中有“ bug”。 I was updating inner components form paint() method so it was the root cause of continuous repainting. 我正在通过paint()方法更新内部组件,因此这是连续重新绘制的根本原因。 But still, the question is not answered: how to understand what supplies events to the queue? 但是仍然没有回答这个问题:如何理解是什么向队列提供事件?

how to understand what supplies events to the queue? 如何理解什么将事件提供给队列?

Whenever a property of a Swing component is changed the component will automatically invoke repaint() on itself. 每当更改Swing组件的属性时,该组件都会自动对其自身调用repaint() The paint request is passed to the RepaintManager . 绘制请求将传递给RepaintManager

The RepaintManager will then consolidate multiple repaint requests into a single painting of all components. 然后, RepaintManager将把多个重新绘制请求合并到所有组件的单个绘制中。 The consolidation is done to make painting more efficient. 进行合并以提高绘画效率。

So the individual component that made the request is not available because in many cases multiple components will make the repaint request at the same time. 因此,发出请求的单个组件不可用,因为在许多情况下,多个组件将同时发出重新绘制请求。

You can read Painting in AWT and Swing for a more detailed explanation. 您可以阅读AWT和Swing中的绘画以获取更详细的说明。

使用debug在paint()函数中创建一个断点,当它调用时,您可以查看回调的堆栈跟踪

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

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