栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用线程显示动画GIF图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用线程显示动画GIF图像

在我看来,您的应用程序正在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发送代码阻止。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号