简体   繁体   English

我可以在java中使用addActionListener(this)代替“ this”吗?

[英]What can I use in java for addActionListener(this) instead of 'this'?

Say, I have a class XFrame that extends JFrame and implements ActionListener. 说,我有一个扩展XFrame并实现ActionListener的XFrame类。 In the class I have a JButton b and I overrode the actionPerformed method. 在该类中,我有一个JButton b,并且覆盖了actionPerformed方法。 Now in the constructor I have to set b.addActionListener(this). 现在在构造函数中,我必须设置b.addActionListener(this)。 My question is: what does 'this' do in this case and what can I replace that with? 我的问题是:在这种情况下,“此”有什么作用,我可以用什么替代? I tried b.addActionListener(new XFrame()) but it didn't work. 我尝试了b.addActionListener(new XFrame()),但是没有用。

Try an anonymous class: 尝试匿名课程:

public class Test {

    public static void main(final String[] args) {
        final JFrame frame = new JFrame();
        final JButton button = new JButton("Test");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                System.out.println("pressed");
            }
        });

        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
    }
}

Or if you want to make your JFrame implement ActionListener instead of using an anonymous class, see this example . 或者,如果您想使JFrame实现ActionListener而不是使用匿名类,请参见以下示例 In this case, this would refer to the instance of the JFrame . 在这种情况下, this将引用JFrame的实例。

What do you mean by "it didn't work". 您所说的“无效”是什么意思。 I expect that it compile fine but it did not what you expected. 我希望它可以编译良好,但它没有您期望的那样。 It is normal because you are saying to swing that the actionListener is not the one from the XFrame that received the event, but the actionlistener related to another XFrame (the new XFrame() as parameter). 这是正常现象,因为您说的是actionListener不是来自接收事件的XFrame的那个,而是与另一个XFrame(将新的XFrame()作为参数)相关的actionlistener。 This another XFrame may not even be displayed on the screen ! 另一个XFrame甚至可能不会显示在屏幕上! So don't expect to see anything ;) 所以不要指望看到任何东西;)

To answer your question, you can replace "this" by any instance of ActionListener. 要回答您的问题,可以用ActionListener的任何实例替换“ this”。 Your XFrame happens to be one hence the "this". 您的XFrame恰好是一个,因此是“ this”。 But you could replace it by any other actionListener created anywhere else. 但是您可以将其替换为在其他任何地方创建的任何其他actionListener。 The actionListener WILL be notified. 将通知actionListener。 But the important point is that this ActionListener has to do something interesting (visible!) upon notification. 但是重要的一点是,该ActionListener必须在通知时做一些有趣的事情(可见!)。 And a "new XFrame()" can not as it is not displayed on screen ! 并且“ new XFrame()”不能显示,因为它没有显示在屏幕上! Only the currently displayed on screen XFrame can. 只有当前显示在屏幕上的XFrame可以。

In a complex system, you generally do not want your Frame to be also the one handling events because you want to separate the concepts. 在复杂的系统中,您通常不希望您的Frame也成为处理事件的一个对象,因为您希望将概念分开。 But in the simple programs you do when learning Java, it is usually easier to visually see a feedback given by the displayed Frame upon clicking a button. 但是,在学习Java时所用的简单程序中,通常更容易在单击按钮时以视觉方式查看显示的框架给出的反馈。

what does 'this' do in this case ...? 在这种情况下,“此”做什么?

The this passed to b.addActionListener(this) tells the button it should call the actionPerformed method of this object (ie that is the XFrame instance) whenever the action of the button is triggered (ie the button is clicked). this传递给b.addActionListener(this)告诉按钮,它应该调用actionPerformed方法this对象(即是XFrame实例)每当触发按钮的动作(即单击该按钮)。

what can I replace that with? 我可以用什么代替它?

You can replace it with any other object that implements the ActionListener interface. 您可以将其替换为实现ActionListener接口的任何其他对象。 Often this is done through an 'anonymous' class, as for the otherActionListener in the example below. 通常,这是通过“匿名”类完成的,就像下面示例中的otherActionListener

Another nice thing: you not only can 'replace' the this with some other ActionListener object, you may even add multiple ActionListener objects to the button. 另一个好消息:你不仅可以“替代”的this一些其他ActionListener对象,你甚至可以添加多个ActionListener对象的按钮。 Eg in the example below I added the XFrame instance ( this ) and the otherActionListener . 例如,在下面的示例中,我添加了XFrame实例( this )和otherActionListener When the button is clicked the actionPerformed methods of both objects will be executed. 单击该按钮时,将执行两个对象的actionPerformed方法。

Here a complete example: 这里是一个完整的例子:

import static javax.swing.JOptionPane.showMessageDialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class XFrame extends JFrame implements ActionListener {
    JButton b = new JButton("Click me");

    ActionListener otherActionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            showMessageDialog(XFrame.this, "The other action says: 'Hi'");
        }
    };

    public XFrame() {
        getContentPane().add(b);

        b.addActionListener(this);

        b.addActionListener(otherActionListener);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       showMessageDialog(this, "XFrame says: 'hello'");
    }

    public static void main(String[] args) {
        XFrame frame = new XFrame();
        frame.setSize(100,60);
        frame.setVisible(true);
    }
}

Take a look at this (pun intended) article. 看看这篇 (双关语)文章。 Also, another way you can do it is this way: 另外,您可以执行的另一种方法是:

ActionListener e = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
        }
};
b.add(e);

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

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