java - JWindow 在显示图像之前显示空白一秒钟

标签 java swing jwindow

我编写了一个 JWindow,用作我的桌面应用程序的启动屏幕。我遇到的问题是,窗口变得可见后,在显示预期图像之前它会暂时空白。空白窗口有时会保持 0.5 秒到 2 秒之间。我希望在窗口可见之前完全呈现内容。

我在 MacOS 上使用 Java 1.6。

这是我启动后立即出现的窗口:

enter image description here

仅仅半秒后,它就显示了图像。图像很小(约 95kBytes JPG)。我知道问题不在于 ImageIcon 加载,因为构造函数应该阻塞,直到加载图像。

enter image description here

这是我的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;

public class SplashScreen extends JWindow
{
    public SplashScreen()
    {
        ClassLoader classLoader = this.getClass().getClassLoader();
        ImageIcon imageIcon = new ImageIcon(classLoader.getResource("res/landscape.jpg"));
        while (imageIcon.getImageLoadStatus() != MediaTracker.COMPLETE) {}
        JLabel lbl = new JLabel(imageIcon);
        getContentPane().add(lbl, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);

        MouseListener mouseListener = new MouseAdapter()
        {
            public void mousePressed(MouseEvent event)
            {
                setVisible(false);
                dispose();
            }
        };

        addMouseListener(mouseListener);
        setVisible(true);
    }

    public static void main(String argv[])
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SplashScreen ss = new SplashScreen();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

编辑:在循环中添加了 imageIcon.getImageLoadStatus(),但没有效果。

最佳答案

尝试使用 JDK SplashScreen。该图像立即为我加载。

参见How to Create a Splash Screen .

关于java - JWindow 在显示图像之前显示空白一秒钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154812/

相关文章:

java - FileInputStream 和 FileOutputStream 类在 JAVA 中与文件描述符的 in 和 out 静态成员一起使用时的意外行为

Java - 是否可以将 setEditable(boolean b) 应用于 JSlider/

java - Java中如何检查文本框中是否输入了某个值

java - 绘制JSlider的 slider 图标

tomcat - 以编程方式识别 Tomcat 的 JDK 版本

java - Android Activity 因内容而产生黑屏

java - 删除在另一个 BufferedImage 上绘制的 BufferedImage

java - JWindow 总是在顶部没有获得焦点事件

java - 显示 JWindow 一段时间

java - 将可变长度的 JRadioButton 数组添加到 JWindow