AbstractAction 作为 WindowListener [英] AbstractAction as WindowListener

查看:20
本文介绍了AbstractAction 作为 WindowListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用 Action 对象将我的 GUI 应用程序中的功能与状态分开.我已经成功地使用这些来创建具有相同功能的菜单项和按钮.

I'm trying to separate function from state in my GUI application by the use of Action objects. I've been successful in using these to create menu items and buttons that have the same functionality.

我的问题是:我希望菜单中的退出"项和框架的关闭按钮具有相同的操作.

My problem is this: I want to have the same Action for both the 'exit' item in my menu and the close button of the frame.

目前我已经能够通过将以下 WindowListener 添加到框架中来解决它:

Currently I've been able to solve it by adding the following WindowListener to the frame:

private class MainWindowListener extends WindowAdapter {
    @Override
    public void windowClosing(WindowEvent e) {
        new ExitAction(model).actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Exit"));
    }
}

难道没有更简单更直接的方法来做到这一点吗?

Isn't there a simpler more straightforward way to do this?

推荐答案

转发事件很方便,但是你也可以使用dispatchEvent(),如图此处.

Forwarding events is handy, but you can also use dispatchEvent(), as shown here.

附录:更多使用 Action 如下所示.

Addendum: More examples that use Action are shown below.

这篇关于AbstractAction 作为 WindowListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆