简体   繁体   English

了解java中的事件侦听器

[英]Understanding event listeners in java

I'm new to java and I'm still trying to understand the language, so I apologize if this question might sound noobish. 我是java的新手,我还在努力理解这门语言,所以如果这个问题听起来很吵,我会道歉。

There's something I don't understand about listeners, sometime you can see a listener declared in such a fashion: 关于听众我听不懂,有时你可以看到以这种方式宣告的听众:

    private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {

// Some code

}

};

or: 要么:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });

The thing that perplex me the most is that semicolon and parenthesis after the end of the method. 令我困惑的是在方法结束后的分号和括号。

I understand inner classes for the purpose of having multiple listeners, but I don't understand this mixed declaration of variables and method. 我理解内部类是为了拥有多个侦听器,但我不理解这个变量和方法的混合声明。

Which purpose it has? 它的目的是什么?

How is it declared? 怎么声明?

WTF? WTF? :P :P

cheers :) 欢呼:)

Normally defining a class, instantiating an instance, and then using that instance are done separately: 通常定义一个类,实例化一个实例,然后使用该实例分别完成:

class MyListener extends OnClickListener {
    public void onClick(View v) {
        // my onClick code goes here
    }
}

MyListener foo = new MyListener();

button.setOnClickListener(foo);

But sometimes you need a subclass that you will instantiate only once immediately, which is often the case for event handlers. 但有时您需要一个只能立即实例化一次的子类,这通常是事件处理程序的情况。 It is convenient to define it and instantiate it together using an anonymous (inner) class: 定义它并使用匿名(内部)类将它实例化是很方便的:

OnClickListener foo =
    new OnClickListener() {
        public void onClick(View v) {
            // my onClick code goes here
        }
    };

button.setOnClickListener(foo);

But since foo is only used once, we can takes this one step further and eliminate the local variable foo as well, so: 但是由于foo只使用了一次,我们可以更进一步并消除局部变量foo ,因此:

button.setOnClickListener(foo);

can be formatted as: 可以格式化为:

button.setOnClickListener(
    foo
);

substitute in the value of foo : 替换foo的值:

button.setOnClickListener(
    new OnClickListener() {
        public void onClick(View v) {
            // my onClick code goes here
        }
    }
);

reformatted again without some much whitespace to see it as it is often written: 重新格式化,没有太多的空白,因为它经常写:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // my onClick code goes here
    }
});

I think this last format hurts readability. 我认为最后这种格式会损害可读性。 I format my anonymous classes similar to how it is in the next to last format - the better readability (IMHO) is worth a little extra whitespace. 我格式化我的匿名类,类似于它的最后一种格式 - 更好的可读性(恕我直言)值得多一些额外的空白。

Break it down... 分解......

javax.swing.SwingUtilities.invokeLater(Runnable runnable);

invokeLater() is a static method on SwingUtilities that takes a Runnable as a parameter. invokeLater()SwingUtilities上的一个静态方法,它将Runnable作为参数。

Note the right parenthesis and semi-colon at the end; 注意最后的右括号和分号; it's just a standard method, right? 这只是一种标准方法,对吧?

The stuff in the middle is just creating a new instance of a Runnable to pass to the method. 中间的东西只是创建一个Runnable的新实例来传递给方法。 You're recognize it better if I assigned it to a reference: 如果我将它分配给参考,你会更好地认识到它:

Runnable parameter = new Runnable() 
{
    public void run() 
    {
        createAndShowGUI();
        }
    }
};

There's nothing perplexing about the semicolon or parentheses here. 这里的分号或括号没有什么困惑。 Basically, this is one way to assign an anonymous class in Java. 基本上,这是在Java中分配匿名类的一种方法。 If you are not used to this construct, you can certainly create your own class that implements Runnable, and assign it like this:- 如果您不熟悉此构造,您当然可以创建自己的类来实现Runnable,并将其分配如下: -

public class YourRunnableClass implements Runnable {
   public void run() {
      ...
   }
}

Then you can make it look nicer, like this:- 然后你可以让它看起来更好,像这样: -

javax.swing.SwingUtilities.invokeLater(new YourRunnableClass());

P/S: Try not to use "WTF" in your post. P / S:尽量不要在帖子中使用“WTF”。 :) :)

That is an anonymous inner class . 那是一个匿名的内部阶级 It's a very common and handy construct. 这是一个非常常见且方便的结构。 Read the tutorial I linked to! 阅读我链接的教程!

That code applies anonymous classes . 该代码适用于匿名类 Search and read for this topic. 搜索并阅读此主题。

The semicolon terminates the assignment respectively the method invocation. 分号分别终止赋值方法调用。

PS. PS。 I always was thinking the name is something misleading because what you really get is an Object. 我一直认为这个名字是误导性的,因为你真正得到的是一个对象。

I'll try to exemplify the second example. 我将尝试举例说明第二个例子。

In this case invokeLater method expects any object which implements the Runnable interface. 在这种情况下,invokeLater方法需要任何 实现 Runnable接口的对象。

for example, this is an alternative way of writing the same thing without an anonymous class: 例如,这是在没有匿名类的情况下编写相同内容的另一种方法:

class Example implements Runnable{
  public void run(){
    // do something
  }
}

and your example could be: 你的例子可能是:

javax.swing.SwingUtilities.invokeLater(new Example());

But since this Example class it's likely you'll use it only once, it's more convenient to use an anonymous class. 但是因为这个Example类很可能你只使用它一次,所以使用匿名类会更方便。

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

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