当浏览器关闭时,如何在循环中杀死 Java-Applet? [英] How to kill Java-Applet beeing in a loop when browser is closed?

查看:27
本文介绍了当浏览器关闭时,如何在循环中杀死 Java-Applet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java Applet 来显示图像.我在paint()中有一个真正的循环,以便通过tcp/ip套接字永久接收新图像.现在我遇到了问题,即使选项卡已关闭,该应用程序仍在从服务器请求新图像.我该如何解决?

I have a Java Applet to show images. I have a while true loop in paint() in order to permantly receive new images via tcp/ip socket. Now I have the problem, that the app is still requesting new Images from the server, even when the tab is closed. How can I fix that?

推荐答案

简单说一下:

我在 paint() 中有一个 while(true) 循环,以便通过 tcp/ip 套接字永久接收新图像.

I have a while(true) loop in paint() in order to permanently receive new images via tcp/ip socket.

不要这样做.绘制方法不应阻塞更长的时间范围.它应该绘制你的图像,别无他物.(只要 Paint 方法正在运行,GUI 中的其他任何东西都无法执行任何操作.这对您的小程序来说可能并不重要,但对于更复杂的程序来说却很重要.)

Don't do this. The paint method should never block for a longer time frame. It should only paint your images, nothing else. (As long as the paint method is running, nothing else in your GUI can do anything. This may not be critical for your applet, but it will be for more complicated programs.)

如果您需要加载新图像,请在单独的线程中执行此操作,将图像放入变量中,并在最后调用 repaint().然后paint方法从变量中获取图像并将其绘制到屏幕上.

If you need to load new images, do this in a separate thread, put the image in a variable, and call repaint() at the end. The paint method then takes the image from the variable and draws it to the screen.

这篇关于当浏览器关闭时,如何在循环中杀死 Java-Applet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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