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

我正在尝试使用以下方法实现图像,以便在执行send操作时,GIF图像应在指定的时间内显示(由threadRunner pause方法实现)。

问题是它没有显示。在测试中,当我禁用stop()时,它与delIveryReport Textarea同时出现,而不应该出现。我该如何解决。

 private void sendActionPerformed(java.awt.event.ActionEvent evt) {

threadRunner t = new threadRunner();

String fone = "";

SendSMS sms = new SendSMS();

String[] arMSISDN = msisdn.split(",");

for (int i = 0; i < arMSISDN.length; i++) {

fone = arMSISDN[i];

fone = fone.trim();

try {

Cursor cursor = new Cursor(Cursor.WAIT_CURSOR);

setCursor(cursor);

t.pause(loading);

sms.sendSMS(user, pass, fone, senderIDString, msgString);

} catch (Exception e) {

e.printStackTrace();

} finally {

Cursor normal = new Cursor(Cursor.DEFAULT_CURSOR);

setCursor(normal);

t.stop(loading);

deliveryReport.append(fone + ": " + sms.response + "\n");

}

}

// JOptionPane.showMessageDialog(rootPane, deliveryReport);

deliveryReport.setVisible(true);

jScrollPane2.setVisible(true);

redo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

redo.setForeground(new java.awt.Color(223, 90, 46));

redo.setText("Would you like to send another Message?");

yes.setEnabled(true);

no.setEnabled(true);

yes.setText("Yes");

no.setText("No");

back.setEnabled(false);

send.setEnabled(false);

}

穿线器

public void pause(JLabel label){

try {

Thread.sleep(5000);

label.setVisible(true);

} catch (InterruptedException ex) {

Logger.getLogger(threadRunner.class.getName()).log(Level.SEVERE, null, ex);

}

}

public void stop(JLabel l){

l.setVisible(false);

}

回答:

在我看来,您的应用程序正在EDT上进行实际工作,而您的线程负责显示和隐藏进度标签。我可能是错的,但是如果是这样,那么我建议您做的事情与您做的完全相反。仅应从EDT(事件调度线程)完成SWING组件的更新,而不能从其他线程进行更新。

如果这是一个SWING桌面应用程序,那么我的建议是让您看一下SwingWorker,这是一个专门设计用于处理长时间运行的任务而又不会阻塞EDT的类。然后,您可以执行以下概述的操作(我的代码可能无法100%编译,但是它应该使您了解我的意思。

private void sendActionPerformed(java.awt.event.ActionEvent evt) {  

//implement code 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 code to notify user about errors here.

} finally {

//implement code to hide progress label here.

}

}

关于您的问题:只需将动画gif设置为JLabel的图标-

SWING应该注意显示它。只要您的SMS发送代码在另一个线程上运行,SWING应该很高兴能够渲染GIF动画而不会被SMS发送代码阻止。

以上是 如何使用线程显示动画GIF图像 的全部内容, 来源链接: utcz.com/qa/397736.html

回到顶部