我正在尝试将JLabel
代表的机器人移到GridLayout
中。进行了移动,但是JLabel
的显示仅在最后一个精加工方块中完成。我希望看到从盒子到盒子的移动。我尝试使用javax.swing.Timer
,但不起作用。
import java.awt.Color;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.actionEvent;
import java.awt.event.actionListener;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.Timer;
// Robot
public class Robot extends Case implements Serializable {
private ImageIcon imageRobot;
private Color couleur;
public Robot () {
imageRobot = new ImageIcon("./assets/balle.png");
setIcon(imageRobot);
}
public void seDeplacer (JPanel panel) {
Robot currentRoot = this;
int delay = 1000; //milliseconds
actionListener taskPerformer = new actionListener() {
public void actionPerformed(actionEvent evt) {
for (int i=0; i<5; i++) {
panel.remove(panel.getcomponent(i));
panel.add(currentRoot,i);
panel.doLayout();
}
}
};
new Timer(delay,taskPerformer).start();
}
public void detruire () {
}
public void setCouleur (Color couleur) {
this.couleur=couleur;
}
public Color getcouleur () {
return this.couleur;
}
}