执行 JavaMail 时所有 JFrame 冻结 [英] All JFrame freeze while do JavaMail

查看:20
本文介绍了执行 JavaMail 时所有 JFrame 冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发汽车管理系统程序.然后我想在汽车进出时给这家公司的老板发邮件.我的代码可以成功发送邮件,但我注意到在发送邮件时,其他 JFrame 窗口被冻结(我无法在所有 JFrame 窗口上执行任何操作),直到邮件发送完成.这通常适用于 Javamail 还是有办法让其他 JFrame 仍然工作?

I develop program of car management system. Then I want to send mail to owner of this company when car coming in and car out. My code can send mail successfully but I notice that while mail send, other JFrame window is freeze (I can't do anything on all JFrame window) until mail send is complete. Is this normally for Javamail or there is a way to make other JFrame still working?

在我的程序中,完成发送一封邮件大约需要 10 秒.

In my program, it take about 10 seconds to complete send one mail.

推荐答案

当你做繁重的任务时,你应该在另一个线程中运行它们,而不是像 gui 一样.如果您在 Event Dispatch Thread 中运行,那么 gui 将冻结直到完成.

When you do heavy task you should run them in another threads rather in the same as gui. If you run in Event Dispatch Thread then the gui is gonna freeze until finish.

你可以使用 SwingWorker 这里是一个我非常喜欢的例子 Swing Worker 例子

You can use SwingWorker here is an example i really like Swing Worker Example

例子:

class Worker extends SwingWorker<String, Object> {

    @Override
    protected String doInBackground() throws Exception {
       //here you send the mail
       return "DONE";
    }

    @Override
    protected void done() {
        super.done();
        //this is executed in the EDT
        JOptionPane.showMessageDialog(null, "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);
    }
}

这篇关于执行 JavaMail 时所有 JFrame 冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆