java计时器,在Java中实现计时器

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JLabel;

import java.awt.*;

import javax.swing.*;

import java.util.Timer;

import java.awt.event.*;

import java.awt.event.*;

import javax.swing.*;

class autos extends JLabel

{

@SuppressWarnings("serial")

int z=100,i=50;

public static void main(String[] args)

{

JFrame frame=new JFrame("Rectangle");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setSize(1000,1000);

frame.add(new autos());

}

@Override

public void paintComponent( Graphics g )

{

for(i=1;i<=7;i++)

{

g.drawRect(z,100,100,100);

z=z+120;

//timer delay

}

}

}

Hello,I'm trying to create a program in java that draws mre rectangles one after another with a delay(not all of them at once).

Since sleep and TimeUnit will freeze the paintComponent, I'm a bit clueless.I tried to use a timer to make a delay, but I failed.I cannot understand how to use the timer in this case.

How do I make a time delay between the rectangles?

解决方案

You have to think of a Swing Timer as pseudo loop, which, every time it ticks, you update some value which, when your call repaint and your paintComponent method is called, you can update the UI appropriately

uZnQP.gif

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {

new Test();

}

public Test() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

private int count = 0;

public TestPane() {

Timer timer = new Timer(500, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

count++;

if (count == 7) {

((Timer)e.getSource()).stop();

}

repaint();

}

});

addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

timer.stop();

count = 0;

timer.start();

}

});

}

@Override

public Dimension getPreferredSize() {

return new Dimension((120 * 7) + 100, 300);

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g.create();

super.paintComponent(g);

int x = 100;

for (int rect = 1; rect <= count; rect++) {

g2d.drawRect(x, 100, 100, 100);

x += 120;

//timer delay

}

g2d.dispose();

}

}

}

ps- Click the panel to get the timer to start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值