Java stopwatch ⏱️

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey also go on everybody its bro here hope you're doing well and in this video we're going to be creating a pretty sweet stopwatch in Java kind of looks like this so let's get into it before you reach the end of this video make sure to LIKE comment and subscribe so that we together can challenge and defeat the mighty YouTube algorithm alrighty everyone let's create a stopwatch so if you're using eclipse we're going to go to file new and we're going to create a new Java project let's call this stopwatch but make sure you don't have your caps lock on like I did so stop watch and then click finish and we're not going to create a module because I find modules annoying alright then go to your source folder and then go to file new class and we're going to create a class maybe called main and then click public static void main ok let's create another class then so make sure you go to your source folder then go to file new class and let's call this class stop watch and then we'll click finish ok so then going back to your main class all we're going to do here is create an instance of our stopwatch class so we're going to type in stopwatch and then we're going to create a name for this stopwatch so we can call it stopwatch with a lowercase s so stopwatch stopwatch equals new stopwatch parenthesis semicolon and that's it so we're going to do everything else within our stopwatch class so we want our stopwatch class to implement the action listener interface so we're going to implements action it's a capital L listener there we go so we need to import a few things so we're going to need 3 imports import java.awt asterisk and import java.awt event dot asterisk and import Java X dot swing dot asterisk so basically we're importing everything for java.awt and Java X dot swing ok so since we're implementing actionlistener we actually need some methods here more specifically the action performed method so it's not gonna work without it so let's get the framework for our stopwatch class all taken care of so we're going to want a few things first so let's create a constructor for this stopwatch class so stop watch and a set of parentheses a set of curly braces and we're also going to create three methods so we'll create a method called start so void start parentheses curly braces and we'll create another method called stop and then reset okay I believe that is everything for our framework now we're going to be declaring some global variables and objects so you want to be sure that you're writing this outside of your constructor so that all of these methods will have access to these global variables and objects so right after we create this class what we're gonna do is to clear a few things so let's create a J frame first so J frame will call this frame equals new J frame will want a start button and a reset button so we can use a J button for this J button will call this start button equals new J button and then we can actually set some text on this button so maybe we'll just say start and we'll do the same thing for a reset button so start button and reset button and we'll change the text here as well so we'll change that to reset well want a jlabel to hold the current time so we'll call this time label equals new J label we're also going to create a integer variable called the elapsed time but make sure you spell it right he elapsed time so this is going to hold the amount of basically milliseconds after we start our timer then we're going to have an integer variable called seconds to hold the amount of seconds that have passed an integer variable named minutes we're going to set these all to zero by the way and int hours you could go crazy and add days to but we'll just stick with seconds minutes and hours now we're also going to want a boolean value and we'll call this started and we'll set this to false we're going to use this to determine if our timer has started or not now with our time label we'll want to put in some zeros kind of as placeholders for the seconds minutes and hours one easy way we can do that is by using string format so we're going to create a string for hours minutes and seconds and they're going to act as a placeholder that'll hold a bunch of zeros as the time is going up so we'll create a string and let's call the first one maybe seconds underscore string equals and then we're going to use string a dot format so we're going to be formatting this string so we'll need to use the format specifier so it's the % and we want to display two zeros if there's currently no value in one of these places basically so we'll place zero to D so if seconds is 0 it's just going to display two zeros if it becomes one it's going to be 0 1 then 0 2 so on and so forth in that pattern same thing with the rest of these so we're going to add loops not hours we're going to as seconds here okay and then we're going to do the same thing for minutes and hours and we can just change a few things so we're going to change seconds to minutes and then we're going to change that here as well and then hours okay so we're going to hold off on creating a timer until the very end because we want to be sure that everything else is working first now for this next part we're going to be adding a few things within our constructor for our stopwatch so let's begin by adding the frame so we're going to type in frame dot set default close operation it's a lot to type and then within here j-frame dot exit underscore on closed that's just so it closes out when you hit the X button basically all right and we're going to set a size to this frame so frame dot set size and then we're going to set to dimensions I tend to like 420 by 420 because 420 is a funny number and then we're going to set frame dot set layout no because I don't like layout managers and we need to set this as visible so we're going to do that at the very end so frame dot set visible and this will be true now make sure that frame dot set visible is at the end of your constructor because if you add components after frame dot set visible sometimes it doesn't work so make sure you have this line last so I'm just going to go back to this main class and then hit run yep here is our frame it is 420 by 420 so this is enough to hold our stopwatch basically okay let's go back to our constructor and let's add a few things before our frame so let's add the time label that's going to hold the current hours minutes and seconds so what will be first is set me text so we'll type in time label dot set text and then we're going to add ours underscore string and that is this right here plus and let's add a colon to separate each of these fields basically so our string plus minutes string plus another colon then plus you guessed it seconds underscore string okay let's do some formatting of this time label so time label and let's set the bounds so set bounds and we're going to place this where X is 100 y is 100 we'll make this 200 pixels long and 100 pixels for the height let's also change the font so time label dot set font then within parentheses new font and the first value is you can pick a font that you like some fonts aren't compatible with Java but one that I kind of like for this project was I think it's pronounced for Danno that's not too bad of a font but you can always pick something you prefer better and let's have this font be plain so font dot plain and 35 is a decent size and let's set a border so time label dot set border then within parentheses border factory and then dot create and then you can pick a border from here I kind of like bevel border one time label dot set I don't know how to pronounce this word OPEC opaque somebody's probably making fun of you right now then lastly let's set the horizontal alignment so time label dot set or is on till alignment and we're going to type in J text field dot Center okay let's see if this label actually appears now oh we need to add it as well almost forgot so we'll do this right by the frame so frame dot add time label I'll probably forget to add one of these components so let's try it now yep here we go let's add the buttons now so let's start with the start button so start button dot set bounds and we'll place this where X is 100 Y is 200 this will be 100 pixels for the width and the height will be 50 pixels so start button dot let's set the font to you know what I'm just gonna copy it here and change a few things just to save time alright so start button dot set font new font and maybe we'll change this to ink free I kind of like that font but pick whatever font you want that's compatible and this will be 20 it's a slightly smaller sized font alright then we'll type in start button and we don't want this to be focusable because things that are focusable are annoying so dot set focusable false and then we need to add an action listener so this button actually does something so start button dot add action listener parentheses then we're going to type in the word this now let's do the same thing for our reset button and honestly we can just copy this and make a few changes so we're going to change start button here to reset button and then for set bounds we're going to change this x-coordinate to 200 so it's right next to the start button and we can keep everything else the same okay now we just need to add these to our frame so frame dot add start button and then frame dot add reset button and let's make sure that these appear all right not too shabby we need them to do stuff though so we'll have to work on that okay guys this next part is going to be a little complex we're going to create a timer and you know what looking back I don't know if I ever created a video on timers in Java I think I have in other languages so that's probably something I'll have to do but don't worry I'll kind of explain things along the way and if you copy it exactly like I do it should be fine so we're going to create a timer here along with our global variables and objects so timer and we'll call this timer the lowercase T equals new timer and then we're going to pass in a few arguments the first value is how often do we want this timer to do something like how frequently let's say that we want this timer to update the current time every second so the unit we're going to be passing in is how frequently we want this timer to do something in milliseconds so if we want this timer to do something every one second we're going to pass in 1000 for 1000 milliseconds which equals one second so the next argument separated with a comma we're going to pass in a new action listener then you need a set of parentheses and this parts going to get a little complicated so we're going to add a set of curly braces within here and we're going to add an action performed method so type in public void action performed parentheses and then pass in action event yeah well I guess we're not passing something in this is a parameter and then a set of curly braces then everything within this set of curly braces is what this timer is going to do every 1000 milliseconds let me just add a semicolon at the end things can get kind of funky with all these curly braces and parentheses and if you use an eclipse everything might become color-coordinated if everything checks out so we're just going to be passing in 1080 then within here we're going to define a actionperformed method and this is what our timer is going to do every 1000 milliseconds now the first line is that we're going to increase the elapsed time by 1000 milliseconds so you can type in elapsed time plus equals 1000 or the longhand way which is more clear is elapsed time equals elapsed time plus 1000 which works the same then we'll also need to figure out how many hours have passed so one way to do that is that we can take our hours variable set this equal to within parenthesis elapsed time divided by 3,600,000 this number is the amount of milliseconds that are in one hour so dividing the total elapsed time that is passed by 3,600,000 milliseconds will give us the amount of hours that have passed then let's do something similar for minutes and seconds so minutes equals within parenthesis elapsed time but this time divided by 60,000 because there's 60,000 Millicent in one minute and then we're also going to set this to modulus 60 so what we're doing here is that we don't want it to display like 60 minutes or 61 minutes or anything above that so let's say that it's at 59 minutes so 59 modulus 60 would be 59 when this increases to 60 minutes 60 modulus 60 would be zero because modulus gives you the remainder and then 61 modulus 60 would be just 1 and then our hours would increase at that time now let's add seconds so seconds equals elapsed time divided by 1,000 because there's 1,000 milliseconds in one second and then modulus 60 because we don't want to display anything 60 or above okay so then we'll want to set our different strings so we can just copy this so we're going to update these different strings so our second string equals whatever our seconds currently is at and then we're going to be adding this later so then let's do the same thing for minutes and then hours so all of these strings are gonna update and the last thing to do within this timer is that we're going to update our time label with these new strings for the hours minutes and seconds so time label dot set text then within parenthesis we're going to add our hours first so hours string and then we'll just add a colon character between these plus our minutes string plus another : plus our seconds string now what we'll want to do is to start our timer when we hit our start button so let's close out of this and head down there too we're going to start with actionperformed so what we'll check here is if e get source parentheses is equal to our start button then what we'll do so within curly braces is we're gonna call the start function and then within the start function all we need to type here is the name of our timer so timer dot start okay let's test this out so far whoops not restart just start okay let's try it crossing my fingers here okay so it's not started currently but when we hit start it is in fact starting so that's pretty sweet to see now let's also test the minutes and the seconds and I don't feel like waiting here for like a minute or even less so an hour so what we're gonna do with our elapsed time is just give this a little boost so let's set this to 60 seconds to begin with and let's see if it begins at one minute yep you can see that it's fine at one minute let's try three million six hundred thousand milliseconds yeah and it starts at one hour and you can see that minutes reset back to zero when it hits sixty okay so we know that that's working then our timer so let's reset elapsed time back to zero and work on a few things within our actionperformed method then so for this part we're going to need our started boolean variable and it's currently false so one thing that I'm thinking is that if we hit our start button we also want it to function as a stop button and we don't have any way of stopping this timer unless we reset it so we can toggle if this is a start button or a stop button but first we'll want to check to see if our started boolean variable is currently false or if it's true so currently it is false so the first thing we'll check so within if the actionperformed is equal to our start button we'll start our timer but then we'll also check if started is equal to false is it currently started and then what we'll do is take our started boolean variable and set this to true and then we'll also change the text on her button so we'll just type in start button dot set text and then we'll change this to stop so we can toggle that button between being a start button and a stop button and I'm going to take this method call and just put it within our if statement so we're only going to start our timer if started is currently equal to false now let's create an else statement so if started is not false it must be true then so our timer must be currently running so honestly we could just take all of these and then we can flip a few things so we're going to take started and flip it to false we're going to change our start button to display a start and then we're going to call the stop method then and then we'll just have to go to our stop method and then set that so timer dot stop and let's try it now okay so the timer is started and then let's stop it yep and it stopped and let's start it again pretty sweet right okay let's work on this reset button and that should be it and for the reset button we're going to write that within our actionperformed method so we got to figure out where this if statement ends okay right here yeah make sure you don't write this within this if statement so it looks like it ends right here so if you dot get source parenthesis is equal to our reset button then what we want to do within the set of curly braces is we're going to take started and set this to false because we're resetting the button then we're also going to let's see we're going to take our start and we should also set that to start so we're gonna type start here and the last thing is that we're going to call our reset method and this is the last thing that we need to fill out so let's go to our reset method we're going to take our timer and then we're going to use its stop function here then we also want to reset all the values so we're going to take our elapsed time variable set this equal to zero and then we're going to take our seconds and set it to zero as well our minutes to zero and our hours to zero and now we just have to update the time label so I kind of feel like just copying and pasting it because I really don't feel like typing this out okay so we're going to take all of this and copy it so second string equals string format yadda yadda yadda same thing with minutes during our string and then time label dot set text to whatever all of these are so take all of that copy it and then we're going to paste it within our reset method here so feel free to pause the video if you want to type this down but it should look like this then so when we hit the reset button we're taking all of these values setting everything back to zero and then we're updating the second string minute string and our string and then we're going to change our time label and display all of these strings then all right let's run this bad boy here is our timer it is currently stopped let's start it it's currently going up let's stop it now let's reset it sweet everything's back to zero let's start it again and it's counting back up from zero so that's pretty sweet one less thing before you go now take a look at this so since we created an instance of this stopwatch class what would happen if we created another so I'm going to call this next one stopwatch two so we have two instances of the stop watch glass just stop watch and stop watch - let's try it now let's see what happens so now we actually have to stop watches and they each have their own individual timers which is pretty sweet so hopefully this will give you some ideas on things where you can have like two timers concurrently going but yeah I guess that's one way to make a stopwatch in Java if you would like a copy of all this code I'll include it in the comments down below and pin it to the top but yeah that's one way of creating a stopwatch in Java hey you yeah I'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music]
Info
Channel: Bro Code
Views: 19,215
Rating: undefined out of 5
Keywords: Java stopwatch, Java stopwatch tutorial, Java stopwatch gui, Java stopwatch gui code, java stopwatch timer, stopwatch java, java swing stopwatch
Id: 0cATENiMsBE
Channel Id: undefined
Length: 27min 40sec (1660 seconds)
Published: Thu Jun 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.