1
我很努力地想知道如何在程序中使用定时器来改变每一秒或两秒。我尝试了一些组合,但是这些组合都没有成功。 我相信ActionListener中有些东西可能会失败。如何在JFrame中实现定时器的正确方法
ArrayList<Point> punkter = new ArrayList<Point>();
int i = 0;
int n = 0;
public Point[] point = null;
private Timer timer;
Random rg = new Random();
public timer(){
this.setTitle("Draw");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1010, 710);
this.setLayout(null);
this.setLocationRelativeTo(null);
point = new Point[100];
this.setVisible(true);
timer = new Timer(500,this);
timer.start();
}
public void paint(Graphics g){
super.paint(g);
for (int i = 0; i < punkter.size(); i++) {
Point a = punkter.get(i);
Point b = punkter.get((i+1)%punkter.size());
g.fillOval(a.x, a.y, 5, 5);
g.drawLine(a.x, a.y, b.x, b.y);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int i = 0;i < 100;i++){
point[i] = new Point(rg.nextInt(1000), rg.nextInt(700));
punkter.add(point[i]);
}
}
}
使用嵌套容器;另外,请注意:“Swing程序应该重写'paintComponent()',而不是重写'paint()'。” - [* AWT和Swing中的绘制:绘制方法*](http://www.oracle.com/technetwork /java/painting-140037.html#callbacks)。 – trashgod