需要帮助在GridLayout中移动JLabel



我正试图将一个由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;
}
}

此块

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();

字面意思是"每秒移动我的机器人5次",因此机器人每秒眨眼移动5次。

你需要的是每秒钟移动一次机器人。要做到这一点,你需要在机器人移动之间引入延迟。

您&;现在的评论员对此不以为然。移动机器人不需要添加或删除组件或更改Z顺序,只需将移动的位置的标签文本从"更改为`"🤖;"即可。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class MovingBot {
private JComponent ui = null;
String bot = new String(Character.toChars(129302));
Font font;
JLabel[] labels = new JLabel[100];
MovingBot() {
initUI();
}
public void initUI() {
if (ui!=null) return;
Font[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
if (font.canDisplay(129302)) {
this.font = font.deriveFont(20f);
}
}
ui = new JPanel(new GridLayout(0,20,2,2));
ui.setBorder(new EmptyBorder(4,4,4,4));
for (int ii=0; ii<labels.length; ii++) {
JLabel l = new JLabel(" ");
l.setFont(font);
ui.add(l);
labels[ii] = l;
}
labels[0].setText(bot);
ActionListener moveListener = new ActionListener() {
int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
int indexLast = count%100;
labels[indexLast].setText(" ");
count++;
int indexCurrent = count%100;
labels[indexCurrent].setText(bot);
}
};
Timer timer = new Timer(50, moveListener);
timer.start();
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
MovingBot o = new MovingBot();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

最新更新