爱玺玺

爱玺玺的生活日记本。wx:lb87626

javax.swing.Timer定时器的理解

//创建个Jpanel面板

class SnakeJpanel extends JPanel implements ActionListener{

Timer timer=new Timer(100, this);

public SnakeJpanel() {

System.out.println("构造方法");

timer.start();

}

@Override

public void paint(Graphics g) {

super.paint(g);

this.setBackground(Color.black);//设置this的背景颜色是白色

g.setColor(Color.ORANGE);

g.fill3DRect(0, 0, 10, 10, false);

System.out.println("paint");

}

@Override

public void actionPerformed(ActionEvent arg0) {

repaint();

}

}


Timer是接口ActionListener的方法,它是定时执行的接口的事件,因此想要定时执行的内容要写在事件里面。

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5