问题
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?
回答1:
Just to make it clear:
I have a
while(true)
loop inpaint()
in order to permanently receive new images via tcp/ip socket.
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.)
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.
回答2:
<script>
function closeIt()
{
document.<appletContainer>.APPLETID.loaded = 0;
}
window.onbeforeunload = closeIt;
window.onunload = closeIt;
</script>
and add public int loaded = 1;
to your applet code
Now, check the loaded variable's value before executing your code in your applet.
回答3:
Override Applet.destroy().
Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated. ..
来源:https://stackoverflow.com/questions/6323773/how-to-kill-java-applet-beeing-in-a-loop-when-browser-is-closed