我正在尝试实现一个简单的闪屏。因此,我有两个类,Loader
它们是的子类JFrame
和Background
(的子类JComponent
,用作GlassPane
)来保存实际图像。在这张图片下方应该有一个小的进度条。
加载图像后,我会立即显示窗口。这工作得很好。但它不会显示图像。滚动条占据整个框架。如果我手动缩小窗口并将其放回前面,则会显示图像。
为什么图像没有立即显示?
这是代码:
装载机
public class Loader extends JFrame{
private static final int _WIDTH = 500;
private static final int _HEIGHT = 300;
private JProgressBar bar;
private Background bg;
public Loader (){
super("Risiko Loader");
this.setAlwaysOnTop(true);
Dimension window = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(window.width / 2 - _WIDTH / 2, window.height / 2 - _HEIGHT / 2, _WIDTH, _HEIGHT);
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setResizable(false);
this.setFocusable(true);
this.setBackground(new Color(1.0f,0.0f,1.0f,1.0f));
this.setFocusableWindowState(true);
this.setIcons();
bar = new JProgressBar();
bar.setSize(_WIDTH, 20);
bar.setLocation(0, _HEIGHT - 20);
bar.setValue(0);
bar.setMinimum(0);
bar.setMaximum(100);
this.add(bar);
bg = new Background();
this.setGlassPane(this.bg);
this.bgIsReady();
}
private void bgIsReady(){
do {
try {
Thread.sleep(100);
}catch(Exception e){}
}while(this.bg.tracker.statusID(1, false) == MediaTracker.COMPLETE);
this.setVisible(true);
this.getGlassPane().setVisible(true);
this.getGlassPane().repaint();
bar.setValue(50);
}
private void setIcons(){
ArrayList<Image> icons = new ArrayList<>();
icons.add(Toolkit.getDefaultToolkit().getImage(System.getProperty("user.dir") + File.separator + "images" + File.separator + "icon16.png"));
icons.add(Toolkit.getDefaultToolkit().getImage(System.getProperty("user.dir") + File.separator + "images" + File.separator + "icon32.png"));
this.setIconImages(icons);
}
public static void main(String [] args){
Loader l = new Loader();
}
}
和背景
public class Background extends JComponent{
private Image img;
public MediaTracker tracker;
public Background(){
this.tracker = new MediaTracker(this);
String imgPath = System.getProperty("user.dir") + File.separator + "images" + File.separator + "splash.png";
this.img = Toolkit.getDefaultToolkit().getImage(imgPath);
this.tracker.addImage(this.img, 1);
}
@Override
public void paintComponent(Graphics g) {
if(this.tracker.statusID(1, false) == MediaTracker.LOADING) {//not loaded
return;
}
// Draw the background image
g.drawImage(this.img, 1, 1, null);
System.out.println("p");
}
}