Swing MVC模型

3

我尝试使用Swing创建一个MVC应用程序。但是,我对实现方式和事物应该如何处理感到困惑。我的意思是:

  1. I have the Gui which is the view all the logic send to a class named controller and I have a model where I have model properties.(I have read MVC is like that)

  2. I create some Random codes in view with input of how many codes I want and transfer this with ActionListener to a Class Named Controller. The Random codes generated with a button on the controller class in a method . The random codes are generated and then i want to save them on a database. I am confused how to save the generated codes on the database. Should i create a method in the Class Named Controller so that i can save them from there? Or another different class with save update find.......... methods? If Yes then why I have create the Model class with Model properties? And how can I use the Model class. what is only left to understand how to use the Model class if I have to use it or if I just have to have this class so that is there. What is the use of Model class if it's only to be there with the properties and do save some where else? What approach is usually used so that I am ok with MVC pattern? Am I confused? Any help I forget to tell that I use Hibernate. thanks ps. I have also read this http://java.sun.com/products/jfc/tsc/articles/architecture/ but i didn't understand it.

    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?
    

现在好了吗? 谢谢


一个 sscce 将会有所帮助。 - gontard
本站无法提供完整的教程。请编辑您的问题,包含一个 sscce,展示您尝试过什么以及遇到了哪些问题。 - trashgod
2个回答

5
让我为您解释一下... Model - 业务逻辑和数据 View - 模型输出的展示 Controller - 处理操作的地方。
Java中的Swing基于MVC,也被称为PLAF(可插入外观)。
使用这种MVC体系结构的优点是,可以保持相同的模型并不断更改视图。
例如:
拥有一个运行计算器程序的模型。
现在将此模型并使用Swing或JSP反映输出,一个用于桌面,另一个用于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

+1 用于概述。还可以查看此相关答案:https://dev59.com/k-o6XIcBkEYKwwoYTy8d#3072979。 - trashgod
@ Kumar Vivek Mitra 控制器告诉模型它需要什么。这意味着什么?控制器告诉模型什么。我的模型是一个带有get/set的类。 - user1577708
@user1577708 一个类应该是内聚的。这意味着类名应该根据其方法来反映。 这指出了方法对于类的重要性,就像实例变量对于对象的重要性一样。因此,您的作为模型的类必须具有程序所需的逻辑。一个只有getter-setter的类不是一个模型。 - Kumar Vivek Mitra
@ Kumar Vivek Mitra 这意味着我必须在模型内实现模型逻辑吗?例如,如果我要生成一个随机数,我会在模型类中执行这个操作?然后我会从模型类创建并保存它?否则,如果我想将其显示到我的视图中,我会将其发送到控制器或视图?我需要为所有这些制作一个示例吗? - user1577708
一个特定的_model_可能会导出一个不可变的java.lang.Number具体子类实例。一般来说,一个_model_代表着_view_呈现给用户的数据。 - trashgod
显示剩余2条评论

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接