有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

问题:

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

    (A) 主要内容:

    MyModel model
    MyView view(model)
    

    MyView:

    MyController(this, model)
    

    (B)
    主要内容:

    MyModel model
    MyView View
    MyController controller(view, model)
    
  2. 当我在大型机中单击jbutton1时,我需要它打开设置框架以编辑设置。我应该在哪里实例化SettingsFrame的视图、模型和控制器?在大型机控制器中

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

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

    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...
    
  6. 有没有关于如何知道我是否正确实现了MVC的提示?我应该在模型或控制器中处理大量数据吗

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

    ArrayList MyModel load()

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

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

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

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

提前感谢您的时间和帮助


共 (1) 个答案

  1. # 1 楼答案

    看看Sun's (Oracle's) suggestions

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

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

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

    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库。以及作为服务库的纯算法/服务片段

    Should I process massive data in Model or Controller?

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

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

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