gpt4 book ai didi

java - Swing Mvc 模型

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:30 24 4
gpt4 key购买 nike

我尝试用 Swing 创建一个 MVC 应用程序。我对实现和事情应该如何感到困惑。我的意思是:

  1. 我有一个 Gui,它是将所有逻辑发送到一个名为 controller 的类的 View ,我有一个模型,其中我有模型属性。(我读过 MVC 是这样的)

  2. 我根据输入的我想要的代码数量创建了一些随机代码,并使用 ActionListener 将其传输到类命名 Controller 。在方法中使用 Controller 类上的按钮生成的随机代码。生成随机代码,然后我想将它们保存在数据库中。我很困惑如何将生成的代码保存在数据库中。我应该在 Class Named Controller 中创建一个方法,以便我可以从那里保存它们吗?或另一个具有保存更新查找.........方法的不同类?如果是,那么为什么我要创建具有模型属性的模型类?以及如何使用 Model 类。如果我必须使用它,或者如果我只需要拥有这个类,那么剩下的就是了解如何使用模型类。如果它只是与属性一起存在并在其他地方保存一些,那么 Model 类的用途是什么?通常使用什么方法让我可以接受 MVC 模式?我糊涂了吗?任何帮助我忘了告诉我我使用 Hibernate。谢谢附言。我也读过这个http://java.sun.com/products/jfc/tsc/articles/architecture/但我不明白。

    public class code(){// this is the Model
    private int i;
    public void setter(int i){
    this.i=i;
    }

    public int getter(){
    return i;
    }

    public String generateStringInt() {
    return new BigInteger(190, random).toString(32);
    }

    // what ever i want to create with the int i variable i will do it on this class?
    ///then i will pass it on the controller to sent it on the view
    //OR save if i want to save it.?Is this way of thinking right?
    //Or is there any other way to do it ?
    /// if i have a Button and press it from the View it will generate this method?or
    // i have to do it else?
    public String generateStringInt() {
    return new BigInteger(190, random).toString(32);
    }

    }

    //then if i want to save i can just do it like
    //session.save(object) or is there any other way?

现在好些了吗?谢谢

最佳答案

让我为你打破这个....

模型 - 业务逻辑和数据

View - 模型输出的显示

Controller - 在其上完成操作。

java中的

Swing是基于MVC的。它也被称为 PLAF(可插入外观)

使用这种 MVC 架构的优势在于,您可以保持相同的模型并不断更改 View 。

例如:

拥有一个运行您的计算器程序的模型。

现在采用此模型,然后使用 Swing 或 JSP 来反射(reflect)输出,一个用于 desktop,另一个用于 web

在 Swing 应用程序的情况下,MVC 的顺序是这样的......

Action is done on the Controller
Controller tells the Model about it
Model make necessary changes, according to the Action
Controller informs the change in state of Model to the View
View will update itself.

在 Web 应用程序的情况下,MVC 的顺序是这样的....

Action is done on the Controller
Controller tells the Model about it
Model make necessary changes, according to the Action
Now Controller informs View and Also make the Changes reflect in View

关于java - Swing Mvc 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com