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 = 开发者_JS百科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 !
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:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppletEg extends JApplet {
private static final int MAX_LOOP = 30;
private static final long SLEEP_TIME = 500;
private JFrame console;
private JTextArea consoleText;
private Thread gameThread;
@Override
public void init() {
console = new JFrame();
console.setSize(500, 300);
console.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
console.setVisible(false);
}
});
consoleText = new JTextArea();
consoleText.setPreferredSize(new Dimension(500, 300));
console.add(new JScrollPane(consoleText));
console.setVisible(true);
gameThread = new Thread() {
public void run() {
mainLoop();
}
};
gameThread.start();
}
private void mainLoop() {
for (int i = 0; i < MAX_LOOP; i++) {
System.out.println("I: " + i);
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
}
}
}
}
Edit 3
My SSCCE using a JDialog:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppletEg extends JApplet {
private static final int MAX_LOOP = 30;
private static final long SLEEP_TIME = 500;
private JDialog console;
private JTextArea consoleText;
private Thread gameThread;
@Override
public void init() {
Window win = SwingUtilities.getWindowAncestor(this);
console = new JDialog(win);
consoleText = new JTextArea();
consoleText.setPreferredSize(new Dimension(500, 300));
console.add(new JScrollPane(consoleText));
console.pack();
console.setLocationByPlatform(true);
console.setVisible(true);
gameThread = new Thread() {
public void run() {
mainLoop();
}
};
gameThread.start();
}
private void mainLoop() {
for (int i = 0; i < MAX_LOOP; i++) {
System.out.println("i: " + i);
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
}
}
}
}
精彩评论