JAVA Swing MVC - 主控制器? [英] JAVA Swing MVC - Main Controller?

查看:212
本文介绍了JAVA Swing MVC - 主控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,设计我想开发的应用程序的架构。我正在开发JAVA,我开始工作这个应用程序,因为我想加深我对JAVA,架构和模式的整体了解。我想遵循指南,使一个可重复使用的低耦合应用程序,就像它应该是。应用程序只有一个JFrame,但在里面有几个JPanels,每个代表应用程序的一个模块。



问题是:在JAVA Swing中,如何实现适当的MVC模式?
我在如何理解应该做的方式上努力。



我应该有一个主控制器类,保存对所有其他控制器的引用?
(我有一个图片演示这个,这里: https: //docs.google.com/file/d/0B7tBdn5slIFeY2FoSmxESTREQ1k/edit?usp=sharing



在这种情况下,如果所有需要的事件更改正在呈现的模块重定向到主控制器?



或者我应该只是将JFrame与应用程序的控制器耦合,并直接与他们通信? p>

基本上,我想知道我是否需要一个类来管理所有其他类。
我已经读了几个解释和不同的意见,但我相信这是一个更具体的。



希望我已经清楚了)



编辑:
a应用程序用法示例:




  • 在应用程序的整个生命周期中只有一个(只有一个)JFrame;

  • 在BorderLayout.WEST中,

  • 应用程序的当前模块将位于中心,如BorderLayout.CENTER中;

  • 当用户按下菜单的一个按钮时,相应的模块被加载到BorderLayout.CENTER中;



有它自己的控制器,并且此控制器与JFrame通信?而JFrame加载新模块到它的布局?或者应该JFrame有自己的控制器(或者模型,正如吉尔伯特·布朗克所说)?



我知道这可能看起来具体或容易理解,想要一个桌面应用程序,我很难理解这一点。

解决方案

应用程序视图。应用程序通过GUI模型与GUI交互。


或者,我应该将JFrame与应用程序的控制器相耦合,与他们?


这是我做的。我已经将控制器类打包在一起,但是我从来没有创建过一个主控制器类。



我将GUI控制器类保存在与其他应用程序控制器类,像数据访问对象。



我通常把每个JPanel放在自己的类中,但我不会称之为需求。 JFrame有自己的类,虽然JFrame的实例和GUI模型的实例被传递给几乎所有的GUI组件。这样可以进行菜单操作。



交通信号GUI 文章介绍了如何编写一个非常简单的GUI的基础知识。



编辑以响应问题中的更改。 / strong>



GUI控制器与GUI模型是分开的。 GUI模型包含组成GUI的所有数据元素。 JTextField的字符串,JTables的DefaultTableModels。



根据您的应用程序设计,我建议您为要放在应用程序中心的每个JPanel创建一个Java类。您的JFrame将基于菜单控制显示哪个JPanel。我还建议您查看 JTabbedPane ,它使用一个不同的用户界面来完成选择使用哪个面板的任务。



假设你要使用左边的菜单,每个菜单选项(切换JButton? )将有自己的控制器方法或类。这些控制器必须有一个JFrame实例,因此控制器可以调用JFrame类中的方法,将相应的面板放在显示的中心。控制器决定调用哪个方法,但方法本身是JFrame类的一部分。



我一直在谈论JFrame和JPanel类。请务必使用组合而不是继承来构建这些类。 JFrame类包含一个JFrame。它不扩展JFrame。只有当你想要覆盖一个组件方法时,你才能扩展一个Swing组件。


I'm having some troubles designing the architecture of an application I'm trying to develop. I'm working on JAVA, and I started working on this application because I want to deepen my overall knowledge of JAVA, architectures and patterns. I want to follow the guidelines to make a reusable, low coupled application, like it should be. The application has only one JFrame, but inside it there are several JPanels, each one representing a module of the application.

The question is: in JAVA Swing, how to implement an appropriate MVC pattern? I struggle on how to understand the way it should be done.

Should I have a main Controller class, that holds references to all the other Controllers? (I have an image to demonstrate this, here: https://docs.google.com/file/d/0B7tBdn5slIFeY2FoSmxESTREQ1k/edit?usp=sharing)

And in this case, should all the events that require changing the module that is being presented redirect to the main Controller?

Or should I just couple the JFrame with the Controllers of the application, and communicate directly with them?

Basically, I would like to know if i need to have a class that 'manages' all the others. I have already read several explanations, and different opinions, but I believe this is a little more specific.

Hope I have made myself clear (and hope as well that my explanation is better than my drawing :)).

EDIT: a sample of the application usage:

  • One (an only one) JFrame throughout all the lifecycle of the application;
  • the menu will be on the left side, as in BorderLayout.WEST;
  • the current module of the application will be in the center, as in BorderLayout.CENTER;
  • when the user presses one button of the menu, the corresponding module is loaded into the BorderLayout.CENTER;

Should the menu (View) have it's own Controller, and this Controller communicate with the JFrame? And the JFrame load the new module into it's Layout? Or should the JFrame have its own Controller (or Model, as Gilbert Le Blanc said)?

I know this may seem to specific, or easy to understand, but everytime I think of an desktop application, I struggle to understand this.

解决方案

When you have an application with a GUI, the GUI model becomes the application view. The application interacts with the GUI through the GUI model.

Or should I just couple the JFrame with the Controllers of the application, and communicate directly with them?

This is what I've done. I've packaged the controller classes together, but I've never created one main controller class.

I keep the GUI controller classes in a separate package from any other application controller classes, like the data access objects.

I usually put each JPanel in its own class, but I wouldn't call that a requirement. The JFrame has its own class, although the instance of the JFrame and the instance of the GUI model are passed to almost all of the GUI components. This makes menu actions possible.

This Traffic Signal GUI article goes over the basics of how to code a very simple GUI.

Edited to respond to the changes in the question.

The GUI controller is separate from the GUI model. The GUI model contains all of the data elements that make up your GUI. Strings for JTextFields, DefaultTableModels for JTables.

Based on your application design, I'd recommend that you create a Java class for every JPanel that you want to put in the center of your application. Your JFrame will control which JPanel is displayed, based on the menu. I'd also suggest that you look at the JTabbedPane which uses a different user interface to accomplish the task of choosing which panel to work with.

Assuming you're going with the menu on the left, each menu option (toggling JButton?) will have it's own controller method or class. These controllers have to have an instance of the JFrame so the controller can call the method in the JFrame class that puts the appropriate panel in the center of the display. The controller decides which method to call, but the methods themselves are a part of the JFrame class.

I've been talking about JFrame and JPanel classes. It's important that you use composition rather than inheritance to build these classes. The JFrame class contains a JFrame. It does not extend JFrame. The only time you extend a Swing component is when you want to override a component method.

这篇关于JAVA Swing MVC - 主控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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