|
|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
<?xml version="1.0" encoding="UTF-8"?>
<cheatsheet title="Create a Hello World SWT application">
<intro href="/org.eclipse.platform.doc.user/reference/ref-cheatsheets.htm">
<description>
This cheat sheet shows you how to create a "Hello World" application
that uses the Standard Widget Toolkit (SWT). The application will
simply display an empty window to the user.<br/>
<br/>
If you need help at any step, click the (?) to the right. Let's get
started!
</description>
</intro>
<item title="Open the Java perspective" dialog="true" skip="false" href="/org.eclipse.platform.doc.user/concepts/concepts-4.htm">
<description>
If you're not already in the Java perspective, in the main menu
select <b>Window > Open Perspective > Java
or click on the "Click to Perform" link below.
</description>
<command serialization="org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.jdt.ui.JavaPerspective)" required="false" translate="">
</command>
</item>
<item title="Download SWT standalone" dialog="true" skip="false" href="/org.eclipse.platform.doc.isv/guide/swt.htm">
<description>
Open your Web browser to http://eclipse.org/downloads/, navigate
to the <b>Eclipse Platform project section, select the latest
release build, and find the <b>SWT Binary and Source
download.<br/>
<br/>
Save the file to your disk; you do not need to extract the archive.
</description>
</item>
<item title="Import the SWT project" dialog="true" skip="false" href="/org.eclipse.platform.doc.user/tasks/tasks-importproject.htm">
<description>
Import the SWT project from the main menu via File > Import...,
and select <b>Existing Projects into Workspace. Specify the
<b>archive file you downloaded and click Finish.
<br/>
This will create the org.eclipse.swt project which we will need
to compile and run the application.
</description>
<command serialization="org.eclipse.ui.file.import(importWizardId=org.eclipse.ui.wizards.import.ExternalProject)" required="false" translate="">
</command>
</item>
<item title="Create a Java project" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/concepts/concepts-3.htm">
<description>
Now we need a project to store our own source code. In the main
toolbar, click on the <b>New Java Project button, or click on
the link below. Enter <b>HelloWorldSWT for the project name,
then click <b>Finish.
</description>
<command serialization="org.eclipse.ui.newWizard(newWizardId=org.eclipse.jdt.ui.wizards.JavaProjectWizard)" required="false" translate="">
</command>
</item>
<item title="Configure the Java project" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/reference/ref-123.htm">
<description>
Since our project requires SWT, we need to specify this in the
project properties. Right-click on the project and select
<b>Properties.
<br/>
In the <b>Java Build Path page open the Projects tab,
add the org.eclipse.swt project, then click <b>OK.
</description>
</item>
<item title="Create a class" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/gettingStarted/qs-9.htm">
<description>
The next step is to create a new class. In the main toolbar,
click on the <b>New Java Class button (or the link below).
If not already specified, select <b>HelloWorld/src as the source folder. Enter HelloWorldSWT for the class name and select the checkbox to
create the <b>main() method, then click Finish.
<br/>
The Java editor will automatically open showing your new class.
</description>
<command serialization="org.eclipse.ui.newWizard(newWizardId=org.eclipse.jdt.ui.wizards.NewClassCreationWizard)" required="false" translate="">
</command>
</item>
<item title="Write the Java code" dialog="true" skip="false" href="/org.eclipse.platform.doc.isv/guide/swt_widgets.htm">
<description>
In the Java editor, enter the following Java code in the
<b>main() method:
<br/>
Display display = new Display();<br/>
Shell shell = new Shell(display);<br/>
shell.setText("Hello world!");<br/>
shell.open();<br/>
while (!shell.isDisposed()) {<br/>
if (!display.readAndDispatch()) display.sleep();<br/>
}<br/>
display.dispose();<br/>
<br/>
You will get <b>compile errors. Right click in the Java editor
and select Source > Organize Imports, then <b>save your
changes.
</description>
</item>
<item title="Run your Java application" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/gettingStarted/qs-12.htm">
<description>
To <b>run your application, right-click on your class in the
Package Explorer and select <b>Run As > Java
Application</b>. A new empty window should appear with the
title "Hello world!".<br/>
<br/>
Congratulations! You have successfully created a Hello World SWT
application!
</description>
</item>
</cheatsheet>
|