简体   繁体   English

在Java中使用Swing计时器

[英]Using a swing timer in java

I have not been coding in java for long, a month at most, but I have coded in multiple other object oriented languages. 我使用Java进行编码的时间最长(长达一个月),但是我已经使用其他多种面向对象的语言进行编码。

I am trying to use the swing timer included in java.swing.timer, and I read https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html to try and understand it. 我正在尝试使用java.swing.timer中包含的摆动计时器,并且阅读了https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html来尝试并理解它。

I understand that with timer = new Timer(speed, this) , speed is how often the timer fires, but I do not understand what this means. 我了解到使用timer = new Timer(speed, this) ,速度是计时器触发的频率,但是我不明白this意味着什么。 Also, I know that public void actionPerformed(ActionEvent e) will execute every time the timer fires, but is there a way for me to use two timers in the same class? 另外,我知道每次触发计时器时都会执行public void actionPerformed(ActionEvent e) ,但是有没有办法让我在同一类中使用两个计时器?

Any help is appreciated, thank you in advance. 任何帮助表示赞赏,在此先感谢您。

The constructor you're invoking has this signature: 您要调用的构造函数具有以下签名:

public Timer(int delay, ActionListener listener)

This means that, in the example, this refers to an instance of the ActionListener interface. 这意味着在示例中, this引用了ActionListener接口的实例。

In your example, the class containing the timer code happens to implement ActionListener . 在您的示例中,包含计时器代码的类恰好实现ActionListener This is simply for the convenience of having a succinct example. 这只是为了方便起见。

You can pass any instance of an ActionListener to your timer. 您可以将ActionListener任何实例传递给计时器。 If you're using Java 8, this is very simple using method references . 如果您使用的是Java 8,则使用方法引用非常简单。

public class TheExample
{
  public TheExample()
  {
    final Timer timerOne = new Timer(speedOne, this::timerOneMethod);
    final Timer timerTwo = new Timer(speedTwo, this::timerTwoMethod);
  }

  private void timerOneMethod(ActionEvent e)
  {
    // do something exciting
  }

  private void timerTwoMethod(ActionEvent e)
  {
    // do something else exciting
  }
}

If you're not using Java 8, the same thing can be achieved using anonymous inner classes . 如果您不使用Java 8,则可以使用匿名内部类实现相同的目的。 You can find an example of this way here . 您可以在此处找到这种方式示例

The javadoc for Timer tells you what that constructor is about - it takes an int and an instance of ActionListener . 定时器的Javadoc告诉你,构造就是 -它需要一个int和ActionListener的实例。

So, to answer your first question, in the example code from Oracle, timer = new Timer(speed, this) happens within the init method of that Applet that makes up the example UI thingy. 因此,为了回答您的第一个问题,在Oracle的示例代码中, timer = new Timer(speed, this)发生在构成示例UI的Applet的init方法内。 Thus: this refers to the "current" object (see here for more details), being that Applet object the whole method "belongs" to. 因此: 是指“当前”对象(有关更多详细信息,请参见此处 ),因为Applet对象是整个方法“所属”的对象。

And that only works because that example class TumleItem is declared as TumbleItem extends JApplet implements ActionListener . 而且这仅是有效的,因为在TumbleItem extends JApplet implements ActionListener声明了示例类TumleItem, TumbleItem extends JApplet implements ActionListener

For the record: understanding what this is about is absolutely basic stuff. 记录:了解是绝对的基础知识。 If you don't know what this is about, then you shouldn't be engaging in UI coding yet. 如果您不知道是什么意思,那么您不应该参与UI编码。 Learn to crawl before you try to run. 在尝试运行之前,请学习爬网。

For your second question: for each instance of a Timer you want to use, you need an ActionListener that you can pass in. The old-school way of doing that would be to use anonymous inner classes, like 第二个问题是:对于要使用的Timer的每个实例,都需要一个可以传递的ActionListener。传统的实现方法是使用匿名内部类,例如

timer = new Timer(someSpeed, new ActionListiner() {
   @Override
   public void actionPerformed(ActionEvent evt) {
      System.out.println("Whatever");
   }
}

Finally: that one example from Oracle is using Applets, and simply spoken: Applets are dead technology. 最后:Oracle的一个示例正在使用Applet,简单地说:Applet是无效的技术。 Don't get too hung up on them; 不要太挂在他们身上; you better avoid spending time on those completely. 您最好避免将时间完全花在这些东西上。

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

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