Step 1 Eclipse 里建立如图的文件结构:
Step2 HelloSWT.java的文件内容:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloSWT {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setSize(150, 150);
canvas.setLocation(20, 20);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc =e.gc;
gc.drawRectangle(3, 5, 20, 25);
gc.drawRectangle(3, 5, 20, 25);
gc.fillRectangle(30, 5, 20, 25);
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Color red = display.getSystemColor(SWT.COLOR_RED);
gc.setForeground(blue);
gc.drawLine(80, 20, 100, 80);
gc.drawOval(40, 40, 10, 10);
gc.drawPolygon(new int[] {100, 100, 120, 120, 140, 100});
gc.setBackground(red);
gc.fillRectangle(20, 100, 20, 20);
gc.fillRectangle(50, 100, 20, 20);
gc.drawRectangle(50, 100, 20, 20);
gc.drawString("Text", 120, 20);
gc.setClipping(40, 60, 40, 40);
gc.fillOval(30, 50, 30, 25);
}
});
shell.open();
shell.setSize(300, 300);
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
display.dispose();
}
}
Step 3 build.xml文件内容:
<?xml version="1.0"?>
<project default="dist" name="hello world">
<description>A Simple Java Programe</description>
<property name="srcDir" location="src"/>
<property name="libDir" location="lib"/>
<property name="buildDir" location="build"/>
<property name="packDir" location="pack"/>
<property name="distDir" location="dist"/>
<property name="distLibDir" location="${distDir}/lib" />
<target name="init">
<tstamp/>
<mkdir dir="${buildDir}"/>
<mkdir dir="${packDir}"/>
<mkdir dir="${distDir}"/>
<mkdir dir="${distLibDir}"/>
</target>
<target name="copyfiles" depends="init">
<copy file="${libDir}/swt.jar" todir="${distLibDir}"/>
<copy file="${libDir}/swt-awt-win32-3062.dll" todir="${distDir}"/>
<copy file="${libDir}/swt-awt-win32-3063.dll" todir="${distDir}"/>
<copy file="${libDir}/swt-win32-3062.dll" todir="${distDir}"/>
<copy file="${libDir}/swt-win32-3063.dll" todir="${distDir}"/>
</target>
<target name="compile" depends="copyfiles">
<javac srcdir="${srcDir}" destdir="${buildDir}" classpath="${libDir}/swt.jar" />
</target>
<target name="pack" depends="compile">
<jar destfile="${packDir}/package-${DSTAMP}.jar" basedir="${buildDir}">
<manifest>
<attribute name="Build-By" value="${user.name}"/>
<attribute name="Class-Path" value="lib/swt.jar"/>
<attribute name="Main-Class" value="HelloSWT"/>
</manifest>
</jar>
<jar destfile="${packDir}/package-src-${DSTAMP}.jar" basedir="${srcDir}"/>
</target>
<target name="dist" depends="pack">
<taskdef resource="proguard/ant/task.properties" classpath="lib/proguard.jar" />
<proguard>
-injars ${packDir}/package-${DSTAMP}.jar
-outjars ${distDir}/package-${DSTAMP}.jar
-libraryjars ${libDir}/rt.jar
-libraryjars ${libDir}/swt.jar
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
<!-- Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
native <methods>;
}-->
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
</proguard>
</target>
<target name="clean">
<delete dir="${buildDir}"/>
<delete dir="${packDir}"/>
<delete dir="${distDir}"/>
</target>
</project>
Step 4 其余文件是SWT的库和运行时的dll文件,以及jre/lib/rt.jar和proguard3.2的库文件
Step 5 右击build.xml文件,选择运行"ant build...".选中"target"里的"dist".
Step 6 运行后的文件结构如下图:
Step 7 到dist目录双击package-20050613.jar文件,就可运行程序。
Step 8 运行build.xml的clean target可以恢复到初始文件结构。