简体   繁体   English

该图像未显示在Java Swing GUI JFrame中

[英]Image not showing up in Java Swing GUI JFrame

I have a main function that calls this function: 我有一个调用此功能的主要功能:

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();
}

I have added the image to the window, packed the window and then made it visible, the frame pops up but, the image does not show up in the frame. 我已将图像添加到窗口中,打包了窗口,然后使其可见,弹出了框架,但是图像未显示在框架中。 I am fairly certain that this code should work? 我相当确定该代码应该起作用?

You are using Thread.sleep() so the GUI sleeps and can't repaint itself. 您正在使用Thread.sleep(),因此GUI处于睡眠状态,无法重新绘制自身。 Read the section from the Swing tutorial on Concurrency for more information. 阅读Swing 并发教程中的有关更多信息的部分。

Don't use Thread.sleep(). 不要使用Thread.sleep()。

Instead use a Swing Timer to schedule the event in 5 seconds. 而是使用Swing Timer在5秒内安排事件。 Read the section from the Swing tutorial on How to Use Timers for more information. 阅读Swing教程中有关如何使用计时器的部分, 获取更多信息。

Like camickr said, a Swing Timer would be the proper way to deal with this. 就像camickr所说的那样,Swing Timer是解决此问题的正确方法。 But since creating custom threads is something you will do a lot in the future, here's a "manual" example for how you could solve this: 但是由于创建自定义线程是您将来要做的事情,因此,这里有一个“手动”示例,介绍了如何解决此问题:

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM