2016-01-21 76 views
1

我正在使用基于我将提供的代码的MVC。我遇到了问题,因为我对这个主题相当陌生。我能够表达观点,但是当谈到制作模型时,这对我来说有点复杂。我需要一些关于如何将以下代码转换为MVC的指导,以便我可以练习和学习。我已经呆了好几个小时了,我决定来这里寻求帮助。如何将普通类转换为MVC?

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

class SayHi extends JFrame implements MouseListener{ 
// components 
protected JLabel helloLabel = new JLabel("Hello"); 
protected JTextField userInputTextField = new JTextField(20); 
private JButton sayHiBtn = new JButton("Say Hi"); 


/** Constructor */ 
SayHi() { 
    //... Layout the components.  
    JPanel content = new JPanel(); 

    content.setLayout(new FlowLayout()); 
    content.add(new JLabel("Enter your name")); 
    content.add(userInputTextField); 
    content.add(sayHiBtn); 
    content.add(helloLabel); 



    // Add a mouse listener to the button 
    sayHiBtn.addMouseListener(this); 

    //... finalize layout 
    this.setContentPane(content); 
    this.pack(); 
    this.setTitle("Simple App - Not MVC"); 

    // The window closing event should probably be passed to the 
    // Controller in a real program, but this is a short example. 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

// Methods I am forced to implement because of the MouseListener 

public void mouseClicked(MouseEvent e) { 
    helloLabel.setText("Hello " + userInputTextField.getText()); 
} 

public void mouseEntered(MouseEvent e){ 
} 

public void mouseExited(MouseEvent e){ 
} 

public void mousePressed(MouseEvent e){ 
} 

public void mouseReleased(MouseEvent e){ 
} 

public static void main(String[] args){ 

    SayHi s = new SayHi(); 
    s.setVisible(true); 

} 
} 
+1

您不会将单个“常规”类转换为MVC。您至少需要3个课程(模型,视图和控制器)。不过,在没有框架的情况下创建模型可能只是简单地创建一个'ArrayList '或类似的东西。 – ChiefTwoPencils

回答

1

View只控制视觉效果,Model只控制数据访问,Controller负责粘贴两者的逻辑。 您可以将该类分成3个类,以使其成为MVC。

所有的Jpanel都将位于视图中,在模型中您将获得所有值,例如设置字符串为hello等,而控制器需要与模型和视图交互。

private SayHiModel model; 
private SayHiView view; 
SayHiController(SayHiModel model, SayHiView view) { 
    this.model = model; 
    this.view = view; 
    this.model.setValue(model.INITIAL_VALUE); 
    view.totalTextField.setText(model.getValue()); 
    //... Add listeners to the view. 
    view.addMultiplyListener(new MultiplyListener()); 
    view.addClearListener(new ClearListener()); 
} 

只是一点点让你去。

0

没有太多的模型可以使用。

以下是您的示例代码,其中包含一个视图类,一个模型类和一个控制器类。 SayHi类是视图类。 SayHiModel类是模型类。 SayHiListener类是控制器类。

package com.ggl.testing; 

import java.awt.FlowLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class SayHi implements Runnable { 

    private JFrame frame; 

    private JLabel helloLabel; 

    private JTextField userInputTextField; 

    private SayHiModel model; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new SayHi()); 
    } 

    public SayHi() { 
     this.model = new SayHiModel(); 
    } 

    @Override 
    public void run() { 
     // ... Layout the components. 
     JPanel content = new JPanel(); 
     content.setLayout(new FlowLayout()); 

     content.add(new JLabel("Enter your name: ")); 

     userInputTextField = new JTextField(20); 
     content.add(userInputTextField); 

     JButton sayHiBtn = new JButton("Say Hi"); 
     // Add a mouse listener to the button 
     sayHiBtn.addMouseListener(new SayHiListener(this, model)); 
     content.add(sayHiBtn); 

     helloLabel = new JLabel("Hello"); 
     content.add(helloLabel); 

     // ... finalize layout 
     frame = new JFrame("MVC App"); 

     // The window closing event should probably be passed to the 
     // Controller in a real program, but this is a short example. 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.setContentPane(content); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public JFrame getFrame() { 
     return frame; 
    } 

    public void setHelloLabel(String name) { 
     helloLabel.setText("Hello " + name); 
    } 

    public String getName() { 
     return userInputTextField.getText().trim(); 
    } 

    public class SayHiModel { 
     private String name; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

    } 

    public class SayHiListener extends MouseAdapter { 
     private SayHi sayHi; 

     private SayHiModel sayHiModel; 

     public SayHiListener(SayHi sayHi, SayHiModel sayHiModel) { 
      this.sayHi = sayHi; 
      this.sayHiModel = sayHiModel; 
     } 

     @Override 
     public void mouseClicked(MouseEvent e) { 
      sayHiModel.setName(sayHi.getName()); 
      sayHi.setHelloLabel(sayHiModel.getName()); 

      JFrame frame = sayHi.getFrame(); 
      frame.setVisible(false); 
      frame.pack(); 
      frame.setVisible(true); 
     } 

    } 

}