Chapter 2 Section 2 greeting the jface world

Source: Internet
Author: User

Section 2 greeting the jface world

Returned directory

 

The helloworld program once again greeted the world, but this time it uses jface. Because jface has added some java libraries (but there is no local library-remember that it is built on SWT), you need a new Ant file (see listing 13-1) generate and run this program.

Listing 13-1: Build. xml

<Project name = "genericjfaceapplication" default = "run" basedir = ".">
<Description>
Generic jface application build and execution File
</Description>

<Property name = "Main. Class" value = ""/>
<Property name = "src" location = "."/>
<Property name = "build" location = "."/>

<! -Update the Location Value Based on your eclipse main directory -->
<Property name = "ECL. Home" location = "C: \ Eclipse"/>

<! -Update the value of Value Based on your window system (Win32, GTK, motif, etc.) -->
<Property name = "win. sys" value = "Win32"/>

<! -Update the value of Value Based on your operating system (Win32, Linux, etc.) -->
<Property name = "OS. sys" value = "Win32"/>

<! -Update the value of Value Based on the architecture of your machine -->
<Property name = "arch" value = "x86"/>

<! -Update the value of Value Based on your SWT version -->
<Property name = "SWT. Ver" value = "3.0.0"/>

<! -Do not modify the following code -->
<Property name = "SWT. subdir"
Location = "$ {ECL. Home}/plugins/org. Eclipse. SWT. $ {win. sys }_$ {SWT. Ver}"/>
<Property name = "SWT. Jar. lib" location = "$ {SWT. subdir}/WS/$ {win. sys}"/>
<Property name = "SWT. JNI. lib"
Location = "$ {SWT. subdir}/OS/$ {OS. sys}/$ {arch}"/>
<Property name = "runtime. Jar. lib"
Location = "$ {ECL. Home}/plugins/org. Eclipse. Core. runtime _ $ {SWT. Ver}"/>
<Property name = "jface. Jar. lib"
Location = "$ {ECL. Home}/plugins/org. Eclipse. jface _ $ {SWT. Ver}"/>
<Property name = "osgi. Jar. lib"
Location = "$ {ECL. Home}/plugins/org. Eclipse. osgi _ $ {SWT. Ver}"/>
<Property name = "jfacetext. Jar. lib"
Location = "$ {ECL. Home}/plugins/org. Eclipse. jface. Text _ $ {SWT. Ver}"/>
<Property name = "text. Jar. lib"
Location = "$ {ECL. Home}/plugins/org. Eclipse. Text _ $ {SWT. Ver}"/>

<Path id = "project. Class. Path">
<Pathelement Path = "$ {build}"/>
<Fileset dir = "$ {SWT. Jar. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
<Fileset dir = "$ {runtime. Jar. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
<Fileset dir = "$ {jface. Jar. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
<Fileset dir = "$ {osgi. Jar. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
<Fileset dir = "$ {jfacetext. Jar. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
<Fileset dir = "$ {text. Jar. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
</Path>

<Target name = "compile">
<Javac srcdir = "$ {SRC}" destdir = "$ {build}">
<Classpath refID = "project. Class. Path"/>
</Javac>
</Target>

<Target name = "run" depends = "compile">
<Java classname = "$ {main. Class}" fork = "true" failonerror = "true">
<Jvmarg value = "-djava. Library. Path =$ {SWT. JNI. Lib}"/>
<Classpath refID = "project. Class. Path"/>
</Java>
</Target>
</Project>

As you can see, this build. xml file adds some jface jar files to classpath. Make sure to update your file according to the notes in this file, such as your operating system and window system. If you are using eclipse, you can add these jar files to the Java build path section on the project properties page.

Listing 13-2 contains the source code of helloworld.

Listing 13-2: helloworld. Java

Package examples. ch13; import Org. eclipse. jface. window. applicationwindow; import Org. eclipse. SWT. SWT; import Org. eclipse. SWT. widgets. *;/*** your first jface application */public class helloworld extends applicationwindow {/*** helloworld constructor */Public helloworld () {super (null);}/*** running program */Public void run () {// Let open () method: Do not return setblockonopen (true) before the window is closed; // open the main window open (); // destroy display. getcurrent (). dispose ();}/*** create the content of the main window ** @ Param parent main window * @ return control */protected control createcontents (composite parent) {// create a hello, world label Label = new label (parent, SWT. center); label. settext ("Hello, world"); return label ;} /*** entry point of the program ** @ Param ARGs command line parameter */public static void main (string [] ARGs) {New helloworld (). run ();}}

Compile and run helloworld as you did for the SWT program:

Ant-dmain. Class = examples. ch13.helloworld

Run this command at the prompt to see the window shown in 13-1.

Figure 13-1: helloworld of jface

Note that the helloworld class inherits from an applicationwindow class, which is the shell abstraction of jface. When you enter the word extends in the Code Editor, SWT will make you feel guilty, because so many SWT classes (including shell) there is a warning that they are not designed to be inherited. Don't worry, inheriting an applicationwindow class is not only legal, but also advocates usage.

Then, you cannot find a typical SWT event loop:

Display display = New Display (); shell = new shell (); // create Shell Content shell. open (); While (! Shell. isdisposed () {If (! Display. readanddispatch () {display. Sleep () ;}} display. Dispose ();

Instead, a piece of code is simpler:

setBlockOnOpen(true);open();Display.getCurrent().dispose();

The first method call, setblockonopen (), sets such a flag: when it is true, tell the next method, open (), enter an event loop that is very similar to the original SWT event loop. Pass true to setblockonopen (), so that open () is not returned before the window is closed. However, when we notice that the third method calls display. getcurrent (). in dispose (), jface elegance brings a little less pleasure: You still need to destroy what you created. This is a small price to use the simplicity of jface.

This helloworld program does not explicitly specify the layout, because an internal class called applicationwindowlayout is used by default to meet all your needs. It does not explicitly call the createcontents () method, but relies on the jface framework. The code here is more neat and more detailed than the SWT examples in this book.

 

Returned directory

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.