为什么JPanel从不打电话给reapint

时间:2016-03-24 03:54:02

标签: java multithreading swing

您好我想写一个简单的arcanoid游戏,但由于某种原因,我的JPanel.repaint()从未被调用过,我的球也没动。所以在我的主类中将JPanel扩展到JPanel我添加了一个在用户想要启动游戏时调用的侦听器,结果一个线程被触发,这个线程改变了球的位置,并且应该(理想情况下)调用我的重绘jpanel类。我检查了球变化的位置,但从未调用重绘方法。有人可以帮忙吗?在此先感谢我的代码:

public class test extends JPanel{
int x=250;
int y=470;
int width=100;
int height=20;
Ball b=new Ball();
public static void main(String[] args){
    test t=new test();
    t.draw();
}
public void draw(){
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(520, 520);
    frame.setResizable(false);
    this.setSize(500,500);
    frame.add(this);
    this.addMouseListener(new mouseL());
    frame.setVisible(true);
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    System.out.println("Yeah I am called");

    Graphics2D g2d=(Graphics2D) g;
    g2d.setColor(Color.blue);
    g2d.fillRect(x, y, width, height);
    g2d.setColor(b.getColor());
    g2d.fillOval(b.x1, b.y1, b.width1, b.height1);
}
class Ball{
    Random rand=new Random();
    int x1=300;
    int y1=450;
    int height1=20;
    int width1=20;
    Color c;
    public Color getColor(){
        return new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
    }
}
class mouseL implements MouseListener{
    Thread t=new Thread(new MyRun());

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        t.run();


    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}
class MyRun implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try{
        while(true){
            //System.out.println("Yeah I work");
            test.this.b.x1=test.this.b.x1-1;
            test.this.b.y1=test.this.b.y1-1;
            System.out.println("the values are: "+test.this.b.x1+" and "+test.this.b.y1);
            Thread.sleep(2000);
            test.this.repaint();
        }
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}

}

1 个答案:

答案 0 :(得分:2)

t.run();调用(在您的情况下)Runnabe#run,它是在事件调度线程的上下文中执行的,从而永远冻结您的UI。

您应该使用t.start()

因为Swing是单线程的,所以存在竞争条件和变量之间脏读/写的风险。一个更安全,更简单的解决方案是使用Swing Timer,请参阅How to use Swing Timers,它会在EDT中安排它的通知,从而可以安全地更新用户界面

此外,请查看How can I set in the midst?,了解您不应该使用JFrame#setSize的原因,而应该覆盖getPreferredSize的{​​{1}}方法并致电而是JPanel