This article details the startup window effect of Java when a program is running, such as commonly used Microsoft Word, Borland JBuilder, etc. Such windows are called information windows. The advantage of using the information window is that the user can know the running status of the software for a period of time before waiting for the main interface of the software to appear. This example will demonstrate how to implement an information window. When the program is opened, the information window is displayed first and counts down on the window until "waiting 0", then the window is closed and the main window of the program is displayed.
The main implementation methods of this function are as follows:
Generally speaking, most information windows do not have a title bar, so the information window cannot be implemented by inheriting the JFrame class. A simple way is to implement it by inheriting the JWindow class (of course, inheriting the Window class is also possible, but one principle is to try to using interface classes in swing). In addition, this example uses the MediaTracker class in the java.awt package. The advantage of using this class is that it can better manage the pictures to be used in the program, and it can also ensure that the pictures and the interface are displayed at the same time, avoiding the disadvantage of displaying pictures long after the window is displayed.
The specific steps are as follows:
1. Create a new Project, name it JSpleshWindowDemo, and keep other settings as default.
2. Create a new Application, name it JSpleshWindowDemo, name the main window MainFrame, and name the main window title JSpleshWindowDemo.
3. Let’s write the code for the information window first. Create a new class SpleshWindow.java, which inherits the java.swing.JWindow class. In the SpleshWindow class, define new properties with the following code:
private String statusStr=null; //Information to be displayed in the information window private Image logoImg=null; //Display image in the information window
4. Add code to the construction method, load the image and initialize the form. The implementation code is as follows:
public SpleshWindow(JFrame owner) { //Take the JFrame object as a parameter, which can be the interaction between the information window and the main window super( owner ); // Load the image logoImg=getToolkit().getImage( ClassLoader.getSystemResource("images/splesh.jpg ") );//Wait for the image to be loaded java.awt.MediaTracker tracker=new java.awt.MediaTracker( this ); //Create a MediaTracker object tracker.addImage( logoImg , 0 ); //Put the image into the MediaTracker object with the serial number 0 try{ //Wait until the image is loaded tracker.waitForAll();}catch ( InterruptedException e ) {e .printStackTrace();}//Set the display position of the information form on the screen setLocation( getToolkit().getScreenSize().width/2 - logoImg.getWidth(this)/2 , getToolkit().getScreenSize().height/2 -logoImg.getHeight(this)/2 );setSize( logoImg.getWidth(this ), logoImg.getHeight(this) ); // Set window size}5. Write a method to set the display information. The code is as follows:
public void setStatus( String status ){statusStr=status;paint( getGraphics() ); // Redraw the window to update the display information in the information window}6. Reset the paint() method to draw pictures and display information. The code is as follows:
public void paint(Graphics g) {/**@todo Override this java.awt.Component method*/super.paint(g);//Draw the picture if (logoImg!=null)g.drawImage( logoImg, 0, 0 , this );//Draw and display information if ( statusStr!=null ){g.setColor(Color.red);g.drawString( statusStr , 240, getSize().height-250 );}}7. Write the MainFrame class, implement the java.lang.Runnable interface, and define new attributes. The settings are as follows:
private SpleshWindow spleshWindow=null;
8. Add the code for running the information window to the initialization method of the MainFrame class. The implementation code is as follows:
private void jbInit() throws Exception {//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));contentPane = (JPanel) this.getContentPane();contentPane.setLayout (borderLayout1);this.setSize(new Dimension(400, 300));this.setTitle("JSpleshWindowDemo");//Create a new thread and run the information window Thread t = new Thread(this);t.start();//Wait for the information window to display try{t.join( );}catch ( InterruptedException e ){e.printStackTrace() ;}// Display message to the information window long x=System.currentTimeMillis();while ( System.currentTimeMillis()-x <35000 ){System.out.print( "Waiting "+(35000-System.currentTimeMillis()+x+" /r") );// you can set status string in splash windowspleshWindow.setStatus ( "Waiting "+(35-(long)(System.currentTimeMillis()/1000)+(long)(x/1000)) );}//Close the information window if ( spleshWindow!=null ) {spleshWindow.dispose();spleshWindow=null;}}9. Write the run() method of the MainFrame class as follows:
public void run() {//Create a new information window and display spleshWindow=new SpleshWindow( this );spleshWindow.show();// throw new java.lang.UnsupportedOperationException("Method run() not yet implemented.") ;}