如何用Swing在Java中正确实现MVC? [英] How to correctly implement MVC in Java with Swing?

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

问题描述

如果您想了解更多详情,请与我们联系,或参考此问题的最后几行。我已经阅读了很多内容,我觉得我正在把一些简单的东西变成复杂的东西而且我仍然被困在那里,所以也许你可以在那些非常具体的方面帮助我。

If you would like more details please let me know, or refer to the last lines of this question. I've already read a lot and I feel I'm turning something simple into something complicated and I still get stuck here and there, so maybe you can help me in those very specific points.

我使用的是Netbeans IDE 7和JDK 7,没有框架。第一个窗口是JFrame,所有其他窗口都是JDialogs,modal = true。

I'm using Netbeans IDE 7 and JDK 7, and no frameworks. The first Window is a JFrame and all other windows are JDialogs with modal=true.

问题:


  1. 如何使用swing正确实现MVC模式?
    从下面的想法哪个更好:(A)还是(B)?或者可能是另一个...为什么它更好?

  1. How do I correctly implement the MVC pattern with swing? From the ideas bellow Which one is better: (A) or (B)? Or maybe another one... Why is it better?

(A)
主要:

(A) Main:

MyModel model
MyView view(model)

MyView:

MyController(this, model)

(B)

Main:

(B)
Main:

MyModel model
MyView View
MyController controller(view, model)


  • 当我点击MainFrame中的jbutton1我需要它来打开SettingsFrame进行编辑设置。我应该在哪里实例化SettingsFrame的View,Model和Controller?在MainFrame控制器中?

  • when I click jbutton1 in MainFrame I need it to open the SettingsFrame for editing settings. where should I instantiate the View, the Model and the Controller of the SettingsFrame? In MainFrame Controller?

    在MVC组织和实现方面,我应该如何处理(显然)缺少一个或两个MVC腿的更多特定功能(模型或视图或控制器)?我应该为他们创建空课吗?

    In terms of MVC organization and implementation, how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)? Should I create empty classes for them?

    a. The implementation of a TrayIcon
    b. A URL connection class (an HttpsUrlConnection which will update data in the main jframe and also upload/download files)
    c. A Directory Monitor (which will update data in the main jframe and also use the urlconnection to download a file)
    d. My own implementation of TableModel
    e. json
    


  • 如何在整个应用程序中正确保存和使用带有设置的对象?我将在不同的地方(视图,模型,控制器)需要它的信息,但在运行时可能会被用户更改。让这个模型成为单身人士是个好主意吗?

  • How to correctly keep and use an object with settings through the whole application? I will need it's information in different places (Views, Models, Controllers) but it might be altered by user during the runtime). Is it a good idea to make this model a singleton?

    我应该怎么做:

    a. View needs some data from the Model? 
    What I'm doing: using the reference of Model which I keep in the View
    b. View needs some data from the Controller?
    What I'm doing: using the reference of Controller which I keep in the View
    c. Model needs some data from the Controller?
    Still didn't happen but I have no idea how to do correctly
    d. Model needs some data from the View?
    What I'm doing: pulling all my hair from my head...
    e. Controller needs some data from the View?
    What I'm doing: using the reference of the View which I keep in the Controller
    f. Controller needs some data from the Model?
    What I'm doing: using the reference of the Model which I keep in the Controller
    g. One of FooModel, FooView or FooController needs data from one of BarModel, BarView or BarController?
    What I'm doing: thinking of jumping from the highest building...
    


  • 有关如何正确实现MVC的任何提示?我应该在模型或控制器中处理大量数据吗?

  • Any hints on how to know if I implemented MVC correctly? Should I process massive data in Model or Controller?

    我也在使用DAO,我正在做的是:我的模型有

    I'm also using a DAO, what I'm doing is: my model has a


    ArrayList MyModel load()

    ArrayList MyModel load()

    创建的方法DAO的一个实例并返回由DAO返回的模型的ArrayList,然后有时我在模型中处理这个模型的ArrayList,有时我允许Controller处理它。这是一个好习惯还是有更好的方法?按进程我的意思是:遍历ArrayList并从模型中获取数据。

    method which creates an instance of the DAO and returns the ArrayList of Models returned by the DAO, and then sometimes I process this ArrayList of Models in the Model and sometimes I allow the Controller to process it. Is this a good practice or is there a better way? By Process I mean: iterate through the ArrayList and get the data from the models.

    我有一个PasswordCheck jDialog来限制对某些视图的访问。如何在MVC方面重用它,以便我可以使用相同的PasswordCheck对话框来允许/限制对不同视图的访问,而不会在代码中造成混乱?

    I Have a PasswordCheck jDialog to restrict access to some Views. How can I reuse it in terms of MVC, so that I can use the same PasswordCheck dialog for allowing/restricting access to different Views without doing a mess in the code?

    任何其他提示,提示,想法,建议?

    Any other tips, hints, ideas, suggestions?

    上下文:
    我需要开发Java Swing MVC软件在短时间内,虽然默认情况下,我不是Java开发人员,也不是那么习惯于实现MVC模式,特别是在Java中(我得到了这个想法,但有时它缺乏实现类之间关系的知识)。
    应用程序基本上是本地/在线文件的监视器,主框架中有一个JTable来显示这些数据。我正在使用新的WatchService API来跟踪本地文件并使用DAO将它们的信息保存在h2数据库中,并在主框架jtable中重新加载这些数据。我还必须通知用户有关新文件(我正在使用TrayIcon)。对于在线文件监控/上传/下载我正在使用HttpsUrlConnection和json。它也可能允许设置自定义。

    Context: I'm required to develop a Java Swing MVC software in a short time, although by default I'm not a Java developer and not so used to implement the MVC pattern, specially in Java (I get the idea but sometimes it lacks me knowledge to implement the relationship between classes). The applications is basically a monitor for local/online files with a JTable in the main frame to show this data. I'm using the new WatchService API to keep track of the local files and saving their info in a h2 database using a DAO, and them reload this data in the main frame jtable. I must also notify the user about new files (which for I'm using TrayIcon). For the online files monitoring/uploading/downloading I'm using HttpsUrlConnection and json. It may also allow Settings customization.

    提前感谢您的时间和帮助。

    Thanks in advance for you time and help.

    推荐答案

    查看 Sun的(甲骨文)建议

    作为一种简化,您可以让每个组件(模型,视图,控制器)注册一个顶级应用程序组件,以提供单一参考点比每个组件(您的A或B)之间的单个引用。我引用的文章提供了推拉设计的想法;我推荐push作为一种更流行的现代方法。披露:我有使用Java和MVC的经验但不具备Swing本身的MVC。

    As one simplification, you could have each component (model, view, controller) register with a top-level application component to provide a single point of reference rather than individual references between each component (your A or B). The article I cite provides ideas for push and pull design; I'd recommend push as a more popular modern approach. Disclosure: I have experience with Java and MVC but not MVC in Swing per se.


    我应该在哪里实例化View,Model和控制器是
    的SettingsFrame?

    where should I instantiate the View, the Model and the Controller of the SettingsFrame?

    当然,是的,还是在顶层应用程序组件中。

    Sure, yes, or in a top-level application component.


    我应该如何处理(显然)缺少一个
    或两个MVC腿(模型或视图或控制器)的更多特定功能?

    how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)?

    我会将仅GUI作为您自己的GUI库。纯粹的算法/服务作为服务库。

    I would implement GUI-only pieces as your own GUI library. And purely algorithm/service pieces as a services library.


    我应该在模型或控制器中处理大量数据吗?

    Should I process massive data in Model or Controller?

    数据处理算法非常适合控制器甚至服务库;除了可能的数据类型转换或验证之外,你的模型不应该进行太多处理。

    Data processing algorithms would fit nicely in a controller or even service library; your model should not do much processing at all beyond possibly data type conversion or validation.


    如何通过设置正确保存和使用对象整个申请?

    How to correctly keep and use an object with settings through the whole application?

    请参阅我的注册说明;单身可能是合适的。

    See my note on registration; a singleton may be appropriate.

    这篇关于如何用Swing在Java中正确实现MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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