In this class, my teacher is talking about MVC
Model-View-Controller
I think it’s not easy to accept MVC when you touch it at the first time.Luckily,I have know a little about it.
Before this class, I think that MCV is design for B/S Framework~ Actually I am wrong.
Now I try to intro MVC.
Controller: To decide what time to create a View to people and send message to the View or the Model.
Model:All kinds of entity-data that will be used for Controller and View.
View:To deal with the Model Data then create the UI Interface for people.
Here is an example
A Main Class (MVCCalculator) and create 3 Class named (CalculatorController、CalculatorModel、CalculatorView)
Result:
There are 4 lines of controls.
So I create ListArray<T> to receive the controls
MVCCalculator:
public class MVCCalculator {
public static void main(String[] args) {
CalculatorView theView = new CalculatorView();
CalculatorModel theModel = new CalculatorModel();
CalculatorController theController = new CalculatorController(theView,theModel);
theView.setVisible(true);
}
}
CalculatorController:
public class CalculatorController {
private CalculatorView theView;
private CalculatorModel theModel;
public CalculatorController(CalculatorView theView, CalculatorModel theModel) {
this.theView = theView;
this.theModel = theModel;
// Tell the View that when ever the calculate button
// is clicked to execute the actionPerformed method
// in the CalculateListener inner class
for(int i = 0;i < 4;i++)
{
this.theView.addCalculateListener(i, new CalculateListener(i));
}
}
class CalculateListener implements ActionListener{
int index = -1;
private CalculateListener(int i) {
index = i;
}
public void actionPerformed(ActionEvent e) {
int firstNumber = 0;
int secondNumber = 0;
// Surround interactions with the view with
// a try block in case numbers weren't
// properly entered
try{
firstNumber = theView.getFirstNumber(index);
secondNumber = theView.getSecondNumber(index);
if(index == 0)
{
theModel.addTwoNumbers(firstNumber, secondNumber);
}
else if(index == 1)
{
theModel.subTwoNumbers(firstNumber, secondNumber);
}
else if(index == 2)
{
theModel.mulTwoNumbers(firstNumber, secondNumber);
}
else if(index == 3)
{
theModel.divTwoNumbers(firstNumber, secondNumber);
}
theView.setCalcSolution(index, theModel.getCalculationValue());
}
catch(NumberFormatException ex){
System.out.println(ex);
theView.displayErrorMessage("You Need to Enter 2 Integers");
}
}
}
}
CalculatorModel:
public class CalculatorModel {
// Holds the value of the sum of the numbers
// entered in the view
private int calculationValue;
public void addTwoNumbers(int firstNumber, int secondNumber){
calculationValue = firstNumber + secondNumber;
}
public void subTwoNumbers(int firstNumber, int secondNumber){
calculationValue = firstNumber - secondNumber;
}
public void mulTwoNumbers(int firstNumber, int secondNumber){
calculationValue = firstNumber * secondNumber;
}
public void divTwoNumbers(int firstNumber, int secondNumber){
calculationValue = firstNumber / secondNumber;
}
public int getCalculationValue(){
return calculationValue;
}
}
CalculatorView:
public class CalculatorView extends JFrame{
private ArrayList<JTextField> firstNumber = new ArrayList<JTextField>();
private ArrayList<JLabel> additionLabel = new ArrayList<JLabel>();
private ArrayList<JTextField> secondNumber = new ArrayList<JTextField>();
private ArrayList<JButton> calculateButton = new ArrayList<JButton>();
private ArrayList<JTextField> calcSolution = new ArrayList<JTextField>();
CalculatorView(){
// Sets up the view and adds the components
ArrayList<JPanel> calcPanel = new ArrayList<JPanel>();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 200);
this.setLayout(new GridLayout(4,1,0,0));
String[] str = {"+", "-", "*", "/"};
for(int i = 0;i < 4;i++)
{
firstNumber.add(new JTextField(10));
additionLabel.add(new JLabel(str[i]));
secondNumber.add(new JTextField(10));
calculateButton.add(new JButton("Calculate"));
calcSolution.add(new JTextField(10));
calcPanel.add(new JPanel());
calcPanel.get(i).add(firstNumber.get(i));
calcPanel.get(i).add(additionLabel.get(i));
calcPanel.get(i).add(secondNumber.get(i));
calcPanel.get(i).add(calculateButton.get(i));
calcPanel.get(i).add(calcSolution.get(i));
this.add(calcPanel.get(i),i);
}
// End of setting up the components --------
}
public int getFirstNumber(int i){
return Integer.parseInt(firstNumber.get(i).getText());
}
public int getSecondNumber(int i){
return Integer.parseInt(secondNumber.get(i).getText());
}
public int getCalcSolution(int i){
return Integer.parseInt(calcSolution.get(i).getText());
}
public void setCalcSolution(int i, int solution){
calcSolution.get(i).setText(Integer.toString(solution));
}
// If the calculateButton is clicked execute a method
// in the Controller named actionPerformed
void addCalculateListener(int i, ActionListener listenForCalcButton){
calculateButton.get(i).addActionListener(listenForCalcButton);
}
// Open a popup that contains the error message passed
void displayErrorMessage(String errorMessage) {
JOptionPane.showMessageDialog(this, errorMessage);
}
}