Java中,小程序:如何初始化(前挡paint()方法的激活),完成它的工作 [英] Java, applet: How to block the activation of paint() before init() finishes it's work

查看:253
本文介绍了Java中,小程序:如何初始化(前挡paint()方法的激活),完成它的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,该方法的paint()被调用的激活一段时间后(不能立即发生,可以吗?)的init(),不是结束后。我有获得创建的init()并制定了的paint()方法的几个对象。但是,开始绘制对象获得初始化之前。这将导致异常,被自动处理。但是,这也导致无法获得的首次激活后绘制的对象的paint() - 他们需要以显示重绘

I found out that the method paint() gets called some time (it can't happen immediately, can it?) after the activation of init(), not after it finishes. I have a few objects that get created in init() and drawn in the paint() method. But the drawing starts before the objects get initialized. This causes exceptions, that are handled automatically. But it also causes the objects not to get drawn after the the first activation of paint()- they need to be redrawn in order to show up.

我能够阻止的paint()方法的具有无限循环,放置的int方法的开头,并不能阻止工作,直到的init()完成它的工作(我猜的init()的paint()在单独的线程中运行)。但是,一个采用Java程序员告诉我,这是不是一个优雅的解决方案 - 我应该尝试做不同的事情(这家伙没有告诉我做什么,他没有与小程序的工作,我想,他从来没有遇到过这种的问题,这就是为什么我问这里)。

I was able to block the paint() method's work with an infinite cycle, placed int the beginning of the method, that doesn't stop until init() finishes it's work (I guess init() and paint() run in separate threads). But an employed Java programmer told me that this isn't an elegant solution- I should try to do something different (the guy didn't tell me what to do, he is not working with applets and I guess, he has never encountered this problem, that's why I'm asking here).

我怎样才能确保的paint()方法不前激活的init()工作结束和我怎么能使其在一个优雅的方式(什么都认为是应该在这种情况下的意思)?

How can I make sure that the paint() method doesn't activate before init() finishes working, and how can I make it in an elegant way (what ever that's supposed to mean in this case...)?

编辑:

我使用Java的医生出于某种原因,它不同运行小程序在两个不同的计算机:一个真正的旧笔记本电脑(7岁),与Win XP的运行和一个2岁的台式电脑,奔跑与Win 7,我已经犯下的错误不要用浏览器测试...

I am using Dr. Java- for some reason, it runs the applet differently on two different computers: a really old laptop (7-years-old) that runs with Win XP and a 2-years-old desktop PC that runs with Win 7. I have made the mistake not to test with a browser...

与Java博士在桌面上进行测试时不会发生此​​问题。运行在浏览器上的小应用程序时不会出现问题。它仅出现在安装在笔记本电脑的编辑器。我想这个问题是在旧技术运行code编辑器,而不是在code。

The problem doesn't occur when testing with Dr. Java on the desktop. And the problem doesn't occur when running the applet on a browser. It only occurs with the editor installed on the laptop. I guess the problem is in the code editor running on the "old tech", not in the code.

推荐答案

简短的回答是你不能。 init和油漆都被称为,因为你怀疑,由两个不同的线程。

The short answer is you can't. Init and paint are being called, as you suspected, by two different threads.

最优雅的解决方案,我认为过是

The most elegant solutions I think off of is


  1. 检查在paint方法零点

  2. 使用EventQueue.invokeLater在init方法,并把其中的初始化code,调用重绘当你完成

这篇关于Java中,小程序:如何初始化(前挡paint()方法的激活),完成它的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆