从小程序关闭框架
我正在尝试在小程序中为 java 游戏制作一个控制台。控制台是一个带有 TextArea 的独立框架,用于显示加载/下载进度。 当我尝试在关闭时隐藏控制台时遇到一些问题。
这是简化的代码:
//Method inherited from the Applet class
public void init() {
console = new Frame();
console.setSize(500, 300);
console.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
console.setVisible(false);
}
});
consoleText = new TextArea();
consoleText.setSize(500, 300);
console.add(consoleText);
console.setVisible(true);
gameThread = new Thread() {
public void run() {
mainLoop();
}
};
gameThread.start();
}
当我关闭框架“控制台”时,线程“gameThread”只是挂起。即使我替换“console.setVisible(false);”使用“console.setExtendedState(Frame.ICONIFIED);”,线程仍然挂起,没有任何警告。 在浏览器中运行小程序时,我必须在任务管理器中终止该进程。很烦人。
使用 JFrame 可以起到完全相同的作用,只是该错误更难重现。
我只是希望用户能够摆脱控制台。
有人有想法吗?谢谢 !
I'm trying to make a console for a java game, in an applet. The console is an independant Frame with a TextArea, used to show loading/downloading progression.
I'm running into some problems when I try to hide the console on closing.
Here is the simplified code :
//Method inherited from the Applet class
public void init() {
console = new Frame();
console.setSize(500, 300);
console.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
console.setVisible(false);
}
});
consoleText = new TextArea();
consoleText.setSize(500, 300);
console.add(consoleText);
console.setVisible(true);
gameThread = new Thread() {
public void run() {
mainLoop();
}
};
gameThread.start();
}
The thread "gameThread" simply hang when I close the Frame "console". Even when I replace "console.setVisible(false);" with "console.setExtendedState(Frame.ICONIFIED);", the thread still hang without any warning.
While running the applet in a browser, I have to kill the process in the task manager. Very annoying.
Using a JFrame instead does the exact same thing, except the bug is harder to reproduce.
I just want the user to be able to get rid of the console.
Does anybody have an idea ? Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不应该为此使用 Frame/JFrame 而应该使用 JDialog,因为窗口的行为就像一个对话框。还要确保您使用的是 JApplet 而不是 Applet。
编辑
请注意,我无法根据您显示的代码片段重现您的问题。考虑创建并发布一个 SSCCE 来直接向我们展示问题。
编辑2
我的 SSCCE 未重现您的问题:
编辑 3
我使用 JDialog 的 SSCCE:
I think that you shouldn't be using a Frame/JFrame for this but rather a JDialog, since the window is behaving as a dialog. Also be sure that you're using a JApplet rather than an Applet.
Edit
Note that I cannot reproduce your problem based on your displayed code snippet. Consider creating and posting an SSCCE that would show us the problem directly.
Edit 2
My SSCCE that does not reproduce your problem:
Edit 3
My SSCCE using a JDialog: