JPanel、侦听器和线程
对于一个学校项目,我正在用 JAVA 编写一个 BlackJack 纸牌游戏。
在程序中执行几个步骤后,我的整个计算机系统速度变慢/卡顿,并且 JFrame 中的某些区域变黑。似乎没有重新粉刷的情况发生。
我会用文字给你一些程序细节,而不是提交代码。
我对几乎每个 GUI 组件都进行了广泛的扩展,以使其具有我想要的外观和感觉。我使用的几乎每个 JComponent 子组件都使用自定义图像背景和适用的抗锯齿功能重写了其paintComponent。
在我的(自定义)JFrame 中,我加载了 JPanel 的扩展版本。该面板包含该屏幕的所有 GUI 组件。当我想切换屏幕(转到程序的不同部分)时,我切换到 JPanel 的另一个扩展版本。我通过从 JFrame 中删除所有元素并添加新面板来完成此操作。这些不同的面板实现了 ActionListener 和 FocusListener。
此时我的想法倾向于线程处理问题。我目前的理论是这样的:当构建一个面板以在 JFrame 中显示时(每个面板都在程序内的不同阶段),之前构建的面板并没有真正“销毁”,并且这些面板的侦听器仍然处于活动状态。
我对你们提出的问题是,这个理论是否站得住脚……在这种情况下,有没有办法“杀死”这些面板或至少停止监听?
如果我的理论没有意义,那么还有什么可能导致这些症状?难道是对paintComponent方法的大量覆盖?
任何想法都将受到高度赞赏!
问候威尔科
For a school project I'm writing a BlackJack card game in JAVA.
After a few steps within the program, my whole computer system slows down/stutters and some areas within the JFrame turn black. No repainting seems te be happening.
I will give you some program details in words rather then submitting the code.
I have done some extensive extending of almost every GUI component to give it the look and feel that I want. Almost every child of JComponent that I use has got its paintComponent rewriten with custom image backgrounds and anti-aliasing where applicable.
In my (custom) JFrame I load an extended version of JPanel. This panel holds all the GUI components for that screen. When I want to switch screen (go to a different part of the program), I switch to another extended version of JPanel. I do this by removing all elements from the JFrame and add the new panel. These different panels implements ActionListeners and FocusListeners.
At this point my thoughts are leaning towards a thread handling issue. My theory for the moment is this: When a panel is constructed for display in the JFrame (each on different stages within te program), the previous constructed panels aren't realy 'destroyed', and the Listeners of those panels remain active.
My question for you people is, wether this theory holds ground... And in that case, is there a way to 'kill' these panels or at least stop the listening?
If my theory doesn't make sense, what else could be causing these symptoms? Could it be the extensive overwriting of the paintComponent methods?
Any ideas would be highly appriciated!
Regards Wilco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会。事件只会分派到具有焦点的组件。如果组件没有焦点,那么它不会接收系统生成的事件,例如 KeyEvents 和 MouseEvents。因此,如果面板不可见,则它将不会接收事件。
这不是最好的设计。最好使用为此设计的 卡片布局目的。
那么您的自定义绘画可能会遇到问题。当您只使用标准组件而不进行自定义绘制时会发生什么?
No. Events are only dispatched to the component that has focus. It a comonents doesn't have focus then it won't received system generated events like KeyEvents and MouseEvents. So if a panel isn't visible then it won't receive events.
This is not the best design. It is better to use a Card Layout which was designed for this purpose.
Then you probably have problems with your custom painting. What happens when you just use the standard components without custom painting?