我试图在java中编写程序,其中包含图像的标签每秒都在移动,但遗憾的是,尽管代码包含零错误,但代码仍未运行.任何人都可以知道发生了什么.这是代码:
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);
}
}
拜托,任何人都可以帮助我.