`

【原创】 java 启动画面(闪屏)splash

阅读更多

关于java jar包启动画面(闪屏)splash

 

目的:在启动jar时,显示启动画面,并动态显示文字内容。

具体代码如下:

package cmsdb.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.io.FileInputStream;
import java.util.Scanner;

public class Frm_main{
    public static void main(String[] args){
    	
    	
    	new Thread() {
    		 SplashScreen splash =SplashScreen.getSplashScreen();
			private FileInputStream fis;
			private Scanner scanner;
			final Graphics2D g2 = splash.createGraphics();
			final Dimension size = splash.getSize();
			
			public void run() {
				try {
					drawInfo("程序启动中……");
					//initAndRecLog();
					// 初始化并记录日志
//					System.out.println("系统正在启动中......");
//					System.out.println("系统启动正常......");
//					System.out.println("进入数据库操作主窗体......");
//					System.out.println("成功进入登陆窗口......");
					fis = new FileInputStream("splash.log");
	
					scanner = new Scanner(fis);
					while (scanner.hasNextLine()) {
						String str = scanner.nextLine();
						str="启动信息:"+str;
						drawInfo(str);
						Thread.sleep(800);
					}
					scanner.close();
					fis.close();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			// 初始化系统,并记录日志
//			private void initAndRecLog() throws FileNotFoundException {
//				FileOutputStream fop = new FileOutputStream("AutoBack.log");
//				PrintStream ps = new PrintStream(fop);
//				System.setOut(ps);
//				
//			}
			// 在闪屏界面绘制文本信息
			private void drawInfo(String info) {
				g2.setColor(Color.BLACK);
				g2.fillRect(0, size.height - 20, size.width, 20);
				g2.setColor(Color.GREEN);
				g2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
				g2.drawString(info, 10, size.height - 5);
				splash.update();
			}
		}.start();
        try{
            Thread.sleep(3000);                        //这儿决定SplashScreen显示时间长短.                                                             
        }catch(Exception e){}                         //对于实际应用程序,这儿对应程序的初始化工作
        WelcomeFrame wf = new WelcomeFrame(); 
        wf.setVisible(true);
    }
}
 
package cmsdb.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class WelcomeFrame extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WelcomeFrame frame = new WelcomeFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public WelcomeFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

}

 1、新建文件夹:META-INF 为空。新建image文件夹,放入显示图片splash.jpg

 2、新建文件 MANIFEST.MF

Manifest-Version: 1.0
Created-By: Fat Jar Eclipse Plug-In
SplashScreen-Image: image/splash.gif
Main-Class: cmsdb.gui.Frm_main
Class-Path: . ./lib

 3、新建文件:splash.log 即想显示的文字内容:

系统正在启动中......
系统启动正常......
进入数据库操作主窗体......
成功进入数据库操作主窗体......

 4、我使用build Fat Jar 插件,打包。

    具体设置 见附件

 

5、选择 MANIFEST.MF 文件,记得要选择 META-INF文件夹 和 splash.jpg 一起 打包。否则

 

欢迎转载,请注明出处。

 

 

  • 大小: 21.8 KB
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics