简体   繁体   English

如何在JPanel中使用Swing计时器

[英]How to use a swing Timer with JPanel

I'm fairly sure I understand how a swing timer works, I just cannot figure out how to apply it in my code. 我相当确定我了解摆动计时器的工作原理,只是无法弄清楚如何在我的代码中应用它。 The way I apply it in my code doesn't allow it to draw because Graphics g is outside of its scope. 我在代码中应用它的方式不允许它绘制,因为Graphics g不在其范围内。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JayFrame extends JFrame
{
  public JayFrame()
  {
    super("My Frame");
    setContentPane(new DrawPane());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1200, 675);
    setResizable(false);
    setVisible(true);
  }

  class DrawPane extends JPanel
  {
    Timer timer = new Timer(1000, new MyTimer());

    public void paintComponent(Graphics g)
    {
      //Paint stuff
      super.paintComponent(g);

      timer.start();

      for(int i = 0; i < 1000; i += 110)
      {
        g.fillRect(i, 10, 100, 100);

        try{Thread.sleep(100);}
        catch(InterruptedException ie){}
      }

      timer.stop();
    }
  }

  class MyTimer implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      //Loop stuff
      repaint();
    }
  }

  public static void main(String[] args)
  {
    new JayFrame();
  }
}

EDIT: I have updated the code to show what I think should work, but does not. 编辑:我已经更新了代码,以显示我认为应该工作的内容,但没有。 So I probably do have a flawed understanding of swing timers. 因此,我可能对摇摆计时器的理解确实有缺陷。

  • Don't call timer.start(); 不要调用timer.start(); and/or timer.stop() in the paintComponent method, this makes no sense what so ever, painting may occur for any number of reasons, my of which you have no control over. paintComponent方法中的timer.stop() ,这毫无意义,可能由于多种原因发生绘画,但您无法控制这些原因。
  • Don't call Thread.sleep in your paintComponent , that's the point of have the Timer . 不要在paintComponent调用Thread.sleep ,这就是Timer You're just preventing Swing from updating the screen or process any new events 您只是在阻止Swing更新屏幕或处理任何新事件

Timer acts as a psudo loop, on each iteration of the Timer , you update some state, check some exit condition and make your decisions about what should happen. Timer充当伪循环,在Timer每次迭代中,您都会更新某些状态,检查某些退出条件并就应该发生的事情做出决定。

paintComponent simply paints the current state. paintComponent只是绘制当前状态。

For example: 例如:

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JayFrame extends JFrame {

    public JayFrame() {
        super("My Frame");
        setContentPane(new DrawPane());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 675);
        setResizable(false);
        setVisible(true);
    }

    class DrawPane extends JPanel {

        private int x = 0;

        public DrawPane() {
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    x += 110;
                    if (x >= 1000) {
                        x = 1000;
                        ((Timer)e.getSource()).stop();
                    }
                    repaint();
                }
            });
            timer.start();
        }

        public void paintComponent(Graphics g) {
            //Paint stuff
            super.paintComponent(g);

            g.fillRect(x, 10, 100, 100);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new JayFrame();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM