Java07 MVC

Inthisclass,myteacheristalkingaboutMVC

Model-View-Controller

Ithinkit’snoteasytoacceptMVCwhenyoutouchitatthefirsttime.Luckily,Ihaveknow alittleaboutit.

Beforethisclass,IthinkthatMCVisdesignforB/SFramework~ActuallyIamwrong.

NowItrytointroMVC.

Controller:TodecidewhattimetocreateaViewtopeopleandsendmessagetotheViewortheModel.

Model:Allkindsofentity-datathatwillbeusedforControllerandView.

View:TodealwiththeModelDatathencreatetheUIInterfaceforpeople.

Hereisanexample

AMainClass(MVCCalculator)andcreate3Classnamed(CalculatorController、CalculatorModel、CalculatorView)

Result:

Thereare4linesofcontrols.

SoIcreateListArray<T>toreceivethecontrols

MVCCalculator:

publicclassMVCCalculator{

publicstaticvoidmain(String[]args){

CalculatorViewtheView=newCalculatorView();

CalculatorModeltheModel=newCalculatorModel();

CalculatorControllertheController=newCalculatorController(theView,theModel);

theView.setVisible(true);

}

}

CalculatorController:

publicclassCalculatorController{

privateCalculatorViewtheView;

privateCalculatorModeltheModel;

publicCalculatorController(CalculatorViewtheView,CalculatorModeltheModel){

this.theView=theView;

this.theModel=theModel;

//TelltheViewthatwheneverthecalculatebutton

//isclickedtoexecutetheactionPerformedmethod

//intheCalculateListenerinnerclass

for(inti=0;i<4;i++)

{

this.theView.addCalculateListener(i,newCalculateListener(i));

}

}

classCalculateListenerimplementsActionListener{

intindex=-1;

privateCalculateListener(inti){

index=i;

}

publicvoidactionPerformed(ActionEvente){

intfirstNumber=0;

intsecondNumber=0;

//Surroundinteractionswiththeviewwith

//atryblockincasenumbersweren’t

//properlyentered

try{

firstNumber=theView.getFirstNumber(index);

secondNumber=theView.getSecondNumber(index);

if(index==0)

{

theModel.addTwoNumbers(firstNumber,secondNumber);

}

elseif(index==1)

{

theModel.subTwoNumbers(firstNumber,secondNumber);

}

elseif(index==2)

{

theModel.mulTwoNumbers(firstNumber,secondNumber);

}

elseif(index==3)

{

theModel.divTwoNumbers(firstNumber,secondNumber);

}

theView.setCalcSolution(index,theModel.getCalculationValue());

}

catch(NumberFormatExceptionex){

System.out.println(ex);

theView.displayErrorMessage("YouNeedtoEnter2Integers");

}

}

}

}

CalculatorModel:

publicclassCalculatorModel{

//Holdsthevalueofthesumofthenumbers

//enteredintheview

privateintcalculationValue;

publicvoidaddTwoNumbers(intfirstNumber,intsecondNumber){

calculationValue=firstNumber+secondNumber;

}

publicvoidsubTwoNumbers(intfirstNumber,intsecondNumber){

calculationValue=firstNumber-secondNumber;

}

publicvoidmulTwoNumbers(intfirstNumber,intsecondNumber){

calculationValue=firstNumber*secondNumber;

}

publicvoiddivTwoNumbers(intfirstNumber,intsecondNumber){

calculationValue=firstNumber/secondNumber;

}

publicintgetCalculationValue(){

returncalculationValue;

}

}

CalculatorView:

publicclassCalculatorViewextendsJFrame{

privateArrayList<JTextField>firstNumber=newArrayList<JTextField>();

privateArrayList<JLabel>additionLabel=newArrayList<JLabel>();

privateArrayList<JTextField>secondNumber=newArrayList<JTextField>();

privateArrayList<JButton>calculateButton=newArrayList<JButton>();

privateArrayList<JTextField>calcSolution=newArrayList<JTextField>();

CalculatorView(){

//Setsuptheviewandaddsthecomponents

ArrayList<JPanel>calcPanel=newArrayList<JPanel>();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(600,200);

this.setLayout(newGridLayout(4,1,0,0));

String[]str={"+","-","*","/"};

for(inti=0;i<4;i++)

{

firstNumber.add(newJTextField(10));

additionLabel.add(newJLabel(str[i]));

secondNumber.add(newJTextField(10));

calculateButton.add(newJButton("Calculate"));

calcSolution.add(newJTextField(10));

calcPanel.add(newJPanel());

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

}

//Endofsettingupthecomponents——–

}

publicintgetFirstNumber(inti){

returnInteger.parseInt(firstNumber.get(i).getText());

}

publicintgetSecondNumber(inti){

returnInteger.parseInt(secondNumber.get(i).getText());

}

publicintgetCalcSolution(inti){

returnInteger.parseInt(calcSolution.get(i).getText());

}

publicvoidsetCalcSolution(inti,intsolution){

calcSolution.get(i).setText(Integer.toString(solution));

}

//IfthecalculateButtonisclickedexecuteamethod

//intheControllernamedactionPerformed

voidaddCalculateListener(inti,ActionListenerlistenForCalcButton){

calculateButton.get(i).addActionListener(listenForCalcButton);

}

//Openapopupthatcontainstheerrormessagepassed

voiddisplayErrorMessage(StringerrorMessage) {

JOptionPane.showMessageDialog(this,errorMessage);

}

}

伟人所达到并保持着的高处,并不是一飞就到的,

Java07 MVC

相关文章:

你感兴趣的文章:

标签云: