有几种执行事件侦听器的常用方法(在下面的代码中,我想到的唯一一个就是静态内部类)。下面的代码使用ActionListener,因为它很简单,但是您可以将其应用于任何侦听器。
请注意,“这种”方式(让类实现了侦听器)会导致大量的if / else语句集。我会说这是最糟糕的方法。我不喜欢使用“票据交换所”方法,原因有两个:
1)它们很大2)很想在方法内部完成工作,而不是让每个if / else都调用一个方法来完成工作(正如您所看到的,这就是我在这里所做的… oops: -)
我也不喜欢匿名方式,原因有两个:
1)您无法轻松地重复使用代码,因此一段时间后您可能会发现重复的代码2)我发现它破坏了代码的读取(其他人则不同意……个人品味)。我想每个人都会同意,如果您要执行5-10行以上的操作,那么匿名内部类并不是一个好主意(我说超过2行太多了)。
这留下了内在和外在的方式。当我编写与监听的GUI不紧密相关的监听器时,我将使用外部方式。如果侦听器不需要属于类(在这种情况下为Testframe)的信息(成员变量/方法),则可以使用外部类。在下面的示例中,我传入了“
this”,以便外部侦听器可以访问GUI …如果我要编写类似的代码,则将其设为内部类,因为它需要GUI进行某些操作。
因此,我的优先顺序是:
- 内部类(如果可能,则为静态,但是如果将其设为静态,我将选择外部类)
- 外层阶级
- 匿名内部类(稀有)
- 让类自己实现它(我永远不会这样做。从不!)
这是代码
import java.awt.FlowLayout;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.Jframe;import javax.swing.SwingUtilities;public class Main{ public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { final Testframe frame; frame = new Testframe(); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.setBounds(new Rectangle(10, 10, 300, 300)); frame.init(); frame.setVisible(true); }}class Testframe extends Jframe implements ActionListener{ private final JButton aBtn; private final JButton bBtn; public Testframe() { super("Test"); aBtn = new JButton("A"); bBtn = new JButton("B"); } public void init() { setLayout(new FlowLayout()); add(aBtn); add(bBtn); // the class, since it implements ActionListener aBtn.addActionListener(this); bBtn.addActionListener(this); // outer classes aBtn.addActionListener(new OuterAListener(this)); bBtn.addActionListener(new OuterBListener(this)); // inner class aBtn.addActionListener(new InnerAListener()); bBtn.addActionListener(new InnerBListener()); // anonymous classes aBtn.addActionListener( new ActionListener() { public void actionPerformed(final ActionEvent e) { System.out.println ("Hi from Anonymous A"); } }); bBtn.addActionListener( new ActionListener() { public void actionPerformed(final ActionEvent e) { System.out.println ("Hi from Anonymous B"); } }); } public void actionPerformed(final ActionEvent evt) { final Object source; source = evt.getSource(); if(source == aBtn) { System.out.println ("Hi from this A"); } else if (source == bBtn) { System.out.println ("Hi from this B"); } else { // ??? } } private class InnerAListener implements ActionListener { public void actionPerformed(final ActionEvent e) { System.out.println ("Hi from Inner A"); } } private class InnerBListener implements ActionListener { public void actionPerformed(final ActionEvent e) { System.out.println ("Hi from Inner B"); } }}class OuterAListener implements ActionListener{ private final Testframe frame; public OuterAListener(final Testframe f) { frame = f; } public void actionPerformed(final ActionEvent e) { System.out.println ("Hi from Outer A"); }}class OuterBListener implements ActionListener{ private final Testframe frame; public OuterBListener(final Testframe f) { frame = f; } public void actionPerformed(final ActionEvent e) { System.out.println ("Hi from Outer B"); }}


