Swing用一张图片作为程序的启动页面

我们使用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秒后,图片自动消失。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值