我可以从其他类方法执行ActionListener方法actionPerformed吗?

时间:2020-12-07 22:22:30

I have

ActionListener actionListenerRing = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {

defined in my class constructor. How to call actionPerformed method form main method?

在我的类构造函数中定义。如何调用actionPerformed方法表单主要方法?

I just wanted to know If i can call action listener directly form any other part of my class, OR I should move code form action listener to other method to be accessible to both ActioListener AND other class methods.

我只是想知道如果我可以直接从我的类的任何其他部分调用动作侦听器,或者我应该将代码形式动作侦听器移动到其他方法,以便可以访问ActioListener和其他类方法。

2 个解决方案

#1


3  

Typically, you do not create a listener without attaching it to something. So if your main method must call such an ActionListener directly something might be wrong with your design. Perhaps you can comment on what you try to achieve.

通常,如果不将其附加到某些内容,则不会创建侦听器。因此,如果您的main方法必须直接调用这样的ActionListener,那么您的设计可能会出现问题。也许你可以评论你想要实现的目标。

You might want to read the whole observer design pattern, but in short you add your listener to another object if you want to be informed when it performs an action. For example:

您可能希望阅读整个观察者设计模式,但简而言之,如果您希望在执行操作时获得通知,则将侦听器添加到另一个对象。例如:

JButton button;//a button I obtained from somewhere and in 
// which I am interested
button.addActionListener( new ActionListener(){ 
  public void actionPerformed( ActionEvent actionEvent ){
     //do something with the fact that the button has been pressed
  }
});

In the above example, the button will inform the ActionListener I added when it's pressed, and I can react on that by placing the relevant code in the actionPerformed method. But I am not going to call my listener myself.

在上面的例子中,按钮将通知我按下时添加的ActionListener,我可以通过将相关代码放在actionPerformed方法中来做出反应。但我不打算自己打听我的听众。

#2


1  

Make actionListenerRing a static (class) variable (assuming main is in the same class) instead of local to constructor then it will be accessible from the main and you can call the actionPerformed method as actionListenerRing.actionPerformed.

使actionListenerRing成为静态(类)变量(假设main在同一个类中)而不是构造函数的本地变量,然后可以从main访问它,并且可以将actionPerformed方法作为actionListenerRing.actionPerformed调用。

But I think calling that method manually is not a good idea.

但我认为手动调用该方法并不是一个好主意。

#1


3  

Typically, you do not create a listener without attaching it to something. So if your main method must call such an ActionListener directly something might be wrong with your design. Perhaps you can comment on what you try to achieve.

通常,如果不将其附加到某些内容,则不会创建侦听器。因此,如果您的main方法必须直接调用这样的ActionListener,那么您的设计可能会出现问题。也许你可以评论你想要实现的目标。

You might want to read the whole observer design pattern, but in short you add your listener to another object if you want to be informed when it performs an action. For example:

您可能希望阅读整个观察者设计模式,但简而言之,如果您希望在执行操作时获得通知,则将侦听器添加到另一个对象。例如:

JButton button;//a button I obtained from somewhere and in 
// which I am interested
button.addActionListener( new ActionListener(){ 
  public void actionPerformed( ActionEvent actionEvent ){
     //do something with the fact that the button has been pressed
  }
});

In the above example, the button will inform the ActionListener I added when it's pressed, and I can react on that by placing the relevant code in the actionPerformed method. But I am not going to call my listener myself.

在上面的例子中,按钮将通知我按下时添加的ActionListener,我可以通过将相关代码放在actionPerformed方法中来做出反应。但我不打算自己打听我的听众。

#2


1  

Make actionListenerRing a static (class) variable (assuming main is in the same class) instead of local to constructor then it will be accessible from the main and you can call the actionPerformed method as actionListenerRing.actionPerformed.

使actionListenerRing成为静态(类)变量(假设main在同一个类中)而不是构造函数的本地变量,然后可以从main访问它,并且可以将actionPerformed方法作为actionListenerRing.actionPerformed调用。

But I think calling that method manually is not a good idea.

但我认为手动调用该方法并不是一个好主意。