自定义Java启动屏幕"冻结",直到整个应用程序加载完毕

And*_*ith 5 java swing splash-screen

我有一个程序,需要很长时间才能加载.因此,我想开发一个启动画面,可以向用户提供有关正在加载的内容的反馈.一个简单的JFrame,带有图像,标签和JProgressBar.

我一直在进行实验,并且在我的工作中取得了最好的成绩main():

SwingUtilities.invokeAndWait(new Runnable() {

    public void run() {

        new SplashScreen();
    }
});

SwingUtilities.invokeAndWait(new Runnable() {

    public void run() {

        //Code to start system
        new MainFrame();
        //.. etc
    }
});
Run Code Online (Sandbox Code Playgroud)

SplashScreen和MainFrame都是扩展JFrame的类.我也使用Substance作为图书馆.

SplashScreen的构造函数将JLabel和JProgressBar添加到自身,包和集可见.JProgressBar是setIndeterminate(true);

当我运行我的程序时,我的SplashScreen会显示但ProgressBar会被锁定,它不会移动,直到程序的其余部分启动它才会按预期开始移动.

我在这里错过了什么?我所做的所有搜索似乎都没有提到这个问题,大多数"自定义启动画面"实现与我自己的方式非常类似.

Rus*_*ard 1

其他答案已经涵盖了大部分内容,但简而言之,您的问题是您正在 Swing 事件调度线程中运行“启动系统的代码”。所有与 GUI 相关的代码(包括组件创建)必须在 EDT 上运行,但所有其他代码不应在 EDT 上运行。尝试更改您的程序来执行此操作:

SwingUtilities.invokeAndWait(new Runnable() {
    public void run() {
        new SplashScreen();
    }
});
// Code to start system (nothing that touches the GUI)
SwingUtilities.invokeAndWait(new Runnable() {
    public void run() {
        new MainFrame();
    }
});
//.. etc
Run Code Online (Sandbox Code Playgroud)