如何使用Swing在Java中正确实现MVC?

如何使用Swing在Java中正确实现MVC?,java,swing,model-view-controller,user-interface,jframe,Java,Swing,Model View Controller,User Interface,Jframe,如果您想了解更多详细信息,请让我知道,或参考此问题的最后几行。我已经读了很多书,我觉得我正在把一些简单的东西变成一些复杂的东西,我仍然被困在这里和那里,所以也许你可以在这些非常具体的点上帮助我 我使用的是Netbeans IDE 7和JDK 7,没有框架。第一个窗口是JFrame,所有其他窗口都是modal=true的JDialogs 问题: 如何使用swing正确实现MVC模式? 从下面的观点来看,哪一个更好:(A)还是(B)?或者另一个。。。为什么更好 (A) 主要内容: MyView: M

如果您想了解更多详细信息,请让我知道,或参考此问题的最后几行。我已经读了很多书,我觉得我正在把一些简单的东西变成一些复杂的东西,我仍然被困在这里和那里,所以也许你可以在这些非常具体的点上帮助我

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

问题:

  • 如何使用swing正确实现MVC模式? 从下面的观点来看,哪一个更好:(A)还是(B)?或者另一个。。。为什么更好

    (A) 主要内容:

    MyView:

    MyController(this, model)
    
    (B)
    主要内容:

  • 当我在大型机中单击jbutton1时,我需要它打开设置框架以编辑设置。我应该在哪里实例化SettingsFrame的视图、模型和控制器?在大型机控制器中

  • 在MVC组织和实现方面,我应该如何处理(显然)缺少一个或两个MVC“支柱”(模型、视图或控制器)的更具体的特性?我应该为他们创建空类吗

    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
    
  • 如何在整个应用程序中正确保留和使用具有设置的对象?我将需要它的信息在不同的地方(视图,模型,控制器),但它可能会被用户在运行时更改)。这是一个好主意,使这个模型成为一个单身

  • 在以下情况下,我应该怎么做:

    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的提示?我应该在模型或控制器中处理大量数据吗

  • 我也在使用DAO,我在做的是:我的模型有一个

    ArrayList MyModel load()

    方法,该方法创建DAO的实例并返回DAO返回的模型的ArrayList,然后有时我在模型中处理该模型的ArrayList,有时我允许控制器处理它。这是一个好的做法还是有更好的方法?我所说的过程是指:遍历ArrayList并从模型中获取数据

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

  • 还有其他提示、提示、想法和建议吗

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

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

    请查看

    作为一种简化,您可以让每个组件(模型、视图、控制器)向顶级应用程序组件注册,以提供单个参考点,而不是每个组件(a或B)之间的单个参考。我引用的文章为推拉设计提供了思路;我建议将推送作为一种更流行的现代方法。披露:我有Java和MVC的经验,但在Swing中没有MVC本身的经验

    我应该在何处实例化的视图、模型和控制器 设置框架

    当然,是的,或者在顶级应用程序组件中

    我应该如何处理(显然)缺少的更具体的功能 还是两个MVC“腿”(模型、视图或控制器)

    我将只实现GUI片段作为您自己的GUI库。以及作为服务库的纯算法/服务片段

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

    数据处理算法很适合控制器甚至服务库;除了数据类型转换或验证之外,您的模型不应该做太多处理

    如何在整个应用程序中正确保留和使用具有设置的对象


    见我关于注册的说明;一个单身汉可能是合适的。

    +1,我喜欢这个想法,就像我喜欢这个链接一样,这也会帮助我的努力:-)+1对这个问题也很好地解释,我是第一个对这两个都投赞成票的人:-)谢谢你的回答。你说的“看我的注册说明”是什么意思?我以前看过suns的建议,但既然是你推荐的,我会更仔细地再读一遍,试着解决我的疑问。我也投了赞成票。+1也看到了这一点,它遵循了一个类似于所引用的图表。@dcr在我关于注册的说明中,我的意思是你可能想注册“每个组件(模型、视图、控制器)……一个顶级应用程序组件,以提供一个单一的参考点。”因此,只共享一个顶级应用程序对象;可以通过应用程序访问设置或其他常用数据。优点包括复制的引用更少,并且能够在运行时更轻松地交换实现;甚至可以使用Spring或其他IOC机制。@trashgood非常感谢您提供的示例。在搜索MVC时,我没有遇到这个示例。我会尽快仔细阅读+1.
    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
    
    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...