我们使用Eclipse的时候,经常先看到一个Eclipse的启动窗口。等Eclipse的类库等东西加载完后,才会消失掉。
这种效果也同样可以用在Swing的程序上。
这里我写了一个例子。
package com.tntxia.example;
import java.awt.*;
import java.io.File;
import javax.swing.*;
public class WindowSplashFrame extends JFrame {
private JWindow splashWin;
public void prepareSplash() {
splashWin = new JWindow(this);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit
.getImage("images" + File.separator + "splash.jpg");
JLabel label = new JLabel();
label.setSize(new Dimension(100,100));
label.setIcon(new ImageIcon(image));
label.setBackground(Color.red);
label.setVisible(true);
splashWin.add(label);
Dimension scmSize = toolkit.getScreenSize();
int imgWidth = image.getWidth(this);
int imgHeight = image.getHeight(this);
System.out.println(imgWidth+","+ imgHeight);
splashWin.setLocation(scmSize.width / 2 - (imgWidth / 2), scmSize.height / 2
- (imgHeight / 2));
splashWin.setSize(imgWidth, imgHeight);
splashWin.setVisible(true);
}
public void startSplash(){
splashWin.setVisible( true );
splashWin.toFront();
}
public void stopSplash(){
splashWin.dispose();
}
public static void main(String args[]){
WindowSplashFrame frame = new WindowSplashFrame();
frame.prepareSplash();
frame.startSplash();
try{
Thread.currentThread().sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
frame.stopSplash();
}
}
把正确的图片,放在项目的根目录下,运行就可以看到效果了。效果非常漂亮,5秒后,图片自动消失。