Java stopwatch ⏱️
Вставка
- Опубліковано 19 вер 2024
- java stopwatch program
#java #stopwatch #program
// ***************************************************
public class Main {
public static void main(String[] args) {
Stopwatch stopwatch = new Stopwatch();
}
}
// ***************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Stopwatch implements ActionListener{
JFrame frame = new JFrame();
JButton startButton = new JButton("START");
JButton resetButton = new JButton("RESET");
JLabel timeLabel = new JLabel();
int elapsedTime = 0;
int seconds =0;
int minutes =0;
int hours =0;
boolean started = false;
String seconds_string = String.format("%02d", seconds);
String minutes_string = String.format("%02d", minutes);
String hours_string = String.format("%02d", hours);
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
elapsedTime=elapsedTime+1000;
hours = (elapsedTime/3600000);
minutes = (elapsedTime/60000) % 60;
seconds = (elapsedTime/1000) % 60;
seconds_string = String.format("%02d", seconds);
minutes_string = String.format("%02d", minutes);
hours_string = String.format("%02d", hours);
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string);
}
});
Stopwatch(){
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string);
timeLabel.setBounds(100,100,200,100);
timeLabel.setFont(new Font("Verdana",Font.PLAIN,35));
timeLabel.setBorder(BorderFactory.createBevelBorder(1));
timeLabel.setOpaque(true);
timeLabel.setHorizontalAlignment(JTextField.CENTER);
startButton.setBounds(100,200,100,50);
startButton.setFont(new Font("Ink Free",Font.PLAIN,20));
startButton.setFocusable(false);
startButton.addActionListener(this);
resetButton.setBounds(200,200,100,50);
resetButton.setFont(new Font("Ink Free",Font.PLAIN,20));
resetButton.setFocusable(false);
resetButton.addActionListener(this);
frame.add(startButton);
frame.add(resetButton);
frame.add(timeLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton) {
if(started==false) {
started=true;
startButton.setText("STOP");
start();
}
else {
started=false;
startButton.setText("START");
stop();
}
}
if(e.getSource()==resetButton) {
started=false;
startButton.setText("START");
reset();
}
}
void start() {
timer.start();
}
void stop() {
timer.stop();
}
void reset() {
timer.stop();
elapsedTime=0;
seconds =0;
minutes=0;
hours=0;
seconds_string = String.format("%02d", seconds);
minutes_string = String.format("%02d", minutes);
hours_string = String.format("%02d", hours); timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string);
}
}
public class Main {
public static void main(String[] args) {
Stopwatch stopwatch = new Stopwatch();
}
}
// ***************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Stopwatch implements ActionListener{
JFrame frame = new JFrame();
JButton startButton = new JButton("START");
JButton resetButton = new JButton("RESET");
JLabel timeLabel = new JLabel();
int elapsedTime = 0;
int seconds =0;
int minutes =0;
int hours =0;
boolean started = false;
String seconds_string = String.format("%02d", seconds);
String minutes_string = String.format("%02d", minutes);
String hours_string = String.format("%02d", hours);
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
elapsedTime=elapsedTime+1000;
hours = (elapsedTime/3600000);
minutes = (elapsedTime/60000) % 60;
seconds = (elapsedTime/1000) % 60;
seconds_string = String.format("%02d", seconds);
minutes_string = String.format("%02d", minutes);
hours_string = String.format("%02d", hours);
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string);
}
});
Stopwatch(){
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string);
timeLabel.setBounds(100,100,200,100);
timeLabel.setFont(new Font("Verdana",Font.PLAIN,35));
timeLabel.setBorder(BorderFactory.createBevelBorder(1));
timeLabel.setOpaque(true);
timeLabel.setHorizontalAlignment(JTextField.CENTER);
startButton.setBounds(100,200,100,50);
startButton.setFont(new Font("Ink Free",Font.PLAIN,20));
startButton.setFocusable(false);
startButton.addActionListener(this);
resetButton.setBounds(200,200,100,50);
resetButton.setFont(new Font("Ink Free",Font.PLAIN,20));
resetButton.setFocusable(false);
resetButton.addActionListener(this);
frame.add(startButton);
frame.add(resetButton);
frame.add(timeLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton) {
if(started==false) {
started=true;
startButton.setText("STOP");
start();
}
else {
started=false;
startButton.setText("START");
stop();
}
}
if(e.getSource()==resetButton) {
started=false;
startButton.setText("START");
reset();
}
}
void start() {
timer.start();
}
void stop() {
timer.stop();
}
void reset() {
timer.stop();
elapsedTime=0;
seconds =0;
minutes=0;
hours=0;
seconds_string = String.format("%02d", seconds);
minutes_string = String.format("%02d", minutes);
hours_string = String.format("%02d", hours);
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string);
}
}// ***************************************************
Instablaster...
Practicing...😓
public class Main
{
public static void main (String[]args)
{
Stopwatch stopwatch = new Stopwatch ();
Stopwatch stopwatch2 = new Stopwatch();
}
}
***************************
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Stopwatch implements ActionListener{
JFrame frame = new JFrame();
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton resetButton = new JButton("Reset");
JLabel timeLabel = new JLabel();
int elapsedTime = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;
boolean started = false;
String seconds_string = String.format("%02d",seconds);
String minutes_string = String.format("%02d",minutes);
String hours_string = String.format("%02d",hours);
Timer timer = new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e){
elapsedTime = elapsedTime +1000;
hours = (elapsedTime/3600000)%60;
minutes = (elapsedTime/60000)%60;
seconds = (elapsedTime/1000);
seconds_string = String.format("%02d",seconds);
minutes_string = String.format("%02d",minutes);
hours_string = String.format("%02d",hours);
timeLabel.setText(hours_string+ ": "+minutes_string+":"+hours_string);
}
});
Stopwatch(){
timeLabel.setText(hours_string+ ":"+minutes_string+":"+hours_string);
timeLabel.setBounds(100,100,200,100);
timeLabel.setFont(new Font("Times New Roman", Font.PLAIN,18));
timeLabel.setBorder(BorderFactory.createBevelBorder(1));
timeLabel.setOpaque(true);
timeLabel.setHorizontalAlignment(JTextField.CENTER);
startButton.setBounds(100,200,100,50);
startButton.setFont(new Font("Hellvetica", Font.PLAIN,12));
startButton.setFocusable(false);
startButton.addActionListener(this);
resetButton.setBounds(200,200,100,50);
resetButton.setFont(new Font("Hellvetica", Font.PLAIN,12));
resetButton.setFocusable(false);
resetButton.addActionListener(this);
frame.add(startButton);
frame.add(timeLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(380,380);
frame.setLayout(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()== startButton){
if(started==false){
started=true;
startButton.setText("Stop");
start();
}
else{
started=false;
startButton.setText("Start");
stop();
}
}
if(e.getSource()==resetButton){
started=false;
startButton.setText("Start");
reset();
}
}
void start(){
timer.start();
}
void stop(){
timer.stop();
}
void reset(){
timer.stop();
elapsedTime = 0;
seconds = 0;
minutes = 0;
hours = 0;
seconds_string = String.format("%02d",seconds);
minutes_string = String.format("%02d",minutes);
hours_string = String.format("%02d",hours);
timeLabel.setText(hours_string+ ": "+minutes_string+":"+hours_string);
}
}
Thx very much, helped me a lot with my chess clock in my school project :)
I practiced your code by using 2 hours and I wrote it for 67 times. Now I feels coding is very easy, and coding has greatly improved my English skills too.
is this an effective way to learn coding
yeah it has also improved my English skills a bit xD
Keep repeating. You can conquer the world with this determination :)
Around half of the video, I stopped it and completedthe rest of the program by myself. and it worked :) I'm learning a lot thanks to your series!
awesome dude! It's probably natural for you now
MORE OF THIS! Very nice. As someone who is relatively new to coding I love to write the code with your help, and then being able to understand it by myself. For me, it is a great way of learning, keep this up please!
Thank you for this new lesson
This is the best Java video I have seen, thank you very much.
It was very helpful and i learned some new things.
Great video
Your videos deserve more attention
Wow bro.. I am learning a lot. Its a very best java coding tutorials ever. Thank you a lot.
Bro am really benefiting a lot from your lectures. thanks
thank you, learning from watching videos is the best way to learn for me, and this is a really well made one, good job Bro Code bro
Thanks for the tutorial it is pretty clear and easy to understand, I learn a lot thanks to you !
Very useful video. New in java, wanting to make something new and u helped me a lot with it. Thks bro, keep this content :)
thanks for watching Samuel!
Such a great and helpful video! Thank you!
thanks for watching!
you make the best tutorials :D
Very good! Now I beginning to understand how a Timer in Java works. Hopefully I can use it to anymate my wavegenerator I wrote in both VB and Java. Thanks for this wonderful video!
This is amazing !!!
please keep up up a good work,and make more projects
Muchas gracias por esta clase, me ayudo mucho con mi proyecto, tendria mi duda en la cuestion de poder hacerlo metodo para poderlo usar con cualquier ob
Amazing tutorial, thank you Bro.
I've already subscribed, but I am still going to like and comment. Liking is easy enough, but it'll be a while before I think of something to comment
Hi Bro Code! thank you for this tutorial. one small thing: I notice that you write the following code multiple times, but I think you dont have to because they are global variable:
String seconds_string = String.format("%02d", seconds);
String mins_string = String.format("%02d", mins);
String hours_string = String.format("%02d", hours);
all in all, rock solid tutorial!
Personally, I'd just store time as a single variable and have a method to take that and convert it to the appropriate format for display. I assume he did that because global variables are best kept to a minimum in case you want or need to refactor later and didn't want to go through the process of creating a specific method for dealing with that.
That video was really helpful
Thanks mate, helped me a lot for my 3rd year.
Thank you bro.I learned alot in your tutorial
Greatttttt!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Cheers Bro
This tutorial was awesome!
Nice bro :-D
Thankyou so much
I just love u channel 🔥🔥
This was such a good video, I was looking for something exactly like this, and this is awsome. Could you make a video for a Timer in Java too(in case you haven't done it already)?
this IS for java
Thank uhhh it helped me alotttt❤️🤔
Nice tutotial.
Thanks for making this video
Good jOB BRO
Thanks for the vid - im study programming and this is still helpful!
Where you get your Eclipse theme? I have already a black one, but my overlay around my code panel is still white :/
See his first video..
can you make a log in system next? That would be very helpful
Kirsty Holmes that’s easier to make it on HTML. And we deal with it by java in servlet
But you can simply use JOptionpane.showinputdialog to make it or use Scanner and file output stream to make a simple demo.
I'll see what I can come up with
Bro code thank you so much make a playlist for swing stuff you can do you are very underrated subbed
I am fellow bro!
When I try to pass two arguments to the timer it says
"The constructor Timer(int, new ActionListener(){}) is undefined"
I got this error for the second time ,even in that 2D animation video of the same playlist ,
What can I do :(
@Bro Code
It's seems like you have used the wrong import of Timer, make sure to import javax.swing.Timer; and not java.awt.Timer;
Can we add milli second in label,how?
Awesome bro!
Great video Bro! Any idea why JButton and JFrame has a slight delay of about a second to load when the app is opened?
Hmmm I'm not sure what caused that
nice tutorial
420 is a funny number caught you bro haha
Nice!
Thank you so much bro...
Thanks
nice tutorial , you teach very smooth well done. I found one small bug here. when second counts if i wait 500 milliseconds and push stop button and again start stopwatch wait 1000 milliseconds to count one second further. how can we fix this issue?
Great 👍
awesome video Bro !
Thanks bro :)
420,420 because it's funny number. great tutorial
Thank you ! ❤️
Wow!
code very helpful and nicely explained, does anyone know how to add hundredths or thousandths of a second?
Regards, Bart
What if I wanted the timer to change every millisecond? The integer conversion in nanoseconds is too large of a number in Java, what should I do?
Thank you bro
I know this is late, but how would the code look if you wanted to measure milliseconds as well?
Cool!!!!
I followed the tutorial exactly and even tried coping and pasting the code but my timer isnt starting.
Is there a way to also implement miliseconds? I tried changing timer delay to 1ms and increment elapsedTime by 1ms, but it doesnt work. Stopwatch starts counting miliseconds really slow for some reason.
sweet
Not sure if u still check comments but how would I add milliseconds into the stopwatch?
thanks
thx!
Yeah Bro, 4:20 is really an interesting number lol
what's the difference between Time from java.util and Time from javax.swing and even there is a third one ???
Why you do not use windowbuilder?!
How do I add Alarm capability so that I can set an alarm? Can I also add both of these to my Clock app that you taught us before? can I combine all of them to have a proper advanced clock app? Otherwise, can you at least teach us how to make one?
Hey Bro, I'm new to coding in Java. Could you tell me how can I put two stopwathes in one window? Thanks.
Hey, what is the variable where I can try to say if the time is 10 seconds stop or do something. I say if ____ = 10 seconds
I tried to do this but it didn't work, so I copied and pasted the code in the description to see exactly how he did it and it was still coming up with the same issue.
Me too
oh wow i made the stop watch please asirbad me
hahaha
nice sir TYSM
thanks for watching TYSM
@@BroCodez lmao
why dont make pause and resume?
is this implementable with java fx ?
bro how do you open this window?
can you tell meor tech me?
since i want to make the the stop button separate i would have to add the following right?
JButton stopButton = new JButton("STOP");
stopButton.setBounds(200,200,100,50);
stopButton.setFont(new Font("Arial",Font.PLAIN,20));
stopButton.setFocusable(false);
stopButton.addActionListener(this);
frame.add(stopButton);
?
is there a way to make this work
?
yeah you would want an action performed method that will check to see if the stopbutton was clicked. If so, call timer.stop();
@@BroCodez is there a way to make it using just by label instead of Jlabel, our professor said not to use it as its not given in our syllabus, help?
I make the stopwatch by my own self before look at your solution . I cant believe that I can do it .
Class : Stopwatch
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
public class Stopwatch implements ActionListener {
JFrame frame ;
JLabel timeLabel ;
JButton startButton , stopButton , resetButton ;
Timer timer ;
TimerTask task ;
String time ;
int secOne , secTen , minOne , minTen , hrOne , hrTen ;
// store the previous state : start = 1 , stop = 0 , reset = -1 .
int state = -1 ;
Stopwatch() {
timeLabel = new JLabel("00:00:00") ;
timeLabel.setFont(new Font("MV Boli" , Font.PLAIN , 50)) ;
timeLabel.setForeground(Color.white) ;
startButton = new JButton("Start") ;
stopButton = new JButton("Stop") ;
resetButton = new JButton("Reset") ;
startButton.addActionListener(this) ;
stopButton.addActionListener(this) ;
resetButton.addActionListener(this) ;
frame = new JFrame("Stopwatch") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
frame.setSize(500 , 500) ;
frame.setVisible(true) ;
frame.setLocationRelativeTo(null) ;
frame.setLayout(new FlowLayout()) ;
frame.getContentPane().setBackground(Color.black) ;
frame.add(timeLabel) ;
frame.add(startButton) ;
frame.add(stopButton) ;
frame.add(resetButton) ;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
System.out.println("test2") ;
showCount() ;
// prevent multiple task threads run together if user clicks [Start] for too many times .
if (state != 1) {
timer.scheduleAtFixedRate(task , 1000 , 1000) ; // (TimerTaskName , startTime/delay , repeatPeriod)
}
state = 1 ;
}
if (e.getSource() == stopButton) {
if (state == 1) {
timer.cancel();
}
state = 0 ;
}
if (e.getSource() == resetButton) {
secOne = 0 ;
secTen = 0 ;
minOne = 0 ;
minTen = 0 ;
hrOne = 0 ;
hrTen = 0 ;
time = time.format("%d%d:%d%d:%d%d" , hrTen , hrOne , minTen , minOne , secTen , secOne) ;
timeLabel.setText(time) ;
if (state == 1) {
timer.cancel() ;
}
state = -1 ;
}
}
public void start() {
}
public void stop() {
}
public void reset() {
}
public void showCount() {
System.out.println("test1") ;
timer = new Timer() ;
task = new TimerTask() { // TimerTask is an abstract class .
@Override
public void run() {
secOne += 1 ;
if (secOne == 10) {
secOne = 0 ;
secTen += 1 ;
if (secTen == 6) {
secTen = 0 ;
minOne += 1 ;
if (minOne == 10) {
minOne = 0 ;
minTen += 1 ;
if (minTen == 6) {
minTen = 0 ;
hrOne += 1 ;
if (hrOne == 10) {
hrOne = 0 ;
hrTen += 1 ;
}
}
}
}
}
time = time.format("%d%d:%d%d:%d%d" , hrTen , hrOne , minTen , minOne , secTen , secOne) ;
timeLabel.setText(time) ;
}
};
}
}
Class : Main
public class Main {
public static void main(String[] args) {
Stopwatch stopewatch = new Stopwatch() ;
}
}
Hey i ran the code as it is, i even copy pasted your code and followed every step as you mentioned in this video, but the code is not running please help me?:(
Sure, do you have an error code at all or is something not displaying? I'm not sure where to begin helping
@@BroCodez error: constructor Timer in class Timer cannot be applied to given types;
Hi sir! can we set up a stopwatch through rest api
한국인은 저 밖에...?
Algorithm where you at?
its crazy how you can watch and copy everything that he is doing line by line and it still fucks up
hi can someone tell me how to add a time lapse in this stopwatch??
I tried adding milliseconds, but for some reason the time goes up slower, can anybody help?
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Stopwatch implements ActionListener{
JFrame frame = new JFrame();
JButton startButton = new JButton("START");
JButton resetButton = new JButton("RESET");
JLabel timeLabel = new JLabel();
int elapsedTime = 0;
int seconds =0;
int milliseconds =0;
int minutes =0;
int hours =0;
boolean started = false;
String seconds_string = String.format("%02d", seconds);
String minutes_string = String.format("%02d", minutes);
String hours_string = String.format("%02d", hours);
String milliseconds_string = String.format("%03d", milliseconds);
Timer timer = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
elapsedTime++;
hours = (elapsedTime/3600000);
minutes = (elapsedTime/60000) % 60;
seconds = (elapsedTime/1000) % 60;
milliseconds = elapsedTime % 1000;
seconds_string = String.format("%02d", seconds);
minutes_string = String.format("%02d", minutes);
hours_string = String.format("%02d", hours);
milliseconds_string = String.format("%03d", milliseconds);
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string+":"+milliseconds_string);
}
});
Stopwatch(){
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string+":"+milliseconds_string);
timeLabel.setBounds(100,100,300,100);
timeLabel.setFont(new Font("Verdana",Font.PLAIN,35));
timeLabel.setBorder(BorderFactory.createBevelBorder(1));
timeLabel.setOpaque(true);
timeLabel.setHorizontalAlignment(JTextField.CENTER);
startButton.setBounds(100,200,100,50);
startButton.setFont(new Font("Ink Free",Font.PLAIN,20));
startButton.setFocusable(false);
startButton.addActionListener(this);
resetButton.setBounds(200,200,100,50);
resetButton.setFont(new Font("Ink Free",Font.PLAIN,20));
resetButton.setFocusable(false);
resetButton.addActionListener(this);
frame.add(startButton);
frame.add(resetButton);
frame.add(timeLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
frame.setResizable(true);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton) {
if(started==false) {
started=true;
startButton.setText("STOP");
start();
}
else {
started=false;
startButton.setText("START");
stop();
}
}
if(e.getSource()==resetButton) {
started=false;
startButton.setText("START");
reset();
}
}
void start() {
timer.start();
}
void stop() {
timer.stop();
}
void reset() {
timer.stop();
elapsedTime=0;
seconds =0;
minutes=0;
milliseconds =0;
hours=0;
seconds_string = String.format("%02d", seconds);
minutes_string = String.format("%02d", minutes);
hours_string = String.format("%02d", hours);
milliseconds_string = String.format("%03d", milliseconds);
timeLabel.setText(hours_string+":"+minutes_string+":"+seconds_string+":"+milliseconds_string);
}
}
hello how can u change the icon thanks
If anyone wants enhanced and optimized tutorial for this app, comment on my comment and I'd use his code for opt tutorial.
Comment for you so the algorithm finds you more often :D
how to add miliseconds?
why are you mixing kebap_case. Just stick to camelCase. Its painful for the eyes.. Anyways goodjob
:)
dont dislike Bro Code channel
hi
I have made it simple .
Timer timer = new Timer(1000 , new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
elapsedTime += 1000 ;
displayTime() ;
}
});
public void reset() {
timer.stop() ;
elapsedTime = 0 ;
displayTime() ;
}
public void displayTime() {
hours = elapsedTime / 3600000 ;
// limit the minutes below 60 .
// e.g. 3%60=3 , 60%60=0 , 100%60=40
minutes = (elapsedTime / 60000) % 60 ;
// limit the seconds below 60 .
seconds = (elapsedTime / 1000) % 60 ;
time = String.format("%02d:%02d:%02d" , hours , minutes , seconds) ;
timeLabel.setText(time) ;
}
I need help
I typed the code multiple times on different versions of eclipse but they all told me that there is an error why is that?