jsplash.java

来自「一个关于SWING学习的源代码. 大家过来看一看」· Java 代码 · 共 90 行

JAVA
90
字号
package com.pepsan.framework;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

/**
 *	Implements a basic splash screen for the Swing Application Framework.
 *	The splash screen will show for a fixed amount of time unles the user
 *	clicks the mouse on the window.
 *
 *	This code is based on code published by the Java Developer's Journal
 *  <http://www.sys-con.com/java/index2.html<
 *
 *	@version	1.0
 */

public class JSplash extends JWindow
	implements KeyListener, MouseListener, ActionListener {
	 
	/*
	 *  JSplash constructs a splash screen (JWindow).
	 *		parent is the parent frame for the window
	 *		filename is the JPEG/GIF file to show as the splash
	 *		timeout is time in milliseconds to display the splash
	 */
	public JSplash(JFrame parent, String filename, int timeout) { 
		super(parent); 
		
		// Note, this code does no error checking
		ImageIcon image = new ImageIcon(filename); 
		// The splash will be centered on the screen
		int w = image.getIconWidth() + 5; 
		int h = image.getIconHeight() + 5; 
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
		int x = (screen.width - w) / 2; 
		int y = (screen.height - h) / 2; 
		setBounds(x, y, w, h); 
		
		getContentPane().setLayout(new BorderLayout()); 
		JLabel picture = new JLabel(image); 
		getContentPane().add("Center", picture); 
		picture.setBorder(new BevelBorder(BevelBorder.RAISED)); 
		
		// Listen for key strokes 
		addKeyListener(this); 
		// Listen for mouse events from here and parent 
		addMouseListener(this); 
		parent.addMouseListener(this); 
		// Timeout after a while 
		Timer timer = new Timer(0, this); 
		timer.setRepeats(false); 
		timer.setInitialDelay(timeout); 
		timer.start(); 
	} 
	
	// This method is called in order to block the application until
	// the splash screen times out or is dismissed.
	// This is a bit of a kludge, actually, and the actual affect
	// varies based on platform.
	public void block() { 
		while(isVisible()) {} 
	} 
	
	// Dismiss the window on a key press, ignore rest.
	public void keyTyped(KeyEvent event) {} 
	public void keyReleased(KeyEvent event) {} 
	public void keyPressed(KeyEvent event) { 
		setVisible(false); 
		dispose(); 
	} 
	
	// Dismiss the window on a mouse click, ignore rest.
	public void mousePressed(MouseEvent event) {} 
	public void mouseReleased(MouseEvent event) {} 
	public void mouseEntered(MouseEvent event) {} 
	public void mouseExited(MouseEvent event) {} 
	public void mouseClicked(MouseEvent event) { 
		setVisible(false); 
		dispose(); 
	}
	
	// Dismiss the window on a timeout 
	public void actionPerformed(ActionEvent event) { 
		setVisible(false); 
		dispose(); 
	} 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?