AbstractAction as WindowListener [英] AbstractAction as WindowListener

查看:138
本文介绍了AbstractAction as 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.

  • LinePanel which connects buttons and keys.
  • ScrollAction which leverages existing Swing actions.
  • KeyPadPanel which illustrates forwarding actions.
  • GraphPanel which shows graph editor actions in a tool bar.

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

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