java 关闭jdialog_如何用Java代码真正关闭一个JDialog?

我知道setVisible(false),dispose(),但他们不能真正关闭一个JDialog。当我有另一个线程停止时,JDialog的线程仍然运行。如何用Java代码真正关闭一个JDialog?

而且我不能使用System.exit(0),因为其他线程需要运行一段时间。

下面的代码,我最终通过System.exit(0)在程序结束时解决问题。

public class CsUpdateCtrl {

/**

* 升级service

*/

private CsUpdateService service;

private CsUpdateCtrl() {

this.service = (CsUpdateService) RmiUtil.getBean(RmiUtil.Service.csupdate);

}

private static final Logger log = Logger.getLogger(CsUpdateCtrl.class.getName());

public static void main(String[] args) throws IOException {

LogUtil.read();

log.info("进入自动更新系统");

CsUpdateCtrl ctrl = null;

try {

ctrl = new CsUpdateCtrl();

} catch (Exception e) {

SwingUtil.lookAndFeel();

SwingUtil.message("无法连接服务器!");

log.info("无法连接服务器:" + e.getMessage());

return;

}

ctrl.start(ctrl.service.version());

}

/**

* 根据版本号判断是否更新,更新完毕则启动程序

*/

private void start(int version) {

PropertyIO io = new PropertyIO();

String oldVersion = io.get(PropertyKey.version);

String serverAddress = io.get(PropertyKey.serverAddress);

if (oldVersion == null || !oldVersion.equals(String.valueOf(version))) {// 版本号不相等,下载更新程序

log.info("下载更新…");

DownLoad download1 = new DownLoad("http://" + serverAddress + "sdxg-csupdate/sdxg-csclient/sdxg-csclient.jar", "sdxg-csclient\\sdxg-csclient.jar");

Thread t1 = new Thread(download1);

DownLoad download2 = new DownLoad("http://" + serverAddress + "sdxg-csupdate/sdxg-csclient/lib/sdxg-web.jar", "sdxg-csclient\\lib\\sdxg-web.jar");

Thread t2 = new Thread(download2);

t1.start();

t2.start();

SwingUtil.lookAndFeel();

final DownloadView dialog = new DownloadView(new javax.swing.JFrame(), true);

java.awt.EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

dialog.setVisible(true);

}

});

// 结果判断

// 若未完成,则不进行下一步

while(!download1.result.finish() || !download2.result.finish()) {

// do nothing

}

if(download1.result == Result.Complete && download2.result == Result.Complete) {// 更新完成

// 版本号写入配置文件

io.write(PropertyKey.version, String.valueOf(version));

// Nothing is usefull here, include dispose,dispatchEvent and so on

// 启用应用程序

log.info("启用应用程序");

this.runApp();

System.exit(0);// well, this is usefull

} else {// 更新失败

// Nothing is usefull here, include dispose,dispatchEvent and so on

JOptionPane.showMessageDialog(null, "下载应用程序失败!");

log.info("下载应用程序失败!");

System.exit(0);// well, this is usefull

}

}else {// 版本号一样

log.info("启用应用程序");

this.runApp();

}

}

/**

* 运行客户端程序

*/

private void runApp() {

try {

Runtime.getRuntime().exec("java -jar " + "sdxg-csclient\\sdxg-csclient.jar");

} catch (IOException ex) {

JOptionPane.showMessageDialog(null, "启动应用程序失败!");

log.info(LogUtil.ex(ex, "启动应用程序失败!"));

}

}

/**

* 线程执行的结果

*/

enum Result {

Complete,

UnComplete,

Fail;

public boolean finish() {

return this != UnComplete;

}

}

/**

* 下载文件

* @param urlName

* @param fileName

* @throws MalformedURLException

* @throws IOException

*/

class DownLoad implements Runnable {

private String urlName;

private String fileName;

Result result = Result.UnComplete;

public DownLoad(String urlName, String fileName) {

this.urlName = urlName;

this.fileName = fileName;

}

@Override

public void run() {

try {

URL url = new URL(urlName);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();

BufferedInputStream bis = new BufferedInputStream(is);

FileOutputStream fos = new FileOutputStream(fileName);

int n = -1;

while ((n = bis.read()) != -1) {

fos.write(n);

}

fos.flush();

fos.close();

bis.close();

is.close();

result = Result.Complete;

} catch (Exception ex) {

result = Result.Fail;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值