Java/Swing JFC/Splash Screen
Содержание
- 1 A progress bar indicating the progress of application initialization
- 2 A simple application to show a title screen in the center of the screen
- 3 A simple Splash screen
- 4 A splash screen for an application
- 5 Class representing an application splash screen
- 6 JSplash extends JWindow
- 7 Simple splash screen
- 8 Splash Screen based on JWindow
A progress bar indicating the progress of application initialization
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.SplashScreen; import javax.swing.JFrame; import javax.swing.JLabel; public class Main {
public static void main(String args[]) throws Exception{ SplashScreen splash = SplashScreen.getSplashScreen(); Graphics2D g = (Graphics2D) splash.createGraphics(); Dimension dim = splash.getSize(); for (int i = 0; i < 100; i++) { g.setColor(Color.RED); g.fillRect(50, 50, dim.width - 100, dim.height - 100); splash.update(); try { Thread.sleep(250); } catch (InterruptedException ignored) { } } JFrame frame = new JFrame("Splash Me2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello, Splash", JLabel.CENTER); frame.add(label, BorderLayout.CENTER); frame.setSize(300, 95); frame.setVisible(true); }
}
</source>
A simple application to show a title screen in the center of the screen
<source lang="java">
/* Java Swing, 2nd Edition By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole ISBN: 0-596-00408-7 Publisher: O"Reilly
- /
// SplashScreen.java //A simple application to show a title screen in the center of the screen //for the amount of time given in the constructor. This class includes //a sample main() method to test the splash screen, but it"s meant for use //with other applications. // import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JWindow; public class SplashScreen extends JWindow {
private int duration; public SplashScreen(int d) { duration = d; } // A simple little method to show a title screen in the center // of the screen for the amount of time given in the constructor public void showSplash() { JPanel content = (JPanel) getContentPane(); content.setBackground(Color.white); // Set the window"s bounds, centering the window int width = 450; int height = 115; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width - width) / 2; int y = (screen.height - height) / 2; setBounds(x, y, width, height); // Build the splash screen JLabel label = new JLabel(new ImageIcon("1.gif")); JLabel copyrt = new JLabel("Copyright 2002, O"Reilly & Associates", JLabel.CENTER); copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12)); content.add(label, BorderLayout.CENTER); content.add(copyrt, BorderLayout.SOUTH); Color oraRed = new Color(156, 20, 20, 255); content.setBorder(BorderFactory.createLineBorder(oraRed, 10)); // Display it setVisible(true); // Wait a little while, maybe while loading resources try { Thread.sleep(duration); } catch (Exception e) { } setVisible(false); } public void showSplashAndExit() { showSplash(); System.exit(0); } public static void main(String[] args) { // Throw a nice little title page up on the screen first SplashScreen splash = new SplashScreen(10000); // Normally, we"d call splash.showSplash() and get on with the program. // But, since this is only a test... splash.showSplashAndExit(); }
}
</source>
A simple Splash screen
<source lang="java">
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002. * All rights reserved. Software written by Ian F. Darwin and others. * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS"" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s, * pioneering role in inventing and promulgating (and standardizing) the Java * language and environment is gratefully acknowledged. * * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for * inventing predecessor languages C and C++ is also gratefully acknowledged. */
import java.awt.Color; import java.awt.*; import java.awt.Graphics; import java.awt.MediaTracker; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JWindow; /** A simple Splash screen. */ public class Splash extends JWindow {
protected ImageIcon im; public Splash(JFrame f, String progName, String fileName) { super(); // Can"t use Swing border on JWindow: not a JComponent. // setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); im = new ImageIcon(fileName); if (im.getImageLoadStatus() != MediaTracker.ruPLETE) JOptionPane.showMessageDialog(f, "Warning: can"t load image " + fileName + "\n" + "Please be sure you have installed " + progName + " correctly", "Warning", JOptionPane.WARNING_MESSAGE); int w = im.getIconWidth(), h = im.getIconHeight(); setSize(w, h); UtilGUI.center(this); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { dispose(); } }); addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { dispose(); } }); } public void paint(Graphics g) { im.paintIcon(this, g, 0, 0); g.setColor(Color.green); g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 7, 7); }
} class UtilGUI {
/** Centre a Window, Frame, JFrame, Dialog, etc. */ public static void centre(Window w) { // After packing a Frame or Dialog, centre it on the screen. Dimension us = w.getSize(), them = Toolkit.getDefaultToolkit() .getScreenSize(); int newX = (them.width - us.width) / 2; int newY = (them.height - us.height) / 2; w.setLocation(newX, newY); } /** * Center a Window, Frame, JFrame, Dialog, etc., but do it the American * Spelling Way :-) */ public static void center(Window w) { UtilGUI.centre(w); } /** Maximize a window, the hard way. */ public static void maximize(Window w) { Dimension us = w.getSize(), them = Toolkit.getDefaultToolkit() .getScreenSize(); w.setBounds(0, 0, them.width, them.height); }
}
</source>
A splash screen for an application
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JWindow; class SplashScreen extends JWindow {
private int duration; public SplashScreen(int d) { duration = d; JPanel content = (JPanel) getContentPane(); content.setBackground(Color.white); int width = 450; int height = 115; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width - width) / 2; int y = (screen.height - height) / 2; setBounds(x, y, width, height); content.add(new JLabel("asdf"), BorderLayout.CENTER); Color oraRed = new Color(156, 20, 20, 255); content.setBorder(BorderFactory.createLineBorder(oraRed, 10)); setVisible(true); try { Thread.sleep(duration); } catch (Exception e) { } setVisible(false); } public static void main(String[] args) { SplashScreen splash = new SplashScreen(10000); }
}
</source>
Class representing an application splash screen
<source lang="java">
/*
* (C) 2004 - Geotechnical Software Services * * This code is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */
//package no.geosoft.cc.ui;
import java.net.URL; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.Timer; import javax.swing.JWindow;
/**
* Class representing an application splash screen.*
* Typical usage: *
* SplashScreen splashScreen = new SplashScreen ("/com/company/splash.jpg"); * splashScreen.open (3000); *
* * @author */
public class SplashScreen extends JWindow {
private Image image_; private int x_, y_, width_, height_;
/** * Create a new splash screen object of the specified image. * The image file is located and referred to through the deployment, not * the local file system; A typical value might be "/com/company/splash.jpg". * * @param imageFileName Name of image file resource to act as splash screen. */ public SplashScreen (String imageFileName) { super (new Frame()); try { Toolkit toolkit = Toolkit.getDefaultToolkit(); URL imageUrl = getClass().getResource (imageFileName); image_ = toolkit.getImage (imageUrl); MediaTracker mediaTracker = new MediaTracker (this); mediaTracker.addImage (image_, 0); mediaTracker.waitForID (0); width_ = image_.getWidth (this); height_ = image_.getHeight (this); Dimension screenSize = toolkit.getScreenSize(); x_ = (screenSize.width - width_) / 2; y_ = (screenSize.height - height_) / 2; } catch (Exception exception) { exception.printStackTrace(); image_ = null; } } /** * Open the splash screen and keep it open for the specified duration * or until close() is called explicitly. */ public void open (int nMilliseconds) { if (image_ == null) return; Timer timer = new Timer (Integer.MAX_VALUE, new ActionListener() { public void actionPerformed (ActionEvent event) { ((Timer) event.getSource()).stop(); close(); }; }); timer.setInitialDelay (nMilliseconds); timer.start(); setBounds (x_, y_, width_, height_); setVisible (true); }
/** * Close the splash screen. */ public void close() { setVisible (false); dispose(); } /** * Paint the splash screen window. * * @param graphics The graphics instance. */ public void paint (Graphics graphics) { System.out.println ("paint"); if (image_ == null) return; graphics.drawImage (image_, 0, 0, width_, height_, this); }
}
</source>
JSplash extends JWindow
<source lang="java">
/*
* Copyright (C) 2004 Giuseppe MANNA * * This file is part of FreeReportBuilder * * FreeReportBuilder is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */
import java.awt.*; import javax.swing.border.*; import javax.swing.*; public final class JSplash extends JWindow {
private JLabel lblVersion = new JLabel(); public JSplash() { init(); center(); } private void init() { JPanel pnlImage = new JPanel(); ImageIcon image = new ImageIcon( getClass().getResource( "img/logo.jpg" ) ); JLabel lblBack = new JLabel( image ); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border loweredbevel = BorderFactory.createLoweredBevelBorder(); lblBack.setBounds( 0, 0, image.getIconWidth(), image.getIconHeight() ); getLayeredPane().add( lblBack, new Integer( Integer.MIN_VALUE ) ); pnlImage.setLayout( null ); pnlImage.setOpaque( false ); pnlImage.setBorder( BorderFactory.createCompoundBorder( raisedbevel, loweredbevel ) ); pnlImage.add( this.lblVersion ); this.lblVersion.setForeground( Color.white ); this.lblVersion.setFont( new Font( "Dialog", Font.PLAIN, 12 ) ); this.lblVersion.setBounds( 15, 69, 120, 20 ); setContentPane( pnlImage ); setSize( image.getIconWidth(), image.getIconHeight() ); } private void center() { Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); int nX = (int) (scr.getWidth() - getWidth() ) / 2; int nY = (int) (scr.getHeight() - getHeight() ) / 2; setLocation( nX, nY ); }
}
</source>
Simple splash screen
<source lang="java">
import javax.swing.JLabel; import javax.swing.JWindow; import javax.swing.SwingConstants; public class SimpleSplashScreen {
public static void main(String[] arg) { JWindow jwin = new JWindow(); jwin.getContentPane() .add( new JLabel("Loading ZIP/JAR Manager...", SwingConstants.CENTER)); jwin.setBounds(200, 200, 200, 100); jwin.setVisible(true); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } jwin.setVisible(false); jwin.dispose(); }
}
</source>
Splash Screen based on JWindow
<source lang="java">
// // SplashScreen // // Copyright (C) by Andrea Carboni. // This file may be distributed under the terms of the LGPL license. //
import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JWindow; // public class SplashScreen extends JWindow {
//--------------------------------------------------------------------------- public SplashScreen(String image) { JLabel lbl = new JLabel(new ImageIcon(image)); getContentPane().add(lbl, BorderLayout.CENTER); pack(); setLocationRelativeTo(null); setVisible(true); }
} //
</source>