Button ActionEvent发送电子邮件并同时显示加载屏幕
问题描述:
目前我正在用java编写一个小工具。我遇到的问题如下: 当我按下按钮时,应该有两件事同时开始。Button ActionEvent发送电子邮件并同时显示加载屏幕
- 启动IconImage(GIF)加载图像
- 发送与附件的电子邮件(使用该code)
我现在有下面的代码,其完美地发送电子邮件,但是没有图像可见。我也尝试开始一个新的线程,但从SendEmail类获得了很多例外。有什么建议么?
btnWeiter4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
URL loader = KeyLogger.class.getResource("/ajax-loader.gif");
JLabel loading = new JLabel("loading ...\r\n");
loading.setIcon(new ImageIcon(loader));
loading.setVisible(true); // show loading image
loading.setBounds(1087, 599, 121, 45);
panel_4.add(loading);
SendEmail email = new SendEmail();
email.send(); // sends email
}
});
更新:
似乎有一个顺序。首先发送电子邮件,之后我的加载图像即使在我开始时显示为setVisible(true)
。
解决方案:
我用匿名内部类来创建新的线程来发送电子邮件,我感动file.delete();
内SendEmail
类。对我来说很好工作
new Thread() {
public void run() {
SendEmail email = new SendEmail();
email.send(); // sends email
}
}.start();
答
我认为问题在于setIcon方法需要一个字符串而不是URL,请尝试像这样。
更新现在,你可以在执行
btnWeiter4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JLabel loading = new JLabel("loading ...\r\n");
loading.setBounds(1087, 599, 121, 45);
loading.setIcon(new ImageIcon("ajax-loader.gif")); // Check whether the gif file is really in the root folder
loading.setVisible(true); // show loading image
String currentTime = String.valueOf(System.currentTimeMillis());
System.out.println("Im done with JPanel " + currentTime);
panel_4.add(loading);
SendEmail email = new SendEmail();
email.send(); // sends email
String currentTime2 = String.valueOf(System.currentTimeMillis());
System.out.println("Im done with Email sending " + currentTime2);
}
});
代码到底是哪一部分瞧瞧updatet岗位。 – WirJun
我不认为在显示Jlabel之前发送的电子邮件,ActionEvent代码将从顶部执行到buttom,您如何测量电子邮件发送的时间? –
由于该按钮被冻结一段时间1-2秒,之后图像可见 – WirJun