Java 需要在GridLayout中移动JLabel的帮助吗
我正在尝试将一个由Java 需要在GridLayout中移动JLabel的帮助吗,java,swing,jlabel,layout-manager,grid-layout,Java,Swing,Jlabel,Layout Manager,Grid Layout,我正在尝试将一个由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.Act
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;
}
}
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.LayoutManager;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.Serializable;
导入java.util.Vector;
导入javax.swing.ImageIcon;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.LayoutStyle;
导入javax.swing.Timer;
//机器人
公共类Robot扩展Case实现可序列化{
私人图像图标图像机器人;
私家色彩库;
公共机器人(){
imageRobot=新的ImageIcon(“./assets/balle.png”);
设置图标(图像机器人);
}
公共无效seDeplacer(JPanel小组){
Robot currentRoot=这个;
int delay=1000;//毫秒
ActionListener taskPerformer=新建ActionListener(){
已执行的公共无效操作(操作事件evt){
对于(inti=0;i此块
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 seDeplacer(JPanel面板){
Robot currentRoot=这个;
int delay=1000;//毫秒
ActionListener taskPerformer=新建ActionListener(){
已执行的公共无效操作(操作事件evt){
对于(int i=0;i你&当前的评论员已经不再(或耸耸肩)考虑这个问题。移动机器人不需要添加或删除组件或更改Z顺序,只需将移动位置的标签文本从”更改为`”