Java,小程序:如何在 init() 完成工作之前阻止激活paint() [英] Java, applet: How to block the activation of paint() before init() finishes it's work

查看:17
本文介绍了Java,小程序:如何在 init() 完成工作之前阻止激活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() 方法的工作,放置在方法的开头,直到 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...)?

我正在使用 Dr. Java - 出于某种原因,它在两台不同的计算机上以不同的方式运行小程序:一台运行 Win XP 的非常旧的笔记本电脑(7 岁)和一台 2 岁的台式 PC用 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...

在桌面上使用 Dr. Java 进行测试时不会出现此问题.并且在浏览器上运行小程序时不会出现此问题.只有在笔记本电脑上安装了编辑器时才会出现这种情况.我想问题出在旧技术"上运行的代码编辑器中,而不是代码中.

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 和paint 正在被两个不同的线程调用.

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. 在 init 方法中使用 EventQueue.invokeLater 并将初始化代码放入其中,完成后调用 repaint

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

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