2012-08-24 54 views
3

我尝试使用Swing创建一个MVC应用程序。我对实施以及应该如何处理感到困惑。我的意思是:swing Mvc模型

  1. 我有桂这是所有逻辑发送到类名为控制器的观点,我有一个模型,在那里我有模特属性(我已阅读MVC就是这样)

  2. 我创建了一些随机代码,并输入了我想要的代码数,并将其与ActionListener一起传输给一个Class Named Controller。随机代码通过方法中的控制器类上的按钮生成。 生成随机代码,然后我想将它们保存在数据库中。 我很困惑如何将生成的代码保存在数据库中。 我应该在类命名控制器中创建一个方法,以便我可以从那里保存它们?或另一个不同的类与保存更新找到..........方法?如果是,那么为什么我使用Model属性创建Model类?我怎样才能使用Model类。 如果我必须使用它,或者如果我只需要让这个类在那里,那么我们还需要了解如何使用Model类。 Model类的用途是什么?如果它只是与属性在一起,并且保存在其他地方? 通常使用什么方法,以便我可以使用MVC模式?我困惑吗?任何帮助 我忘记告诉我使用Hibernate。感谢 ps。我也读过这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? 
    

是不是好些了吗? 感谢

+0

a [sscce](http://sscce.org/)将有所帮助。 – gontard

+0

完整的教程超出了本网站的范围。请编辑您的问题以包含[sscce](http://sscce.org/),其中显示您尝试过的内容以及遇到问题的位置。 – trashgod

回答

5

让我打破了这个给你....

Model - 业务逻辑和数据

View - 模型的输出显示

Controller - 在其中行动已经完成。

Swing在java中是基于MVC。它也被称为PLAF(可插入的外观和感觉)

使用此MVC架构的优点是,您可以保留相同的模型,并不断更改视图。

如:

有它运行您的计算器程序的模型。

现在采取这种模式,然后使用Swing或JSP来反映输出,一个用于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 
+0

+1。另请参阅此相关的[答案](http://stackoverflow.com/a/3072979/230513)。 – trashgod

+0

@ Kumar Vivek Mitra Controller告诉Model关于它。这意味着什么?Controller告诉Model.My Model是带get/set的类。 – user1577708

+0

@ user1577708'一个类应该具有内聚性。这意味着类名应该根据它的方法反映出来。这指出方法对于类很重要,作为对象的实例变量。所以你的类作为模型工作,必须有你的程序应该做的逻辑。只有getter-setter的类不是模型 –