Java事件绑定 [英] Java Event Binding

查看:205
本文介绍了Java事件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Windows应用程序开发中使用java。问题是这个如何将事件绑定到自定义类方法?



由于fas我看到我可以注册侦听器类摆动组件来处理事件。但是我必须实现一个实现例如ActionListener接口来处理事件的类,然后实现actionPerformed方法,这意味着我必须处理每个事件的一个类?



我不能有一个类听所有组件的事件,并且这个类中的每个方法都执行事件处理?



一个例子:

  class MyEventListener {//伪代码[无构造函数等] 

public void handleSubmitFormBtn(Event e){

}

//其他方法去处理来自其他摆动组件的事件
}

注意:我不知道方法签名,但我希望你得到点。



结论:ONE处理事件的方法摆动组件有可能吗?每个事件的唯一方法是创建一个类吗?我不能将事件处理路由到单个类的特定方法?

解决方案

您有几个选择,优惠/缺点。



匿名内部课程

  component.addActionListener(
new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{
outerClassesMethod();
}
});

内部课程

  class Foo 
implements ActionListener
{
public void actionPerformed(final ActionEvent e)
{
outerClassMethod();
}
}

外部类 p>

  public class Foo 
implements ActionListener
{
private final OuterClass target;

public Foo(final OuterClass t)
{
target = t;
}

public void actionPerformed(final ActionEvent e)
{
target.targetClassMethod();
}
}

类实现侦听器 / p>

  public class OuterClass 
implements ActionListener
{
public void actionPerformed(final ActionEvent e)
{
method();
}

//代码中的其他地方
{
component.addActionListener(this);
}
}

每种方式都有好坏。 p>

匿名内部类不会允许你做你所要求的,它只能实现一个监听器。



其他三个都将允许你做你想要的(只需添加,WindowListener到实现列表中的exaple)。你可能希望内部类或外部类实现侦听器的方式来做你想要的。我建议,因为听众可能非常高度地耦合到你的程序,你需要做一个大量的if语句,以确定哪个控件被执行来执行实际的操作(你使用evt.getSource()找出正在执行的控件,然后将其转到实例变量中,以查看它是哪一个)。



但是,除非您处于内存受限的设备,否则作为Android手机,您可能不应该为所有侦听器执行一种方法,因为它很容易导致非常糟糕的代码。如果记忆是一个问题,那就去找它,但如果不是,你最好做以下一件事:




  • 每个控件一个监听器类

  • 所有控件的每个事件类型的一个监听器类

  • 每个事件类型的每个控件的一个监听器类



我喜欢以下列方式编码,我发现它是最灵活的:

  public class Outer 
extends JFrame
{
private final JButton buttonA;
私人最终JButton buttonB;

{
buttonA =新的JButton(A);
buttonB = new JButton(B);
}

//除非外部类是final
public void init()
{
buttonA.addActionListener(新的AListener());
buttonB.addActionListener(new BListener());
}

private void aMethod()
{
}

private void bMethod()
{


public void AListener
implements ActionListener
{
public void actionPerformed(final ActionEvent evt)
{
aMethod();
}
}

public void BListener
implements ActionListener
{
public void actionPerformed(final ActionEvent evt)
{
bMethod();
}
}
}

我喜欢这样做,因为它强制方法从监听器,这意味着我只有一个地方寻找代码(不分散在整个内部类)。这也意味着aMethod()和bMethod()可能被重用 - 如果代码在一个不实用的监听器(这是可能的,但不值得的努力)。



上述方式也是一致的,我喜欢大多数事情的一致性,除非有一个很好的理由不这样做。例如在Android上我不这样做,因为类创建是昂贵的(我仍然有监听器调用方法,但类本身实现监听器,我做一个if语句)。


I'm new in windows application development using java. The question is this "How to i bind events to custom class methods?

As fas i have seen i can register listener classes to swing components to handle events. That is OK but i have to implement a class that implements e.g. the ActionListener interface to handle an event and then implement the actionPerformed method. That means ONE class FOR each event i have to handle?

Can't i have ONE class "listening" for events from all components and have each method in this class do the event handling?"

An example:

class MyEventListener { //pseudo code [no constructors, etc]

    public void handleSubmitFormBtn(Event e) {

    }

    //other methods go here handling events from other swing components
}

Note: I am not sure about the method signature but i hope that you get the point.

Conclusion: ONE method to handle events fired from swing components..is it possible? Is the creation of ONE class for each the event the only way? Can't i route event handling to specific methods for a single class?

解决方案

You have a few choices, each with their particular benefits/drawbacks.

anonymous inner classes

component.addActionListener(
    new ActionListener()
    {
        public void actionPerformed(final ActionEvent e)
        {
            outerClassesMethod();
        }
    });

inner class

class Foo
    implements ActionListener
{
    public void actionPerformed(final ActionEvent e)
    {
        outerClassMethod();
    }
}

outer class

public class Foo
    implements ActionListener
{
    private final OuterClass target;

    public Foo(final OuterClass t)
    {
        target = t;
    }

    public void actionPerformed(final ActionEvent e)
    {
        target.targetClassMethod();
    }
}

class implements listener

public class OuterClass
    implements ActionListener
{
    public void actionPerformed(final ActionEvent e)
    {
        method();
    }

    // somewhere else in the code
    {
         component.addActionListener(this);
    }
}

Each way has good and bad to it.

The anonymous inner class will not allow you to do what you are asking, it can only implement one listener.

The other three will all allow you to do what you want (just add , WindowListener to the implements list for exaple). You likely want the inner class or outer class implementing the listener way to do what you want. I suggest that because the listener is likely very highly coupled to your program, and you will need to do a large set of "if" statements to figure out which control was acted on to perform the actual action (you use evt.getSource() to figure out which control was being acted on and then comare it to your instance variables to see which it was).

However, unless you are in memory constrained device, such as an Android phone, you probably should not do one method for all listeners as it can easily lead to very bad code. If memory is an issue, then go for it, but if it isn't you are better of doing one of the following things:

  • one listener class per control
  • one listener class per event type for all controls
  • one listener class per control per event type

I prefer to code the following way, I find it to be the most flexible:

public class Outer
    extends JFrame
{
    private final JButton buttonA;
    private final JButton buttonB;

    {
        buttonA = new JButton("A");
        buttonB = new JButton("B");
    }

    // do not put these in the constructor unless the Outer class is final
    public void init()
    {
        buttonA.addActionListener(new AListener());
        buttonB.addActionListener(new BListener());
    }

    private void aMethod()
    {
    }

    private void bMethod()
    {
    }

    public void AListener
        implements ActionListener
    {
        public void actionPerformed(final ActionEvent evt)
        {
            aMethod();
        }
    }

    public void BListener
        implements ActionListener
    {
        public void actionPerformed(final ActionEvent evt)
        {
            bMethod();
        }
    }
}

I prefer this way because it forces the methods out of the listeners, which means I only have one place to look for the code (not scattered throughout the inner classes). It also means that it is possible that aMethod() and bMethod() can be reused - if the code is in a listener that isn't practical (it is possible, but not worth the effort).

Doing it the above way is also consistent, and I prefer consistency over most things unless there is a good reason not do it. For instance on the Android I do not do that since class creation is expensive (I still have the listener call methods only, but the class itself implements the listeners and I do an if statement).

这篇关于Java事件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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