Anonymous
JLabel не двигается
Сообщение
Anonymous » 11 окт 2024, 01:32
Я хотел немного переместить картинку. Это ImageIcon в JLabel. Мой план состоял в том, чтобы нажать JButton, выполняя цикл while, который перемещает ее каждую секунду на 50 пикселей вправо.
Код: Выделить всё
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();}
}
}
Это два класса
Код: Выделить всё
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){}
}
}
Теперь моя проблема в том, что изображение не двигается. Координаты изменяются, но не перемещаются.
Подробнее здесь:
https://stackoverflow.com/questions/408 ... oesnt-move
1728599573
Anonymous
Я хотел немного переместить картинку. Это ImageIcon в JLabel. Мой план состоял в том, чтобы нажать JButton, выполняя цикл while, который перемещает ее каждую секунду на 50 пикселей вправо. [code]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();} } } [/code] Это два класса [code]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){} } } [/code] Теперь моя проблема в том, что изображение не двигается. Координаты изменяются, но не перемещаются. Подробнее здесь: [url]https://stackoverflow.com/questions/40817355/jlabel-doesnt-move[/url]