[英]Update a picture contained in a JLabel after a half a second
我想創建一個狼奔跑的簡單動畫。 為此,我有9 png的狼的奔跑動作。 當按下start_button時,它將顯示屏幕的所有擺動元素。 然后,當按下wolf_run按鈕時,我希望JLabel wolf將其圖標更改為“ wolf2.png”,“ wolf3.png”,wolf4.png“,直至” wolf9.png“,然后再返回至” wolf1.png“ 。它們之間的間隔為0.5秒。wait()函數和其他所有功能都可以正常工作,只是更改JLabel中的圖像。我嘗試了很多方法,但似乎無濟於事。如果我在沒有wait()的情況下手動進行操作)函數,然后它可以工作,但是我需要JLabel在程序運行時同時進行更改。請在執行過程中幫助我更改圖像。謝謝
PS:我已經使用JFrame表單來設計和實現程序中的GUI
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class page2 extends javax.swing.JFrame {
static int i =2;
public page2() {
initComponents();
text_field.setVisible(false);
wolf.setVisible(false);
wolf_run.setVisible(false);
jPanel.setVisible(false);
jPanel.setOpaque(false);
}
public void wait(int a){
long b = (long)a;
Calendar za = Calendar.getInstance();
long t1 = za.getTimeInMillis();
while (true) {
Calendar zb = Calendar.getInstance();
long t2 = zb.getTimeInMillis();
if((t2==(t1+b))||(t2==(t1+b-1))||(t2==(t1+a+1))){
break;
}
}
}
private void initComponents() {
start_button = new javax.swing.JButton();
text_field = new javax.swing.JTextField();
wolf_run = new javax.swing.JButton();
jPanel = new javax.swing.JPanel();
wolf = new javax.swing.JLabel();
background2 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(849, 499));
setSize(new java.awt.Dimension(850, 500));
getContentPane().setLayout(null);
start_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button_start.png"))); // NOI18N
start_button.setToolTipText("click this button to start");
start_button.setBorder(null);
start_button.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
start_button.setName("");
start_button.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/button_start_go.png"))); // NOI18N
start_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
start_buttonActionPerformed(evt);
}
});
getContentPane().add(start_button);
start_button.setBounds(290, 80, 310, 310);
text_field.setFont(new java.awt.Font("Lucida Grande", 0, 22)); // NOI18N
text_field.setHorizontalAlignment(javax.swing.JTextField.CENTER);
text_field.setToolTipText("Type here");
text_field.setFocusCycleRoot(true);
getContentPane().add(text_field);
text_field.setBounds(370, 390, 450, 60);
wolf_run.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
wolf_run.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
wolf_runActionPerformed(evt);
}
});
getContentPane().add(wolf_run);
wolf_run.setBounds(630, 120, 190, 140);
wolf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wolf1.png"))); // NOI18N
jPanel.add(wolf);
getContentPane().add(jPanel);
jPanel.setBounds(20, 320, 330, 150);
background2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/backgroung2.png"))); // NOI18N
getContentPane().add(background2);
background2.setBounds(0, 0, 850, 500);
getContentPane().add(jPanel1);
jPanel1.setBounds(10, 320, 310, 160);
pack();
}// </editor-fold>
private void start_buttonActionPerformed(java.awt.event.ActionEvent evt) {
Calendar za = Calendar.getInstance();
int m1 = za.get(Calendar.MINUTE);
int s1 = za.get(Calendar.SECOND);
while (true) {
Calendar zb = Calendar.getInstance();
int m2 = zb.get(Calendar.MINUTE);
int s2 = zb.get(Calendar.SECOND);
if (m2 != m1) {
s2 = s2 + 60;
}
if (s2 == s1 + 1) {
start_button.setVisible(false);
start_button.setEnabled(false);
text_field.setVisible(true);
wolf.setVisible(true);
wolf_run.setVisible(true);
jPanel.setVisible(true);
break;
}
}
}
private void wolf_runActionPerformed(java.awt.event.ActionEvent evt) {
while(true){
wait(500);
wolf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wolf"+i+".png")));
System.out.println("processing "+i);
i=i+1;
if(i>9)
{ i=1;}
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(page2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(page2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(page2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(page2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new page2().setVisible(true);
}
});
}
private javax.swing.JLabel background2;
private javax.swing.JPanel jPanel;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton start_button;
private javax.swing.JTextField text_field;
private static javax.swing.JLabel wolf;
private javax.swing.JButton wolf_run;
}
使用javax.swingTimer 。 Swing事件線程中的Waitnig導致GUI凍結
使用javax.swing.Timer
。
有關更多信息和示例,請參見Java教程如何使用Swing計時器和課程:Swing中的並發 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.