Ich wollte ein kleines Bild bewegen. Es ist ein ImageIcon in einem JLabel. Mein Plan war es, einen JButton zu drücken, der eine while-Schleife ausführt, die ihn jede zweite 50px nach rechts bewegt.JLabel bewegt sich nicht
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JFrame implements ActionListener{
JButton jbUUp;
JButton jbUDw;
JButton jbUSh;
public static ImageIcon shot;
public static ImageIcon spL;
public static ImageIcon spR;
public static JLabel LspL;
public static JLabel LspR;
public static JLabel Lshot;
public static int shPosUser = 150;
public static int shPosKI = 150;
public static int shXPosUser = 180;
Game(){setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 500);
getContentPane().setBackground(Color.WHITE);
spR = new ImageIcon("src/spR.jpg");
LspR = new JLabel(spR);
LspR.setSize(170, 170);
LspR.setLocation(820, shPosKI);
LspR.setVisible(true);
spL = new ImageIcon("src/spL.jpg");
LspL = new JLabel(spL);
LspL.setSize(170, 170);
LspL.setLocation(10, shPosUser);
LspL.setVisible(true);
shot = new ImageIcon("src/shot.gif");
Lshot = new JLabel(shot);
Lshot.setSize(21, 15);
Lshot.setLocation(shXPosUser, shPosUser + 77);
Lshot.setVisible(false);
jbUUp = new JButton("U");
jbUUp.setSize(60, 30);
jbUUp.setLocation(10, 350);
jbUUp.addActionListener(this);
jbUDw = new JButton("D");
jbUDw.setSize(60, 30);
jbUDw.setLocation(10, 420);
jbUDw.addActionListener(this);
jbUSh = new JButton("S");
jbUSh.setSize(60, 30);
jbUSh.setLocation(10, 385);
jbUSh.addActionListener(this);
add(LspR);
add(LspL);
add(Lshot);
add(jbUUp);
add(jbUDw);
add(jbUSh);
}
public void actionPerformed(ActionEvent e){if(e.getSource() == jbUUp){User.moveUp();}
if(e.getSource() == jbUDw){User.moveDown();}
if(e.getSource() == jbUSh){User.shot();}
}
}
Dies sind die beiden Klassen
public class User {
User(){}
public static void moveUp(){Game.shPosUser = Game.shPosUser - 10;
Game.LspL.setLocation(10, Game.shPosUser);}
public static void moveDown(){Game.shPosUser = Game.shPosUser + 10;
Game.LspL.setLocation(10, Game.shPosUser);}
public static void shot(){Game.Lshot.setVisible(true);
while(Game.shXPosUser < 500){timeout(1000);
Game.shXPosUser = Game.shXPosUser + 50;
System.out.println(Game.shXPosUser);
Game.Lshot.setLocation(Game.shXPosUser, Game.shPosUser);
}
Game.Lshot.setVisible(false);
Game.shXPosUser = 180;
Game.Lshot.setLocation(Game.shXPosUser, Game.shPosUser);
}
public static void timeout(int time){try{Thread.sleep(time);}
catch(Exception e){}
}
}
Nun mein Problem ist, dass das Bild nicht bewegt. Die Koordinaten werden geändert, aber nicht verschoben.
* "Ich wollte ein kleines Bild bewegen. Es ist ein ImageIcon in einem JLabel. Mein Plan war es, einen JButton zu drücken, der eine While-Schleife ausführt." * Ich schlage einen neuen Plan vor. Passen Sie das Bild in einem 'JPanel' an, das' paintComponent (Graphics) 'überschreibt. Das Verschieben einer Komponente ist keine empfohlene Methode zur Animation. Besonders wenn es darum geht, mehr als ein visuelles Element auf dem Bildschirm zu animieren. Der obige Code lädt eine Vielzahl von Bildern. Wenn sie sich in Komponenten in einem Null-Layout befinden, ist das Verhalten nicht definiert, wenn sie sich überlappen. (Ist ein Grund, die Wiedergabe hier vollständig zu steuern.) –