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);
}
}
相关推荐
Java Swing MVC使用
mvc的详细设计模式,里面还有mvc的设计模式图及例子,对java想有更深层研究的人必看之选
Java版本mvc模式购物车 技术:jsp + servlet + javabean + html jsp购物车,轻松构建网络商城系统网站。 本版本带有分布功能。 如果有问题可以在线交流。
java web MVC的实现(JSP+Servlet+javaBean) 简单的MVC实现登录功能,使用初学者
Java用MVC模式+Servlet+Mysql做的论坛
mysql+java(MVC)+bbs论坛完整实例mysql+java(MVC)+bbs论坛完整实例
java MVC模式范例 MVC DAO
控件连接数据库,struts2.0 spring hibernate,MVC模式连接数据库,异步调用
简单的java mvc模式,包含分页功能
一个关于java Swing MVC 的开发实例,
JAVA MVC +实例
这个项目是基于MVC模型而携程的案例,案例使用JAVA语言编写的
java 自定义MVC框架实战项目
很好的JAVA的MVC框架应用实例
Java MVC 扫雷 带计时器,自动打开周边无雷区域 右键插旗标记等功能。
用MVC的架构实现简单的猜山羊小游戏 使用java作为编程语言 用图表和文字作为选中概率的显示方式 分享此程序希望能给大家带来帮助!
用java写的网站,采用mvc模式.应用了mysql数据库 是一个很好的学习代码
java mvc的例子代码很有用,很不错 值得学习和研究
单例模式、MVC、工厂模式、抽象工厂模式、代理模式详解
java Web MVC模式下的数据库连接项目