2016-01-21 77 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); 
     } 

    } 

}