程序启动界面

在应用程序启动的时候,如果初始化的时间较长,通常需要安排一个表示欢迎使用本程序的启动界面,在初始化完成后该界面自动消失,这就是平时所说的Splash Window(快闪窗口)。

/************************************************
 * 程序文件名称:JSplashWindow.java
 * 功能:程序启动界面
 ***********************************************/
import javax.swing.*;
import java.awt.*;
import java.net.*;

public class JSplashWindow extends JWindow implements Runnable{
	Thread splashThread = null;
	public JSplashWindow(){
		JPanel splash = new JPanel(new BorderLayout());
		URL url=getClass().getResource("elephant.jpg");//在JSplashWindow.java文件相同路径下添加一图片,图片名称为elephant.jpg
		if(url!=null){
			splash.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER);
		}
		setContentPane(splash);
		Dimension screen = getToolkit().getScreenSize();//获取屏幕分辨率
		pack();
		setSize(500,400);
		setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2);
		//使窗口居中显示
		
	}
	public void start(){
		this.toFront();//当前窗口在最前面显示
		splashThread=new Thread(this);
		splashThread.start();
	}
	public void run(){
		try{
			show();
			Thread.sleep(3000);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		this.dispose();//关闭窗口
	}
	static void showFrame(String title){
		JFrame frame=new JFrame(title);
		frame.setSize(400, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize=frame.getSize();
		
		if(frameSize.height>screenSize.height){
			frameSize.height=screenSize.height;
		}
		if(frameSize.width>screenSize.width){
			frameSize.width=screenSize.width;
		}
		frame.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2);
		Label lb=new Label("进入应用系统");
		frame.getContentPane().add(lb, BorderLayout.CENTER);
		lb.setFont(new Font("default",1,36));
		frame.setVisible(true);
	}
	public static void main(String[] args){
		showFrame("Do splash window");
		JSplashWindow splash=new JSplashWindow();
		splash.start();
	}
	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值