Home>

The example form of this article details the effect of Java's startup window 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 users can learn the software running status while waiting for the software main interface to appear.This example will demonstrate how to implement the information window,When opening the program,The information window is displayed first,And count down on the window,Until "waiting 0", close the window,The main window of the program is displayed.

The main implementation method of this function is as follows:

Generally speaking,Most info windows have no title bar.Therefore, the information window cannot be implemented by inheriting the jframe class.A simple way is to implement it by inheriting from jwindow (Of course, it is also possible to inherit the window class,But one principle is to use the interface in swing as much as possible

class). In addition, this example uses the mediatracker class in the java.awt package. The advantage of using this class is that you can better manage the pictures to be used in the program,At the same time, it can also ensure that the picture and interface are displayed at the same time.Avoid the disadvantages of displaying pictures long after the window is displayed.

The specific steps are as follows:

1. Create a new project, name it jspleshwindowdemo, and set other settings as default.

2. Create a new application named jspleshwindowdemo, the main window named mainframe, and the title of the main window named jspleshwindowdemo.

3. Let's write the code for the info window first.Create a new class spleshwindow.java and inherit the java.swing.jwindow class. In the spleshwindow class, define new properties,code show as below:

private string statusstr=null;// Information to be displayed in the info window
private image logoimg=null;// Display image in info window

4. Add code to the constructor,Load the picture and initialize the form,The implementation code is as follows:

public spleshwindow (jframe owner) {// Take jframe object as parameter,Can be information window and main window interaction
super (owner);
// load image
logoimg=gettoolkit (). getimage (classloader.getsystemresource ("images/splesh.jpg"));
// Wait for the image to load
java.awt.mediatracker tracker=new java.awt.mediatracker (this);// Create a mediatracker object
tracker.addimage (logoimg, 0);// Put the picture into the mediatracker object, the serial number is 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 the window size
}

5. Write a method to set the display information,code show as below:

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,code show as below:

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 properties,The settings are as follows:

private spleshwindow spleshwindow=null;

8. To the initialization method of the mainframe class,Add code to run the info window,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,Run info window
thread t=new thread (this);
t.start ();
// Wait for the info window to appear
try {
t.join ();
} catch (interruptedexception e) {
e.printstacktrace ();
}
// show message to info 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 window
spleshwindow.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 form and display it
spleshwindow=new spleshwindow (this);
spleshwindow.show ();
// throw new java.lang.unsupportedoperationexception ("method run () not yet implemented.");
}
  • Previous Example of using Out-String command to convert an object to string output in PowerShell
  • Next PowerShell uses regular expressions to find string instances