Java SE 6.0 with the development of high-quality desktop integration (rpm)

Download the source code for this article
Abstract: This article will show you concrete examples 6 series of excellent features in desktop development provided in the Java SE.
I. Introduction
With the Java SE 6 Beta release, Java developers no longer need to own to achieve Java native Interface (JNI) will be able to join in their applications in the desktop products with features. these features desktop integration has now become an integral part of the kernel.
the latest version of Java standard 6.0 (code-named Mustang) will a series of improvements in functionality - developers can easily handle security with Java, JMX, operating system files, and desktop development and other international issues .Sun a series of exhibits in the development of this version of the process in a very positive cooperative attitude. they listen carefully to the views by JSR 270 developers and in early February 2006 on the issue of all the Java SE source code and binary code. it seems, Sun is the effort to build a new desktop development tools.
relative in the previous version of Java, Mustang's desktop integration capabilities particularly popular with Java developers welcome. these new features put a lot of interesting features added to the Java Desktop application Of development. Previously, when developing such as Internet Explorer, system tray and e-mail clients such as Java, developers must create as Jtray and SysTray and other Java Native Interface (JNI) aspects of a big headache. Although this "patch" program to achieve the intended purpose, but most Java developers are very looking forward to these features become part of the core Java platform.
This paper will analyze the process of creating a sample application GoMustang detail. from this, I will show you how to use the Java SE 6 Splash display a screen, how to create system tray, as well as how to start the native desktop Internet browser from within Java when the application starts.
Note that in order to debug the sample program in this article, you will need to install:. Java Standard Edition 6 Beta and Apache Ant 1.6.5
Second, the development environment
(a) preparation Java SE 6 Beta
First you from Sun's Java site to download the Java SE 6 Beta have been identified as Sun Java SE 6 will be officially released in the fall of 2006, and it was suggested that JSR 270 Expert Group, some of which also feature will make appropriate changes; therefore, the current trial Mustang in your desktop development and it was time
estimate, final features described herein will not change much; so you can safely download the latest version of the JDK, JRE, etc.
first, make sure your desktop platforms supported my development on Windows XP. and test the sample application in this article, and there is no problem.
Secondly, make sure that the Windows system properties of your computer's environment variables point to Java SE 6.0 JDK I JAVA_HOME environment variable JAVA_HOME = C:. \ program Files . \ Java \ jdk1.6.0
Once the installation is complete, you should immediately check the version of Java JRE if all goes well, it should display a message similar to the following:.
C: \ the Java \ Mustang> the Java -version
the Java version "1 Beta-.6.0 "
the Java ((TM)) 2 the Runtime Environment, Standard (1.6.0 Build-Beta-b59g) Edition
HotSpot the Java (TM) Client VM (Build 1.6.0-Beta-b59g, the MODE Mixed, Sharing)
(b) prepare Ant
In this article, you will use Apache Ant to build the sample application. Therefore, if you do not have this tool , and you can install it from the Apache download site to your computer.
then, you can use the property to check Ant Ant Java version following example shows an incorrect version of Java:.
$ {} ant.java.version
this is because Ant built ant.java.version system is designed only property owned 1.1,1.2,1.3,1.4 and 1.5 estimate these values, Ant development team may soon update this property to contain 1.6; however, when you want to achieve their own to be especially careful this time.
to be prudent, you can use it to run Ant -debug If the following information is displayed, then Ant actually use a 1.6 version of the JRE:.
>>
Detected the Java version: in 1.5: C: \ Program Files \ the Java \ jdk1.6.0 \ jre
<<
(c) the installation of your development environment
Next, set up your development environment to this end, the easiest way is to download the corresponding source code and extract the paper. If you unzip the ZIP file to your C: \ path Then you will see the following files and directory structure:
c: \ Mustang \
| _build.xml
+ _src
Later, you will use the Ant build file build.xml to implement copy, compile, build, package and run the sample application GoMustang the file consists of the following key objectives Ant:.
[The init]
[Clean]
[the compile]
[dist]
[run]
here, [init] target is responsible for establishing the folder structure and copy the files to the build folder; [compile] achieve compile, and [dist] (discussed later in detail) by using a Manifest file to create a JAR [. run] target is responsible for starting your application, [clean] will help you first delete all files and directories by the Ant tasks created, and then from a totally clean environment to start the following tasks.
src directory contains three files: a GoMustang.java (source files) and two GIF files (gomustang-splash.gif used as Splash screen, an icon in the system tray gomustang.gif used).
now, you've laid the groundwork. now, we begin to create Splash screen your application.
three, Splash screen
for users hot, Splash screen has played a key role in the startup process of the application is desktop-based applications. Splash screen can make an eye-catching Looking forward to starting the application - by displaying advertising marketing, specific legal provisions, professional image, etc. It also provides for the application of a certain period of time required to load the library file
in Java application development achieve Splash screen has been a challenging program, because the program is loaded before the JVM starts and all the required JRE and application libraries can not take control. and wait for the opportunity to get a Java application thread of control when displaying a Splash the screen was too late.
Java SE 6 even allows an application can display a Splash screen before JVM launch this tool is built on Java application launcher. - It is the responsibility of displaying an image in a window in its unmodified allows the use of GIF, PNG, or JPEG images, may be transparent or translucent, and there may be animated further, Java SE 6 also has a release SplashScreen class - Once an application that allows the control thread, the program Splash screen can operate.
you can use one of the following two ways to start a Splash screen:
1. specify a specific SplashScreen-Image attribute in a JAR file Manifest in;
2. use a command-line parameter -splash
( a) use the JAR Manifest attribute
JAR files in the source code download for this article contains a gomustang-splash.gif file (the file displays a Splash screen) and compiled class files GoMustang.class when the application starts. this Manifest JAR file containing name of the class definition of main () method and SplashScreen-Image attribute (which points to gomustang-splash.gif file).
the following list shows the Ant build.xml JAR Task content - wherein the main-class specifies SplashScreen-Image and attributes:
...
<JAR JarFile = "$ {dist} /GoMustang.jar" the basedir = "Build $ {}">
<the manifest>
<attribute name = "Main-Class" value = "
<attribute name = "SplashScreen-Image" value = "GoMustang-splash.gif" />
</ the manifest>
</ JAR>
...
in order to see the true running Splash screen, you can enter ant at the command prompt run build.xml to execute the [run] target. because the program is small, so Java applications will be able to see it completed before the eyes open and close operations in person. to do this, you can use the 3-second pause coding tips.
Once you run the run target, Ant should console output shown in Figure 1 tracking information, and then pause for 3 seconds, the user will see here Splash screen gomustang-splash.gif picture.
Figure 1.Ant pause
for 3 seconds, Ant script to create the application program shown in Figure 2.
FIG 2.Ant create a system tray script completes
an icon will be created in the system tray shown in FIG. 3.
Fig 3.GoMustang icon
(b) using the command line - parameter splash
display a splash screen second method is to use the command-line parameters -splash splash screen file name, as shown in
C: \ mustang \ build> java -splash: gomustang-splash.gif GoMustang
noted that in order for the command normal operation, you need the c: \ mustang \ build directory (eds location of class files and GIF files are located splash screen using Java under) the command line. These files are created by the Ant build.xml file in the task.
Once the application is able to access a thread, you can access Splash screen using java.awt.SplashScreen class programmatically. This is a singleton class that provides special functions to change Splash screen image retrieval Splash screen size and boundaries, operations related to graphic objects, and finally close the Splash screen.
four, GoMustang.java file
before continuing, you should familiarize yourself GoMustang.java file. this file defines a single Java class GoMustang, and contains the following two key function:
. main
. createTrayIcon ()
you may have guessed, main is the main entry point - you can access java.awt.SplashScreen Java classes to operate Splash screen GoMustang application, you can make it pause for a few seconds. and then closed by calling splashScreen.close () Splash screen. However, you can turn it off before operating the Splash screen.
createTrayIcon () class of these two functions is more exciting. it is responsible for implementing all Interestingly work to install the application which in turn involves another topic we will discuss: the system tray icon
Fifth, the system tray Standard
System Tray located in the Windows task status area or in the Gnome notification area. It is in the area of a corner of the desktop operating system UI is a small, able to provide visualization applications currently running and direct access. GoMustang applications on the Windows platform provided herein can be implemented to generate a pop-up menu (see FIG. 4) when the right-click the icon in the system tray.
FIG. 4. GoMustang pop-up menu on the system tray
The GoMustang application SystemTray have a menu pop-up menu with two menu items:.. "Exit" and "Launch Browser" "Exit" responsible exit the application, and "Launch Browser" opens the default Internet browser and point
in the past, creating the flexibility to control the operating system's desktop system tray application requires Java support is not part of the core Java libraries. to this end, resulting in a JDIC (Java Desktop integration components), but Sun decided to put some of the components of a Java JDIC SE 6 together with a portion of release. java.awt.SystemTray and related classes java.awt.Desktop describe these critical components. wherein, Java.awt.SystemTray described operating system task status notification region or zone
displayed in the system tray the icon is .TrayIcon by the java.awt.TrayIcon description describes not only the image but also the actual object to receive events, and is responsible for the prompt appears, add the pop-up menu item, display messages, etc. you can displayMessage (String) and setToolTip (String) function to set the message and the message .TrayIcon on TrayIcon can put forward ActionEvents to Trayicon via the addActionLis tener (ActionListener) function registered ActionListener. java.awt.PopupMenu pop-up menu is described, and you can add by addPopupMenu (PopupMenu) function to them TrayIcon.
Let's analyze what createTrayIcon GoMustang Java class () function detailed notes as GoMustang code in Listing 1 (see accompanying source code) is indicated: First, you want to call SystemTray.isSupported () function to check whether SystemTray minimum operating system support functions;. If supported, this function should return true
once the program know that the operating system supports system tray functionality, it creates Exit and Launch Browser menu items, and these menu items added to the pop-up menu when the user. when right-clicking the system tray icon, this menu will pop up .ActionListeners function is added to the Exit and Launch the Browser menu items. when the user selects the menu item, actionPerformed (ActionEvent) will give the program a chance to perform some action. this for the exit menu item is exited by executing the application System.exit (0) command; and for the Launch Browser menu item is by pointing
then, the application creates a pop-up menu object and add the exit menu items and Launch Browser the pop-up menu. the next step is to create a tray icon with the image of a galloping horse. you can make .. getClass () getResourceAsStream (file name) function to access the source code download for this article gomustang.gif file and to load it into an image object and then, the tray icon object can be created together with the pop-up menu; Action and also listen to mouse is added to the tray icon - respectively for this application, only one message is added to the last tray icon on the system tray system tray is a singleton object, that object can SystemTray getSystemTray () on them retrieval. Creating tray icon in the system tray whole process is like that.
Now, let us more to look at in detail how "Launch Browser" menu item is to start the browser. You can analyze corresponding to "Launch Browser" menu item function actionPerformed (ActionEvent) "to see how to use the desktop object. Please refer to the following (listing part 2):
...
the MenuItem launchBrowserItem the MenuItem new new = ( "the Launch Browser");
the ActionListener launchListener the ActionListener new new = () {
public void the actionPerformed (the ActionEvent E) {
IF (Desktop.isDesktopSupported ()) {
ActionMessage = "Launched Browser";
the try {
trayIcon.displayMessage ( "! GoMustang",
"... The Launching Browser", TrayIcon.MessageType.INFO);
Desktop Desktop Desktop.getDesktop = ();
the desktop.browse (new new the URI ( "
trayIcon. setToolTip ( "GoMustang!");
}
catch(Exception exp){...}
}
}
};
LaunchBrowserItem.addActionListener (launchListener);
...
in Listing 2 (see accompanying source code) we are interested in the most critical objects of this class is java.awt.Desktop Desktop class, as we mentioned earlier. and from the Desktop JDIC class is responsible for locating and running the operating system-specific desktop applications default Internet browser application mapping is Mozilla's FireFox;.. Therefore, after displaying the message "Launching browser ...", desktop.browse (URI) function opens a point
VI Summary
for Java developers in control of the desktop, the real fun is just beginning. we sincerely look forward to adding more control of the desktop Java in future releases.

Reproduced in: https: //www.cnblogs.com/521taobao/archive/2012/03/17/2402524.html

Guess you like

Origin blog.csdn.net/weixin_34332905/article/details/93355927