问题描述
假设我有一个swing GUI,我想听 MouseEvents
。你认为谁应该是听众课,谁应该负责?实施它的最佳或首选方式是什么?任何意见?我通常会这样:
Say I have a swing GUI, and I want to listen MouseEvents
. Who do you think should be the Listener class, who should be responsible? What is the best-or preferred way to implement it? Any opinions? I usually go like this:
public class MyPanel extends JPanel implements MouseListener{
private JTable table;
public void foo(){
table.addMouseListener(this);
}
/* MouseListener */
//Implement MouseListener here.
}
还有更好的方法吗?
编辑:感谢大家的智慧和帮助。我很感激。
Thank you everyone for the wisdom and help. I appreciate it.
推荐答案
有几种常见的方法可以做事件监听器(我能想到的唯一一种方法)在下面的代码中左边是静态内部类)。下面的代码使用ActionListener,因为它最简单,但您可以将该想法应用于任何侦听器。
There are a few common ways to do event listeners (the only one that I can think of that I left off in the code below is static inner classes). The code below uses ActionListener since it is simplest, but you can apply the idea to any listener.
请注意this方式(让类实现侦听器)可以导致一个巨大的if / else语句集。我会说这是最糟糕的方式因为这个。我不喜欢清算所方法有两个原因:
Notice that the "this" way (having the class implement the listener) can lead to a huge if/else set of statements. I'd say this is the worst possible way because of that. I dislike having "clearing house" methods for two reasons:
1)它们很大
2)它很有可能在方法内部做工作而不是比每个if / else调用一个方法来完成工作(正如你所看到的,这就是我在这里做的... oops: - )
1) they are large2) it is tempting to do the work inside of the method rather than having each if/else call a method to do the work (which, as you can see, is what I did here... oops :-)
我也不喜欢匿名方式有两个原因:
I also do not like the Anonymous way for two reasons:
1)你不能轻易地重复使用代码,这样你可能会发现你在一段时间后有重复的代码
2)我发现它打破了代码的阅读(其他人不同意......个人品味)。我想每个人都会同意,如果你做的超过5-10行,那么一个匿名的内部课程不是一个好主意(我会说超过2个是太多了)。
1) you cannot easily re-use the code so you may find that you have duplicate code after a while2) I find it breaks up the reading of the code (others disagree... personal taste). I think everyone would agree that if you are doing more than 5-10 lines that an anonymous inner class is not a good idea (I would say more than 2 is too much).
留下内在和外在的方式。当我编写一个与它正在监听的GUI没有紧密联系的监听器时,我会使用外部方式。如果监听器不需要属于该类的信息(成员变量/方法)(在本例中为TestFrame),我将使用外部类。在下面的示例中,我传入了this,以便外部侦听器可以访问GUI ...如果我要编写类似的代码,我会改为使其成为内部类,因为它需要来自GUI的东西。
That leaves the inner and the outer ways. I would use the outer way when I am writing a listener that is not tightly tied to the GUI that it is listening. If the listener doesn't need information (member variables/methods) that are part of the class (TestFrame in this case) I would go for the outer class. In the example below I passed in the "this" so that the outer listeners could access the GUI... if I were to write code like that I would instead make it an inner class since it requires something from the GUI.
所以,我的优先顺序是:
So, my order of preference is:
- 内部类(如果可能,静态,但是如果你把它变成静态的我会去外层)
- 外类
- 匿名内部类(罕见)
- 让类自己实现它(我永远不会这样做。从不!)
- inner class (static if possible, but if you are making it static I'd go for the outer class)
- outer class
- anonymous inner class (rare)
- have the class implement it itself (never would I do this. Never!)
这里是代码
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");
}
}
这篇关于听众有更好的练习吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!