如何让JWindow立即绘制?在下面的示例中,从日志输出“完成”到“绘制jwindow”,在窗口显示和绘制之间有整整2秒的时间。当窗口第一次显示时,它是空的。两秒钟后,将显示图像“splash-creen.png”。
我不想使用Java内置的闪屏支持。它有问题。求求你,我们别谈这个了。我已经花了几天时间研究它..。它不适用于我的情况(这里没有完整描述)。
我已经连续多次运行这段代码,看看延迟是否减少了……它保持在2秒...始终如一。我在macOS 10.14.1和JDK10.0.2上运行。
这是我的SSCCE:
import java.util.logging.Logger;
import javax.swing.JWindow;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.BorderLayout;
public class MyApp {
private final static Logger LOG = Logger.getLogger(MyApp.class.getName());
public static void main(String args[]) throws Exception {
final JWindow[] startupScreen = {null};
SwingUtilities.invokeAndWait(() -> {
try {
Thread.currentThread().setPriority(10);
LOG.info("edt thread priority: "+Thread.currentThread().getPriority());
startupScreen[0] = new JWindow() {
@Override
public void paint(Graphics g) {
super.paint(g);
LOG.info("painting jwindow");
}
};
/*
LOG.info("set layout");
startupScreen[0].setLayout(new BorderLayout());
*/
LOG.info("add splash image");
startupScreen[0].add(
new JLabel(new ImageIcon(
MyApp.class.getResource("splash-screen.png")
)), BorderLayout.CENTER);
LOG.info("pack");
startupScreen[0].pack();
LOG.info("set relative location");
startupScreen[0].setLocationRelativeTo(null);
LOG.info("set visible");
startupScreen[0].setVisible(true);
startupScreen[0].repaint();
startupScreen[0].revalidate();
LOG.info("done");
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
}发布于 2018-12-02 02:00:15
可能的瓶颈:
startupScreen[0].add(new JLabel(new ImageIcon(
MyApp.class.getResource("splash-screen.png"))),
BorderLayout.CENTER);或者更具体地说,在这里:
MyApp.class.getResource("splash-screen.png")因为您正在读入一个未压缩的、可能很大的.png图像资源。解决方案包括不需要图像或读取较小的压缩.jpg图像。
https://stackoverflow.com/questions/53573524
复制相似问题