不好意思呀,从网上找到的源代码,再找时,找不到了原网址了,表示歉意。
java 应用程序,闪屏,也就是弹出一个图片,几秒后显示主程序,或是主程序的登录界面。网上的应用例子应该有好几种,这里给出一种,在闪屏下面加动态文字提示的闪屏。
源代码:
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) { try { 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(); } catch (Exception e) { System.out.println(e.toString()); } try { Thread.sleep(3000); // 这儿决定SplashScreen显示时间长短. } catch (Exception e) { System.out.println(e.toString()); } // 对于实际应用程序,这儿对应程序的初始化工作 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); } }
上面的这个例子,不能在eclipse 下运行可以显示闪屏效果。把应用程序打包成jar格式的文件后,运行才可以显示。在工程下右键,导出-导出为jar文件。
1.在src同级目录下创建MANIFEST.MF文件,内容如下:
Manifest-Version: 1.0
SplashScreen-Image: image/splash.jpg
Main-Class: cmsdb.gui.Frm_main
Class-Path: . lib/msbase.jar lib/mssqlserver.jar lib/msutil.jar
2.在src同级目录下,创建lib文件夹,用来存放数据库连接库jar
3.在src目录下,创建一个image文件夹,里面存放splash.jpg
4.打包后的jar文件和src 放在同级目录下。
打包后,运行下,看下效果吧。