创建SWT工程
在Eclipse中安装WindowBuilder插件,可快速搭建SWT工程。通过WindowBuilder创建一个HelloWorldSWT工程,界面如下:

HelloWorld的代码如下:
protected void createContents() {
shlSwthelloWorld = new Shell();
shlSwthelloWorld.setSize(342, 342);
shlSwthelloWorld.setText("SWT-Hello World");
text = new Text(shlSwthelloWorld, SWT.BORDER);
text.setBounds(10, 73, 322, 237);
Button btnHellosend = new Button(shlSwthelloWorld, SWT.NONE);
btnHellosend.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText("SWT, Hello World!");
}
});
btnHellosend.setBounds(10, 32, 94, 27);
btnHellosend.setText("Hello_Send");
btnClearcontent = new Button(shlSwthelloWorld, SWT.NONE);
btnClearcontent.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText("");
}
});
btnClearcontent.setBounds(238, 32, 94, 27);
btnClearcontent.setText("Clear");
}
导出jar包
选中工程点击【Export】–【Runnable JAR File】–【Package required libraries into generated JAR】–【finish】
发布EXE应用
1、安装exe4j,适配windows、macOS、linux,可自行选择,其中windows是安装版,macOS/linux是免安装版,直接在安装软件中拖拽源安装文件包到本地即可。
2、使用exe4j,需注意以下几个点:
- Choose Project Type,如下图所示:

- Executable info – 32-bit or 64-bit,勾选【Generate 64-bit executable】,如下图

- 在JRE处选择导入的Jar包,并在 Main class from 选择 org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

- 在Splash screen – 【Configure search sequence】将jre的文件夹copy一份到导出exe的文件夹中,如下图

- 其余的大部分都可以默认,点击【finish】
这样就可以实现免安装且client端无需安装java-jre也可打开执行。
本文详细介绍了如何在Eclipse中使用WindowBuilder插件创建SWT工程,并通过HelloWorldSWT实例演示了界面设计与交互代码编写。此外,还提供了导出为jar包及使用exe4j工具将jar包封装成EXE应用的全过程,实现跨平台运行。
2415

被折叠的 条评论
为什么被折叠?



