Java 如何重新运行paint方法以使JPanel动画化?

Java 如何重新运行paint方法以使JPanel动画化?,java,swing,jpanel,Java,Swing,Jpanel,我想我需要把一些代码放在注释所在的地方(或者使用非静态方法,但我不确定)。main方法创建窗口,然后启动graphics方法。我希望蓝色的方块闪烁 import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class paintTest extends JPanel{ private static JFrame theWindow

我想我需要把一些代码放在注释所在的地方(或者使用非静态方法,但我不确定)。main方法创建窗口,然后启动graphics方法。我希望蓝色的方块闪烁

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class paintTest extends JPanel{
private static JFrame theWindow = new JFrame("Window");
static boolean blueSqr = false;

public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(10, 10, 10, 10);

    if(blueSqr){
        g.setColor(Color.BLUE);
        g.fillRect(10, 10, 10, 10);
    }
}

public static void main(String[] args){
    createWindow();
    theWindow.getContentPane().add(new paintTest());
    while(true){
        blueSqr = false;

        System.out.println("off");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}

        blueSqr = true;
        // Needs something here
        System.out.println("on");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
}

public static void createWindow(){
    theWindow.setSize(500, 500);
    theWindow.setLocationRelativeTo(null);
    theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theWindow.setVisible(true);
}
}

任何帮助都会非常好。

使用Swing
计时器调用
repaint()
。另外,在
JPanel
中重写
paintComponent()
,而不是
paint()

大概是这样的:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintTest extends JPanel{

    boolean blueSqr = false;

    PaintTest() {
        setPreferredSize(new Dimension(100,25));
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                blueSqr = !blueSqr;
                repaint();
            }
        };
        Timer timer = new Timer(1000,al);
        timer.start();
    }

    public void paintComponent(Graphics g) {
        Color c = (blueSqr ? Color.BLUE : Color.RED);
        g.setColor(c);
        g.fillRect(10, 10, 10, 10);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame theWindow = new JFrame("Window");
                theWindow.getContentPane().add(new PaintTest());
                createWindow(theWindow);
            }
        });
    }

    public static void createWindow(JFrame theWindow){
        theWindow.pack();
        theWindow.setLocationByPlatform(true);
        theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theWindow.setVisible(true);
    }
}
还有其他一些改进我都懒得去记录(代码胜于雄辩)。如果您有任何问题(先检查文档,然后)请询问。

您的问题是

1) 通过在与Swing相关的代码中调用
Thread.sleep(int)
,千万不要这样做,因为在Swing中会延迟(有很多关于为什么不在编程语言中使用sleep的主题…)使用

2) 您的
JPanel
不会返回任何
XxxSize

3) 对于Swing,请使用
paintComponent()
,如果您有非常重要的原因,请使用方法
paint()

4) Swing GUI应该构建在

中,它被称为“方法”而不是“无效”。不客气。:)+1用于发布易于使用的代码。