在Java中实现计时器 [英] Implementing a timer in java

查看:1255
本文介绍了在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
        }

    }
}

您好,我正在尝试用Java创建一个程序,该程序会延迟地逐个绘制mre矩形(并非一次绘制所有矩形).

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).

由于睡眠和TimeUnit会使paintComponent冻结,因此我有点无能为力.我尝试使用计时器进行延迟,但是我失败了.在这种情况下,我不知道如何使用计时器.

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?

推荐答案

您应该首先查看 Swing中的并发

您必须将Swing Timer视为伪循环,它在每次滴答时都会更新一些值,当您调用repaintpaintComponent方法时,可以更新UI适当地

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

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-单击面板以启动计时器

ps- Click the panel to get the timer to start

这篇关于在Java中实现计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆