在我看来,您的应用程序正在EDT上进行实际工作,而您的线程负责显示和隐藏进度标签。我可能是错的,但是如果是这样,那么我建议您做的事情与您做的完全相反。仅应从EDT(事件调度线程)完成SWING组件的更新,而不能从其他线程进行更新。
如果这是一个SWING桌面应用程序,那么我的建议是让您看一下SwingWorker,这是一个专门设计用于处理长时间运行的任务而又不会阻塞EDT的类。然后,您可以执行以下概述的 *** 作(我的代码可能无法100%编译,但是它应该使您了解我的意思。
private void sendActionPerformed(java.awt.event.ActionEvent evt) { //implement pre to show progress label here SMSWorker w = new SMSWorker(user, pass, senderIdString, msgString, msisdn.split(",")); w.execute();}public SMSWorker extends SwingWorker<Void, DeliveryReport> { private final String user; private final String pass; private final String senderIdString; private final String msgString; private final String[] arMSISDN; // this constructor runs on the current (EDT) thread. public SMSWorker(String user, String pass, String senderIdString, String msgString, String[] arMSISDN) { this.user = user; this.pass = pass; this.senderIdString = senderIdString; this.msgString = msgString; this.arMSISDN = arMSISDN; } // this function runs in a separate thread. public Boolean doInBackground() { // Instantiate SMS gateway client. SendSMS sms = new SendSMS(); // Assuming a delivery report can be created like this. DeliveryReport deliveryReport = new DeliveryReport(); for (int i = 0; i < arMSISDN.length; i++) { fone = arMSISDN[i]; fone = fone.trim(); try { sms.sendSMS(user, pass, fone, senderIDString, msgString); } catch (Exception e) { // you can notify users about exception using the publish() method. } finally { deliveryReport.append(fone + ": " + sms.response + "n"); } } return deliveryReport; } // this function runs on the current (EDT) thread. public void done() { try { // synchronize worker thread with EDT. DeliveryReport deliveryReport = get(); } catch (Exception e) { //implement pre to notify user about errors here. } finally { //implement pre to hide progress label here. }}
关于您的问题:只需将动画gif设置为JLabel的图标-
SWING应该注意显示它。只要您的SMS发送代码在另一个线程上运行,SWING应该很高兴能够渲染GIF动画而不会被SMS发送代码阻止。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)