如何处理Java MVC中的事件 [英] How to deal with events in Java MVC

查看:221
本文介绍了如何处理Java MVC中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中创建了我第一个更大的应用程序。由于MVC只是我知道的模式,所以我决定使用它。但这个概念有问题。



例如。我需要一个动作(或一般事件)从2个地方(从 Button 框架和菜单项)。它必须在至少2个地方和模型中进行更改。



我有一些想法,但他们似乎错了:


  1. 将控制器对象传递给每个视图元素,因此新创建的操作可以使用控制器的方法来修改应用程序的其余部分。

  2. Make控制器静态(由于相同的原因)

  3. 仅使控制器模型侦听器

请告诉我如何建立它。或者给我一些链接,一些易于分析的应用程序。



我的项目的来源在这里,如果有人想看看: https://github.com/Arrvi/ColorExtractor

解决方案

正确使用 操作 来封装功能,供不同组件(如菜单和按钮)使用。 此处引用了一系列示例。关于 MVC ,请记住,Swing使用可分离模型架构,检查此处。实际上,用户是控制器,而不是每个交互需要通过您的应用程序的控制器。


I'm creating my first "bigger" application in Java. As MVC is only pattern I know, I decided to use it. But there's something wrong with this concept.

For example. I need an Action (or generally event) fired from 2 places (from Button in frame and MenuItem). It has to do changes in at least 2 places and in the model.

I've got some ideas, but they seem wrong:

  1. Pass the controller object to every view element, so newly created actions could use controller's methods to modify rest of the application.
  2. Make controller static (for same reasons)
  3. Make controller only model listener

Please tell me how to build it. Or give me some links to some easy to analyse applications.

Source of my project is here, if anyone wants to have a look: https://github.com/Arrvi/ColorExtractor

解决方案

You are correct to use Action to encapsulate functionality for use by disparate components such as menus and buttons. A spectrum of examples is cited here. As regards MVC, recall that Swing uses a separable model architecture, examined here. In effect, the user is the controller, and not every interaction needs to pass through your application's controller.

这篇关于如何处理Java MVC中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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