我编写了一个 JWindow,用作我的桌面应用程序的启动屏幕。我遇到的问题是,窗口变得可见后,在显示预期图像之前它会暂时空白。空白窗口有时会保持 0.5 秒到 2 秒之间。我希望在窗口可见之前完全呈现内容。
我在 MacOS 上使用 Java 1.6。
这是我启动后立即出现的窗口:
仅仅半秒后,它就显示了图像。图像很小(约 95kBytes JPG)。我知道问题不在于 ImageIcon 加载,因为构造函数应该阻塞,直到加载图像。
这是我的代码:
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。该图像立即为我加载。
关于java - JWindow 在显示图像之前显示空白一秒钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154812/