JAVA

(1)
Model
Our simple model just has a one private variable “x”. This variable is initialized to zero by default constructor Model(), or to custom value by constructor Model(int x) . Methods incX() and getX() give access to “x”. Method incX() performs some operations: it increments “x”, while method getX just return “x”. Note that Model don’t know anything about View and Controller.
package mvc.models;

public class Model {

private int x;

public Model(){
x = 0;
}

public Model(int x){
this.x = x;
}

public void incX(){
x++;
}

public int getX(){
return x;
}
}
(2)
View
The View class uses Swing library to create window and place label and button on it. Public method setText(String text) allows us to set text label, method getButton() returns button reference. Note that View don’t know anything about Model and Controller.
package mvc.views;

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

public class View {

private JFrame frame;
private JLabel label;
private JButton button;
public View(String text){
frame = new JFrame(“View”);
frame.getContentPane().setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);

label = new JLabel(text);
frame.getContentPane().add(label, BorderLayout.CENTER);

button = new JButton(“Button”);
frame.getContentPane().add(button, BorderLayout.SOUTH);
}

public JButton getButton(){
return button;
}

public void setText(String text){
label.setText(text);
}

}
(3)

Controller
The Controller class keeps references to Model and View classes. So Controller sees Model and View interfaces. Controller provides interaction between Model and View. Method control() gets reference to view’s button and assign actionListener to it. In actionListener’s method actionPerformed() method linkBtnAndLabel() is called. In linkBtnAndLabel() model’s variable “x” increments and than “x” sends to view’s label to display changes.
package mvc.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import mvc.models.*;
import mvc.views.*;

public class Controller {

private Model model;
private View view;
private ActionListener actionListener;

public Controller(Model model, View view){
this.model = model;
this.view = view;

}

public void contol(){
actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
linkBtnAndLabel();
}
};
view.getButton().addActionListener(actionListener);
}

private void linkBtnAndLabel(){
model.incX();
view.setText(Integer.toString(model.getX()));
}
}
(4)
Main class
The Main class creates objects of Model, View and Controller classes, initializes Controller by Model and View and call Controller’s method control().

code for main class:

package mvc;

import javax.swing.SwingUtilities;

import mvc.models.*;
import mvc.views.*;
import mvc.controllers.*;

public class Main
{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model = new Model(0);
View view = new View(“-“);
Controller controller = new Controller(model,view);
controller.contol();
}
});
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s