English 中文(简体)
添加行动的方法
原标题:Method to add ActionListener

我要说的是,我有一个叫 but的纽特。 如果我想为纽伦特人采取行动,那么我应该选择什么方法: (在后一种情况下,你必须延长行动周期)

// Imports
public class Test{
    JButton test = new JButton();
    Test(){
        // Pretend there is an adapter
        test.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                ...
            }
        });
    ...
}

// Imports
public class Test2 extends ActionListener{
    JButton button2 = new JButton();
    Test2(){
        button2.addActionListener(this);
    }
    // Pretend there is an adapter

    public void actionPerformed(ActionEvent e){
        Object src = e.getSource();
        if(src == button2){
            ...
        }else{
            ...
    }
}
最佳回答

In the second case, you have to implement the ActionListener interface. Other than that, the answer is "it depends". If it makes sense to reuse the same action listener for several graphical components, then use the second version. If handling the event is a one-shot affair for a single component, then use the first version.

问题回答

首先。 你们的“全球倡议”班子也在执行你们的听众,因为这要求全球倡议或观点小组做太多的工作。 如果你想要把你的听众守则与你的观察守则完全分开,那么,即使你以匿名的听众为课,你会更早一些。

如果每个听众都是独一无二的,你很可能要使用匿名课(第一例)。 否则,如果你不得不重新制定同样的准则,那么最好在指定类别(如你的第二个例子)中加以执行,以便你能够重新利用同样的听众。

然而,你可能认为,将听众的节目分为不同的班级(即使是执行“生命行动”的班级),而不是扩大行动范围(如你的第二个例子)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签