Java implementation method of starting Information window when software runs _java

Source: Internet
Author: User

The example of this article describes the Java implementation of a Program runtime startup window effect, such as the commonly used Microsoft Word, Borland JBuilder, such as windows, such as the Information window. The advantage of using an information window is that it allows the user to know the state of the software for a period of time before the main interface of the software is available. This example shows how to implement the Information window, when the program is opened, the Information window is displayed first, and the window is counted down until "waiting 0", which closes the window and displays the main window of the program.

The main implementation methods for this feature are as follows:

In general, most information windows do not have a title bar, so the information window cannot be implemented by inheriting the JFrame class, an easy way to do this is by inheriting the JWindow (of course, inheriting 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 you want to use in your program, while also ensuring that both the picture and the interface are displayed, avoiding the disadvantages of displaying the picture long after the window is displayed.

The specific steps are as follows:

1. Create a new project, named Jspleshwindowdemo, and other settings by default.

2. Create a new application, named Jspleshwindowdemo, the main window named mainframe, the main window title named Jspleshwindowdemo.

3. To write the code for the Information window first. Creates a new class Spleshwindow.java, inheriting the Java.swing.JWindow class. In the Spleshwindow class, define the new property, as follows:

Private String Statusstr=null; Information to display in the information window
private image logoimg=null//Information window display picture

4. Add code to the construction method, load the picture, and initialize the form with the following implementation code:

Public Spleshwindow (JFrame owner) {//is a JFrame object parameter, can be the information window and the main window interaction
Super (owner);
Load Picture
Logoimg=gettoolkit (). GetImage (Classloader.getsystemresource ("images/splesh.jpg"));
Wait for the picture to load complete
java.awt.MediaTracker tracker=new java.awt.MediaTracker (this);//Create a Mediatracker object
Tracker.addimage (logoimg, 0); Put the picture in the Mediatracker object, the ordinal number is 0
try{//wait until the picture loads complete
tracker.waitforall ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
Sets the location where the information form appears 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. To write a method for setting up display information, the code is as follows:

public void SetStatus (String status) {
statusstr=status;
Paint (Getgraphics ()); Repaint the window to update the display information in the Information window
}

6. Reset the Paint () method to draw the picture and display the information in the following code:

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 Display Information
if (statusstr!=null) {
g.setcolor (color.red);
g.DrawString (Statusstr, GetSize (). height-250);
}


7. Write the mainframe class, implement the Java.lang.Runnable interface, and define a new property, set as follows:

Private Spleshwindow Spleshwindow=null;

8. In the initialization method of the mainframe class, add the code that runs the information window to implement the following code:

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);
This.settitle ("Jspleshwindowdemo");
Create a new thread, run Information window
thread t = new threads (this);
T.start ();
The Wait Information window displays
try{
t.join ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
Displays a message to the information Form
long X=system.currenttimemillis ();
while (System.currenttimemillis ()-x <35000)
{
System.out.print ("Waiting" + (35000- System.currenttimemillis () +x+ "\ R"));
can set status string in Splash Window
spleshwindow.setstatus ("Waiting" + (35-(long) System.currenttimemillis ()/1000) + (Long) (x/1000));
}
Closes the information Form
if (spleshwindow!=null) {
spleshwindow.dispose ();
Spleshwindow=null
}
}

9. Write the Run () method for the mainframe class, as follows:

 public void run () {//Create a new information form and display the Spleshwindow=new Spleshwindow (this); spleshwindow.sh
ow ();
throw new Java.lang.UnsupportedOperationException ("Method run () is not yet implemented."); }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.