Java Swing GUI JFrame中未显示图像

Java Swing GUI JFrame中未显示图像,java,swing,user-interface,jframe,Java,Swing,User Interface,Jframe,我有一个调用此函数的主函数: private void splashScreen() throws MalformedURLException { JWindow window = new JWindow(); ImageIcon image = new ImageIcon(new URL("https://i.imgur.com/Wt9kOSU.png")); JLabel imageLabel = new JLabel(image); window.add(

我有一个调用此函数的主函数:

private void splashScreen() throws MalformedURLException {
    JWindow window = new JWindow();
    ImageIcon image = new ImageIcon(new URL("https://i.imgur.com/Wt9kOSU.png"));
    JLabel imageLabel = new JLabel(image); 
    window.add(imageLabel);
    window.pack();
    window.setVisible(true);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    window.setVisible(false);
    window.dispose();
}
我已将图像添加到窗口,打包窗口,然后使其可见,框架弹出,但图像未显示在框架中。我很确定这段代码应该可以工作?

您使用的是Thread.sleep(),因此GUI会休眠,无法重新绘制自身。有关更多信息,请阅读上的Swing教程部分

不要使用Thread.sleep()

相反,使用
摆动计时器
将事件安排在5秒内。有关更多信息,请阅读Swing教程中的部分。

您使用的是Thread.sleep(),因此GUI处于休眠状态,无法重新绘制自身。有关更多信息,请阅读上的Swing教程部分

不要使用Thread.sleep()


相反,使用
摆动计时器
将事件安排在5秒内。阅读Swing教程中的部分以了解更多信息。

正如camickr所说,Swing计时器将是处理此问题的正确方法。但是,由于创建自定义线程是您将来要做的很多事情,下面是一个“手动”示例,介绍如何解决此问题:

private void showSplashScreen() {

    [Create window with everything and setVisible, then do this:]

    final Runnable threadCode = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(window::dispose); // Closes the window - but does so on the Swing thread, where it needs to happen.
        // The thread has now run all its code and will die gracefully. Forget about it, it basically never existed.
        // Btw., for the thread to be able to access the window like it does, the window variable either needs to be "final" (((That's what you should do. My mantra is, make EVERY variable (ESPECIALLY method parameters!) final, except if you need them changeable.))), or it needs to be on class level instead of method level.
    };

    final Thread winDisposerThread = new Thread(threadCode);
    winDisposerThread.setDaemon(true);  // Makes sure that if your application dies, the thread does not linger.
    winDisposerThread.setName("splash window closer timer"); // Always set a name for debugging.
    winDisposerThread.start(); // Runs the timer thread. (Don't accidentally call .run() instead!)
    // Execution of your program continues here IMMEDIATELY, while the thread has now started and is sleeping for 5 seconds.
}

正如camickr所说,摆动计时器将是处理此问题的正确方法。但是,由于创建自定义线程是您将来要做的很多事情,下面是一个“手动”示例,介绍如何解决此问题:

private void showSplashScreen() {

    [Create window with everything and setVisible, then do this:]

    final Runnable threadCode = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(window::dispose); // Closes the window - but does so on the Swing thread, where it needs to happen.
        // The thread has now run all its code and will die gracefully. Forget about it, it basically never existed.
        // Btw., for the thread to be able to access the window like it does, the window variable either needs to be "final" (((That's what you should do. My mantra is, make EVERY variable (ESPECIALLY method parameters!) final, except if you need them changeable.))), or it needs to be on class level instead of method level.
    };

    final Thread winDisposerThread = new Thread(threadCode);
    winDisposerThread.setDaemon(true);  // Makes sure that if your application dies, the thread does not linger.
    winDisposerThread.setName("splash window closer timer"); // Always set a name for debugging.
    winDisposerThread.start(); // Runs the timer thread. (Don't accidentally call .run() instead!)
    // Execution of your program continues here IMMEDIATELY, while the thread has now started and is sleeping for 5 seconds.
}
如果这应该是一个真正的“飞溅屏幕”(而不仅仅是一些实验/测试来熟悉Swing),那么你应该考虑可能,如果这应该是一个真正的“飞溅屏幕”(而不仅仅是一些实验/测试来熟悉Swing),那么你应该考虑到可能,