Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在swing中手动调用操作?_Java_Swing_Action - Fatal编程技术网

Java 如何在swing中手动调用操作?

Java 如何在swing中手动调用操作?,java,swing,action,Java,Swing,Action,在我的一生中,我似乎找不到有关Java Swing操作的详细信息:'(当我遇到它们时,我立即意识到它们的有用性。到目前为止,它们都很容易使用。现在我只剩下一件事:如何手动运行它们?我指的是代码?请注意,我正在使用Netbeans构建GUI(如果这有什么区别的话).我已经做到了: Application a = Application.getInstance(JPADemoApp.class); ApplicationContext ctx = a.getContext(); ActionMap

在我的一生中,我似乎找不到有关Java Swing操作的详细信息:'(当我遇到它们时,我立即意识到它们的有用性。到目前为止,它们都很容易使用。现在我只剩下一件事:如何手动运行它们?我指的是代码?请注意,我正在使用Netbeans构建GUI(如果这有什么区别的话).我已经做到了:

Application a = Application.getInstance(JPADemoApp.class);
ApplicationContext ctx = a.getContext();
ActionMap am = ctx.getActionMap(JPADemoView.class, this.app);
Action act = am.get("fetchOrders");
(为了简化调试,我将所有代码都写在单独的行中)


现在我有了一个对操作的有效引用。现在我如何运行它呢?

如果你想手动运行你的操作,你可以生成一个
ActionEvent
,并将它传递到
actionPerformed
方法中,你的
Action
必须实现,因为
Action
接口扩展了
ActionListener

<由于是一个AN,您可能需要考虑实现一种方法来揭示火灾行为的方法。

< P>可以简单地直接调用Actudio事件的方法:

for(ActionListener a: buttonExample.getActionListeners()) {
    a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null) {
          //Nothing need go here, the actionPerformed method (with the
          //above arguments) will trigger the respective listener
    });
}

我又遇到了这个问题,在我看来,你的答案比@b1nary.atr0phy的答案更详细。尽管它有更多的更新投票,我还是会接受这个。我猜另一个会吸引更多的投票,因为它是可复制/粘贴的。也许一个代码示例会改进这个答案?当然,一个代码示例会很方便-不需要支持端口复制/粘贴编程,但它将以程序员更容易理解的方式说明该概念。