我試圖用java編寫程序,其中包含圖像的標籤每秒都在移動,但不幸的是,儘管代碼包含零錯誤,但代碼並未運行。任何人都可以弄清楚發生了什麼。這裏是代碼:在Java中移動JLabel
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class what extends JFrame {
private JPanel panel;
private Random r= new Random();
private JLabel image;
private Random s = new Random();
public what(){
super("Catch him!");
panel = new JPanel();
Icon b = new ImageIcon(getClass().getResource("img.JPG"));
image = new JLabel(b);
panel.add(image);
add(panel,BorderLayout.CENTER);
panel.setBackground(Color.yellow);
panel.add(image);
while(true){
int x = s.nextInt(1000);
int y = s.nextInt(900);
try{
Thread.sleep(1000);
}catch(Exception e){
}
image.setBounds(x, y,200, 200);
}
}
public static void main(String[] args){
what c = new what();
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(1920,1080);
}
}
請問,任何人都可以幫助我。
你得到的輸出是什麼? –
沒有什麼程序不會執行 –