首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JWindow -没有显示

JWindow -没有显示
EN

Stack Overflow用户
提问于 2015-07-24 13:23:38
回答 1查看 709关注 0票数 0

我对JWindow有个问题。

这是我的全班学生,他们控制着JWindow:

代码语言:javascript
复制
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()时,它工作得很好,但是当我尝试从另一个类运行它时,它不会显示出来。例如:

这是另一门课:

代码语言:javascript
复制
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是因为我想要一个没有边框的空窗口。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-24 14:56:06

沉睡的线程不能显示窗口。虽然在您的第一个示例中是这样做的,但这是错误的做法。使用swing员工在5秒后关闭窗口:

代码语言:javascript
复制
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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31611708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档