我对JWindow有个问题。
这是我的全班学生,他们控制着JWindow:
public class NextLevelCounter {
JWindow window = new JWindow();
public static void main(String[] args) {
new NextLevelCounter();
}
public NextLevelCounter() {
window.getContentPane().add(new JLabel("Waiting"));
window.setBounds(0, 0, 300, 200);
window.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
window.dispose();
}
}当我从NextLevelCounter类运行main()时,它工作得很好,但是当我尝试从另一个类运行它时,它不会显示出来。例如:
这是另一门课:
private void isGameFinished() {
if(food.size() > 0)
return;
else if(food.size() == 0) {
timer.stop();
System.out.println("I am here");
new NextLevelCounter();
System.out.println("I am here 2");
this.level++;
}
}“我在这里”和“我在这里2”都有5000毫秒的差异(这是应该的),但是窗口没有显示出来。
我做错了什么?
编辑:
我使用JWindow是因为我想要一个没有边框的空窗口。
发布于 2015-07-24 14:56:06
沉睡的线程不能显示窗口。虽然在您的第一个示例中是这样做的,但这是错误的做法。使用swing员工在5秒后关闭窗口:
public class NextLevelCounter {
JWindow window = new JWindow();
public static void main(String[] args) {
new NextLevelCounter();
}
public NextLevelCounter() {
window.getContentPane().add(new JLabel("Waiting"));
window.setBounds(0, 0, 300, 200);
window.setVisible(true);
//Create a worker that whill close itself after 5 seconds. The main thread
//is notified and will dispose itself when worker finishes
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(5000);
return null;
}
protected void done() {
window.dispose();
}
};
worker.execute();
}
}https://stackoverflow.com/questions/31611708
复制相似问题