我想创建一个狼跑的简单动画。要做到这一点,我有9 png狼的跑步运动。按下start_button时,它将显示屏幕的所有swing元素。然后当按下wolf_run按钮时,我希望JLabel狼将其图标更改为“wolf2.png”,“wolf3.png”,wolf4.png“,upto”wolf9.png“然后返回”wolf1.png“这将在它们之间有0.5秒的间隔。除了更改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;
}
答案 0 :(得分:1)
使用javax.swingTimer。 Swing Event线程中的Waitnig导致GUI冻结
答案 1 :(得分:1)
使用javax.swing.Timer
。
有关更多信息和示例,请参阅Java教程How to Use Swing Timers和Lesson: Concurrency in Swing。