如何将常规类转换为MVC?

时间:2016-01-21 22:51:17

标签: java model-view-controller

我正在研究基于我将提供的代码的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);

}
}

2 个答案:

答案 0 :(得分:1)

视图仅控制视觉效果,模型仅控制数据访问,而控制器负责粘合两者的逻辑。 您可以将该类拆分为3个类,以使其成为MVC。

所有Jpanel都将位于View中,在Model中你将获得所有值,比如将字符串设置为hello等,而控制器需要与Model和View进行交互。

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());
}

稍微暗示让你前进。

答案 1 :(得分: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);
        }

    }

}