Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).
MVC Java Tutorial
Das Wichtigste aus dem Video
Tipp auf eine Zeit – das Video springt genau dorthin.
Transkriptautomatisch erstellt · 85 Zeilen
- well hello Internet and welcome to my MVC Java tutorial my goal here is to teach you completely how to use the model view controller it is extremely
- simple if you have ever had any problem with it in the past those problems are going to be over by the end of this tutorial so what is the MVC I think the
- reason why people get confused about it is the name model view controller kind of confusing not really though you could instead refer to it as the data
- interface coordinator cuz basically all it does is separates all data and calculations into the model class and separates the interface into the view
- class and then the controller class is going to coordinate all interactions between the view and the model that is all it is and I'm just going to jump
- right into the code and show you how it works so let's get into it so here we are got my Eclipse open and I'm going to create the model first because it is the
- simplest part of this whole entire thing and the model is just simply going to hold all the data and perform all the calculations needed and that is it it
- won't even know the view exists so we're just going to go public class calculator model and all the code is available in a link underneath of the video you should
- get it because it's heavily commented and it will help you understand and of course it is free so what I'm going to do here this program is just going to
- add two values and display them on the screen and the final value is going to be calculation value it's just going to sum two numbers put them on a screen
- that's it so then I need to provide all the methods that are going to be needed to perform these calculations on this data so I'm going to create a method
- called add two numbers going to get an INT named first number and another one second number and guess what it's going to add them that's all it's going to do
- and it's going to store them in calculation value first number and this might seem very simplistic but I promise you this is the basics of an MVC and it
- can be applied in any way and this program could be definitely elaborated on and turned into a full functioning calculator if you'd like or whatever you
- want to do with it and then we have to provide a way to get my calculation value return calculation value and that is it there's the model it's done what
- does it do it contains the data which is just calculation value it performs the method that is needed and provides access to the data that is all the model
- does in all mvc's so now let's get into the view this is probably the most complicated part of it calculator view is the name and the only reason it's
- complicated is the interface part that's it the actual code that is part of the view is very simple so we're going to have the capability to perform and catch
- up events whenever the user is going to click on things and there that is and then we're going to import some swing components so that we'll be able to make
- ourselves an interface then I'm just going to come in here public class calculator view extends jframe and this is the Java stuff for making the
- interface doesn't really have anything to do with the model view controller or the view in this situation so we're going to allow the user to enter a
- number and let's make it 10 wide and then I'm just going to build the interface right here exactly the way that I would have it on the screen so
- there's going to be a block in there where they're going to be able to enter a number and then I'm going to put a addition sign new J label you could skip
- to the end of the tutorial if you want to see what the final thing's going to look like and then come back CU I haven't built it yet that's the reason
- why I can't show it to you all right then what makes sense after that is I'm going to have another block where they're going to be able to enter the
- second number easy enough and then I'm going to need to have a button that they can click on to perform the calculation J button and it's going to have the word
- calculate on it guess I could have put some and then finally JX field solution and like I said before the model doesn't know anything about the view it doesn't
- even know it exists and let's just make that tensu so there's that now that we have all our components defined that we're going to use and like I just said
- if that in any way confuses you don't worry about it this can be applied to any language as just Java specific now I'm going to set up the interface that's
- going to display on the screen it's equal to new J panel and then you can forget about this all together all this is going to do is whenever they click on
- the X to close the application it's going to make sure that the application is indeed closed and I'm just going to click exit on close and then let's just
- set a size I know I don't need that big of a area I'm going to say 600 pixels wide 200 pixels tall and then I'm going to add all of my components that I
- created above to the panel save myself some time and this is going to be second number and this guy is going to be actually I want this to be addition
- label is going to be the plus sign second number calculate button everything's just going to be displayed in the exact order that I have here calc
- solution and then I just want to add calc panel to the jframe which that's what calculator view is is the jframe and that is the end of set setting up
- components that is all I need to do that is what's going to be displayed I'm going to go and provide a way to get access to the first number and this is
- just going to return I'm going to have to turn this into an integer because it returns Whenever I Call get text it returns a string so I have to do that
- get text like that and there we are and then we're going to do pretty much the identical same exact thing for the second number and then we're going to
- perform pretty much exact exactly the same thing to get the calculation so calc solution and calc solution which is going to get me whatever is inside of
- the J text Fields see J text Fields going to have whatever is entered in there it's going to return that for me that is what G text does and then we
- have to provide a way to set the value of calc solution because the model is going to do that calculation for me but the model doesn't know about the
- existence of the view so the controller part is going to have to actually set this I'm just going to name that solution and if
- I want to set the value for that J text field I'm just going to go set text and then it's going to come over as an integer so I am going to convert it into
- a string like I said you can just get all this code and copy and paste it if you don't want to type it out and then this is probably the most complicated
- part of the whole thing I'm going to add a way so that whenever a butt Buton is clicked on my little calculator here that the controller is going to be
- alerted to that fact that's all action listener and I'm going to call this listen for Cal button so whenever the calc button is clicked on it's going to
- go and call the controller and say hey controller you handle this because I'm the view I don't do anything like that I'm going to go calculate button add
- action listener so it's going to listen for the action being the clicking of the C calculation button and of course it gives me an error because that doesn't
- exist anymore I haven't created the controller yet and then let's also say like what do we going to do if they go and click on the calculation button
- without entering any numbers or whatever though that's going to trigger an error so we want to be able to handle that and that is all the view does is handle what
- is being viewed and let's just have a little popup show up here so message dialogue this going to be my screen and then whatever the error message is that
- they pass over and that is the end of the view it's not complicated quite simple all it is is a view that is it doesn't do much of anything else so now
- let's go into the actual calculator controller and this guy is going to handle interactions between everything so I know I needed action events and all
- that stuff I'm just going to let that come up here on its own calculator controller and since it is the only thing that knows about the view I'm just
- going to name the the View and the model we're going to store that inside of it and then we're going to go and create a Constructor for it and then we're going
- to pass in calculator view the view and calculator model the model this makes much more sense if you just think of that as data I think this is the guy
- that's kind of confusing then just go this the view is equal to the view that was passed in here and then this the model is equal to the model that was
- passed in now what I got to do is tell the view that whenever the calculate button is clicked to execute the action performed method that is going to be in
- the in class called calculate listener below which I'm going to create here in a second so I'm just going to go this the view cuz I want to refer to this
- objects version of The View not the view that was passed in here and I'm going to go add calculate listener new calculate listener I'm going to create that
- Constructor here in a second so just copy that that and I'm just going to have this be an inter class calculate listener implements action listener so
- we can listen to what's going on with our view with our controller and it's going to give me a message that I need to implement my unimplemented methods
- which is going to be action performed which I mentioned before and this is where all of the interactions going to occur so I'm going to go first number
- second number and then because I don't know if the user of my view or my interface is going to enter both numbers before they hit calculate I have to
- surround everything with a tri block so that I can stop any errors from being triggered first number and to get the first number entered I'm just going to
- come in here and go get first number and that's going to get whatever is in the view and store it in there the value and then I'm going to do the same thing for
- second number and this is second number there we are then I have to go okay hey model you perform all my calculations remember the controller doesn't do
- anything except handle the interactions between the view and the guy that's going to perform our calculations and that's going to be first number second
- number pass those in there and the model is going to remember store that in calculation value right like that and then I'm going to go to the view all
- right well I want you to set my calculation solution there it is right there however I need to actually get it and it is stored in the model because
- that's where all the data is and I have to say get calculation value that is stored inside of there see this guy right here get calculation value that's
- all and we're pretty much done only thing I got to do is catch the error that's going to be triggered and the error that will be triggered if they do
- not enter a number into one part of my calculator and hit calculate the view display error message remember and what do I want to say I'll say you need to
- enter two into ERS and that is it so now I just need to go create all these objects and I'm going to do that inside of MVC calculator. Java so just come in
- here and go calculator view the view is equal to new calculator View and then what do I need to do I need to create the model just call that the model new
- calculator model and as you can see they don't know each other exists and then I need to create the controller that's going to allow those two to interact
- with each other other without knowing that either one exists so in this situation I'm going to have to pass in the model and the view so it knows what
- to work with the view and the model and then the final thing I need to do is actually make the view visible on the screen and I just set visibility to true
- and that does that for me and I have one little error add calculate listener ah the name's wrong that's all I need to call add calculation listener instead
- that sort of stuff that happens sometimes when I do this stuff out of my head Just Bounce in there and the airor goes away and then bounce over into
- calculator View and just change this to listener for calc button and there we are problem solved and execute and there you can see opens up
- on our screen and I can enter 1 + 2 and hit calculate and get three and change this to four and hit calculate and get six and whatever you would like and as
- you could see you could easily add a whole calculator interface on this or most anything you could ever want all you're going to need to do is add
- additional methods to the model or additional data and everything just works so that is my tutorial on MVC I hope it is now completely understandable
- if not leave any questions or comments below otherwise till next time