6、为按钮添加以下事件处理:
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(reset);
button.setBounds(22, 33, 72, 22);
button.setText("\u91CD\u65B0\u542F\u52A8");
}
/**重启*/
private SelectionListener reset=new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
ProcessBuilder pb=new ProcessBuilder("java","-jar","newApp.jar");
try {
Process p=pb.start();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(),"提示", JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
};
其中,newApp.jar是应用发布成为可运行jar包的文件名。
7、进行完上面的步骤后,如果在调试模式下运行,会发现窗口消失了。还没完呢,还需要将整个应用发布后才能行。
先新建个文件夹,将本项目输出成为可运行jar文件,如图:
8、选定应用程序运行入口窗口以及jar存放地址:
9、jar文件生成了,但是在winXP下点击没反应。我们还需要建两个辅助文件:先加个javastart.bat文件:
@echo off
java -jar newApp.jar
10、bat文件执行时候会出现窗口,这是我不愿意看到的,我决定把它弄没了,所以加了newApp.vbe文件:
set ws=wscript.createobject("wscript.shell")
ws.run "javastart.bat /start",0
11、ok,点击newApp.vbe,测试可行性。