问题描述
我在Java中创建了我第一个更大的应用程序。由于MVC只是我知道的模式,所以我决定使用它。但这个概念有问题。例如。我需要一个动作
(或一般事件)从2个地方(从 Button
框架和菜单项
)。它必须在至少2个地方和模型中进行更改。
我有一些想法,但他们似乎错了:
- 将控制器对象传递给每个视图元素,因此新创建的操作可以使用控制器的方法来修改应用程序的其余部分。
- Make控制器静态(由于相同的原因)
- 仅使控制器模型侦听器
请告诉我如何建立它。或者给我一些链接,一些易于分析的应用程序。
我的项目的来源在这里,如果有人想看看:
正确使用来封装功能,供不同组件(如菜单和按钮)使用。 引用了一系列示例。关于,请记住,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:
- Pass the controller object to every view element, so newly created actions could use controller's methods to modify rest of the application.
- Make controller static (for same reasons)
- 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中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!