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

查看:34
本文介绍了如何使用 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,没有框架.第一个 Window 是 JFrame,所有其他窗口都是 modal=true 的 JDialogs.

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. 如何正确实现 MVC 模式的 Swing?从下面的想法哪个更好:(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?

(一)主要:

MyModel model
MyView view(model)

我的视图:

MyController(this, model)

(乙)
主要:

MyModel model
MyView View
MyController controller(view, model)

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

  • 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 的任何提示?我应该在 Model 还是 Controller 中处理海量数据?

  • 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,有时我允许控制器处理它.这是一个好的做法还是有更好的方法?流程我的意思是:遍历 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 (Oracle) 的建议.

    作为一种简化,您可以让每个组件(模型、视图、控制器)注册到一个顶级应用程序组件,以提供单个引用点,而不是每个组件(您的 A 或 B)之间的单独引用.我引用的文章提供了推拉式设计的想法;我建议将推送作为一种更流行的现代方法.披露:我有 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.

    我应该在哪里实例化视图、模型和控制器设置框架?

    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.

    我应该在 Model 还是 Controller 中处理海量数据?

    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天全站免登陆