Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

MVC Java Tutorial

Derek Banas13:16 597.170 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 85 Zeilen
Herunterladen
  1. 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
  2. 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
  3. 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
  4. interface coordinator cuz basically all it does is separates all data and calculations into the model class and separates the interface into the view
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. 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
  78. 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
  79. 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
  80. 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
  81. 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
  82. 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
  83. 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
  84. 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
  85. if not leave any questions or comments below otherwise till next time

Zum Nachlesen