我在框架内有一个JPanel.JPanel的内容应该在每次调用时更新paintComponent(调用方式repaint()),但是当我按照下面的方式调用时,我只看到一个白色的窗口.(请原谅那些破损的缩进,Eclipse会使用标签做各种奇怪的事情.)
private static void handleGUI()
{
JFrame frame = new JFrame("Animation");
frame.setPreferredSize(new Dimension(100, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Board b = new Board();
frame.getContentPane().add(b);
frame.pack();
frame.setVisible(true);
while(true)
{
System.out.println("Repainting panel");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
b.repaint();
}
}
public class Board extends JPanel
{
public Board() { t=0; }
private int t;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
++t;
/* Variables snipped */
g.setColor(Color.white);
g.drawOval(0, 0, width, height);
BufferedImage image = ImageIO.read(new File(imagePath));
g.drawImage(image, x(t), …Run Code Online (Sandbox Code Playgroud)