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);
    }
    }

КОМЕНТАРІ • 125

  • @BroCodez
    @BroCodez  4 роки тому +34

    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);
    }
    }// ***************************************************

    • @jaxtonabraham4778
      @jaxtonabraham4778 3 роки тому

      Instablaster...

    • @joyceasante8292
      @joyceasante8292 Рік тому

      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);

      }
      }

    • @rob.it0717
      @rob.it0717 Рік тому

      Thx very much, helped me a lot with my chess clock in my school project :)

  • @girl6994
    @girl6994 4 роки тому +39

    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.

    • @adeyemiadeolu2961
      @adeyemiadeolu2961 3 роки тому +3

      is this an effective way to learn coding

    • @bewaryoftherabbithole8011
      @bewaryoftherabbithole8011 3 роки тому +2

      yeah it has also improved my English skills a bit xD

    • @kkaragoz
      @kkaragoz 2 роки тому +2

      Keep repeating. You can conquer the world with this determination :)

  • @angelcastineira2561
    @angelcastineira2561 4 роки тому +14

    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!

    • @BroCodez
      @BroCodez  4 роки тому +9

      awesome dude! It's probably natural for you now

  • @Sam-mo9sm
    @Sam-mo9sm 3 роки тому +8

    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!

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 2 дні тому

    Thank you for this new lesson

  • @elgatoJNF
    @elgatoJNF 2 роки тому

    This is the best Java video I have seen, thank you very much.

  • @vedP-02
    @vedP-02 4 роки тому +2

    It was very helpful and i learned some new things.
    Great video

  • @Dygit
    @Dygit 4 роки тому +4

    Your videos deserve more attention

  • @primeoptumas8435
    @primeoptumas8435 3 роки тому

    Wow bro.. I am learning a lot. Its a very best java coding tutorials ever. Thank you a lot.

  • @ousmanjallow1643
    @ousmanjallow1643 4 роки тому +1

    Bro am really benefiting a lot from your lectures. thanks

  • @vitobahari2ia079
    @vitobahari2ia079 2 роки тому +1

    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

  • @boubou3171
    @boubou3171 Рік тому

    Thanks for the tutorial it is pretty clear and easy to understand, I learn a lot thanks to you !

  • @samuel43834
    @samuel43834 3 роки тому +2

    Very useful video. New in java, wanting to make something new and u helped me a lot with it. Thks bro, keep this content :)

    • @BroCodez
      @BroCodez  3 роки тому +1

      thanks for watching Samuel!

  • @customministrymusic4954
    @customministrymusic4954 3 роки тому +2

    Such a great and helpful video! Thank you!

    • @BroCodez
      @BroCodez  3 роки тому

      thanks for watching!

  • @cate01a
    @cate01a 4 роки тому +3

    you make the best tutorials :D

  • @SwingDanceOpa
    @SwingDanceOpa 2 роки тому

    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!

  • @admoonhermiz1
    @admoonhermiz1 11 місяців тому

    This is amazing !!!
    please keep up up a good work,and make more projects

  • @solracnauj19
    @solracnauj19 Рік тому

    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

  • @mozcoder
    @mozcoder Рік тому

    Amazing tutorial, thank you Bro.

  • @socaljusticewarrior558
    @socaljusticewarrior558 2 роки тому

    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

  • @SimonZhao-sq1wz
    @SimonZhao-sq1wz Рік тому

    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!

    • @SmallSpoonBrigade
      @SmallSpoonBrigade Рік тому

      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.

  • @yasinmohammadi8
    @yasinmohammadi8 3 роки тому +1

    That video was really helpful

  • @PetronasBoyy
    @PetronasBoyy 2 роки тому

    Thanks mate, helped me a lot for my 3rd year.

  • @365motivation.9
    @365motivation.9 Рік тому

    Thank you bro.I learned alot in your tutorial

  • @parsachavoshu8797
    @parsachavoshu8797 2 роки тому

    Greatttttt!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @user-bg9cj2iq9m
    @user-bg9cj2iq9m Рік тому

    Cheers Bro

  • @justinjohnson422
    @justinjohnson422 2 роки тому

    This tutorial was awesome!

  • @changeme1832
    @changeme1832 2 роки тому

    Nice bro :-D

  • @anonymous-ws5cv
    @anonymous-ws5cv 2 роки тому

    Thankyou so much

  • @vedanshisahay9179
    @vedanshisahay9179 2 роки тому

    I just love u channel 🔥🔥

  • @vortexjl9680
    @vortexjl9680 3 роки тому +2

    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)?

    • @axle4565
      @axle4565 2 роки тому

      this IS for java

  • @zunairaiqbal1490
    @zunairaiqbal1490 4 роки тому +1

    Thank uhhh it helped me alotttt❤️🤔

  • @anonimanonim1223
    @anonimanonim1223 2 роки тому

    Nice tutotial.

  • @sujanbasnet8243
    @sujanbasnet8243 3 роки тому

    Thanks for making this video

  • @aarushbasak4941
    @aarushbasak4941 2 роки тому

    Good jOB BRO

  • @silvergodanalyser7762
    @silvergodanalyser7762 3 роки тому +3

    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 :/

  • @kirsty6108
    @kirsty6108 4 роки тому +4

    can you make a log in system next? That would be very helpful

    • @girl6994
      @girl6994 4 роки тому

      Kirsty Holmes that’s easier to make it on HTML. And we deal with it by java in servlet

    • @girl6994
      @girl6994 4 роки тому

      But you can simply use JOptionpane.showinputdialog to make it or use Scanner and file output stream to make a simple demo.

    • @BroCodez
      @BroCodez  4 роки тому

      I'll see what I can come up with

  • @n9583
    @n9583 3 роки тому

    Bro code thank you so much make a playlist for swing stuff you can do you are very underrated subbed

  • @raviratnakar3803
    @raviratnakar3803 3 роки тому +1

    I am fellow bro!

  • @Foxlaren
    @Foxlaren 3 роки тому +3

    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

    • @APDesignFXP
      @APDesignFXP 2 роки тому +3

      It's seems like you have used the wrong import of Timer, make sure to import javax.swing.Timer; and not java.awt.Timer;

  • @gautampareek5076
    @gautampareek5076 3 роки тому +2

    Can we add milli second in label,how?

  • @NatyTor
    @NatyTor Рік тому

    Awesome bro!

  • @sergeyb6071
    @sergeyb6071 4 роки тому +2

    Great video Bro! Any idea why JButton and JFrame has a slight delay of about a second to load when the app is opened?

    • @BroCodez
      @BroCodez  4 роки тому +1

      Hmmm I'm not sure what caused that

  • @nobodyanonimus7953
    @nobodyanonimus7953 2 роки тому

    nice tutorial

  • @EyBossPusi
    @EyBossPusi 5 місяців тому

    420 is a funny number caught you bro haha

  • @yuryaraujo1612
    @yuryaraujo1612 Рік тому

    Nice!

  • @princekumarmehta8788
    @princekumarmehta8788 3 роки тому

    Thank you so much bro...

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 роки тому

    Thanks

  • @trendbitess
    @trendbitess 3 роки тому

    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?

  • @kemann3815
    @kemann3815 2 роки тому

    Great 👍

  • @xXMaDGaMeR
    @xXMaDGaMeR 2 роки тому

    awesome video Bro !

  • @augischadiegils.5109
    @augischadiegils.5109 3 роки тому +1

    Thanks bro :)

  • @AbdirazakAli1
    @AbdirazakAli1 3 роки тому

    420,420 because it's funny number. great tutorial

  • @aakmsh
    @aakmsh 2 роки тому

    Thank you ! ❤️

  • @hanami279
    @hanami279 2 роки тому

    Wow!

  • @bartschauer7391
    @bartschauer7391 3 роки тому

    code very helpful and nicely explained, does anyone know how to add hundredths or thousandths of a second?
    Regards, Bart

  • @dr1lltrckz147
    @dr1lltrckz147 2 роки тому

    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?

  • @ibenja7515
    @ibenja7515 3 роки тому

    Thank you bro

  • @theslipperypot8o674
    @theslipperypot8o674 3 роки тому

    I know this is late, but how would the code look if you wanted to measure milliseconds as well?

  • @developerjunior446
    @developerjunior446 3 роки тому

    Cool!!!!

  • @jasonkress6287
    @jasonkress6287 2 роки тому

    I followed the tutorial exactly and even tried coping and pasting the code but my timer isnt starting.

  • @imie5762
    @imie5762 2 роки тому

    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.

  • @eugenezuev7349
    @eugenezuev7349 2 місяці тому

    sweet

  • @Stryker-ye7wn
    @Stryker-ye7wn 3 роки тому

    Not sure if u still check comments but how would I add milliseconds into the stopwatch?

  • @gaetanbarette3339
    @gaetanbarette3339 3 роки тому +1

    thanks

  • @ElepIn
    @ElepIn 2 роки тому

    thx!

  • @hudihu5395
    @hudihu5395 2 роки тому

    Yeah Bro, 4:20 is really an interesting number lol

  • @salaheddine6556
    @salaheddine6556 3 роки тому

    what's the difference between Time from java.util and Time from javax.swing and even there is a third one ???

  • @khabbazz89
    @khabbazz89 Рік тому

    Why you do not use windowbuilder?!

  • @releNtless-eu3xc
    @releNtless-eu3xc 3 роки тому

    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?

  • @tytuskopak3998
    @tytuskopak3998 3 роки тому

    Hey Bro, I'm new to coding in Java. Could you tell me how can I put two stopwathes in one window? Thanks.

  • @alaapnair6923
    @alaapnair6923 3 роки тому

    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

  • @ElectroMitch
    @ElectroMitch 2 роки тому

    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.

  • @maniron168
    @maniron168 2 роки тому

    oh wow i made the stop watch please asirbad me
    hahaha

  • @frgaming6711
    @frgaming6711 4 роки тому

    nice sir TYSM

  • @JehanSaren
    @JehanSaren Рік тому

    why dont make pause and resume?

  • @agny369
    @agny369 3 роки тому

    is this implementable with java fx ?

  • @YTtopPAULO
    @YTtopPAULO 2 роки тому

    bro how do you open this window?
    can you tell meor tech me?

  • @smarkie2910
    @smarkie2910 3 роки тому

    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);
    ?

    • @smarkie2910
      @smarkie2910 3 роки тому +1

      is there a way to make this work
      ?

    • @BroCodez
      @BroCodez  3 роки тому +1

      yeah you would want an action performed method that will check to see if the stopbutton was clicked. If so, call timer.stop();

    • @smarkie2910
      @smarkie2910 3 роки тому +1

      @@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?

  • @maxwong1768
    @maxwong1768 2 роки тому

    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() ;
    }

    }

  • @adityawaghmode4751
    @adityawaghmode4751 4 роки тому +1

    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?:(

    • @BroCodez
      @BroCodez  4 роки тому

      Sure, do you have an error code at all or is something not displaying? I'm not sure where to begin helping

    • @michael54321
      @michael54321 3 роки тому

      @@BroCodez error: constructor Timer in class Timer cannot be applied to given types;

  • @manikandanbalasubramani3172
    @manikandanbalasubramani3172 3 роки тому

    Hi sir! can we set up a stopwatch through rest api

  • @솜쿤-g6o
    @솜쿤-g6o 2 роки тому +1

    한국인은 저 밖에...?

  • @munisahmad9125
    @munisahmad9125 3 роки тому +2

    Algorithm where you at?

  • @gavblack
    @gavblack 5 місяців тому

    its crazy how you can watch and copy everything that he is doing line by line and it still fucks up

  • @francisllaneta9536
    @francisllaneta9536 2 роки тому

    hi can someone tell me how to add a time lapse in this stopwatch??

  • @kelloxdrc9485
    @kelloxdrc9485 2 роки тому

    I tried adding milliseconds, but for some reason the time goes up slower, can anybody help?

    • @kelloxdrc9485
      @kelloxdrc9485 2 роки тому

      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);
      }
      }

  • @ozzyyorke9851
    @ozzyyorke9851 3 роки тому

    hello how can u change the icon thanks

  • @APDesignFXP
    @APDesignFXP 2 роки тому

    If anyone wants enhanced and optimized tutorial for this app, comment on my comment and I'd use his code for opt tutorial.

  • @lolnypop
    @lolnypop 6 місяців тому

    Comment for you so the algorithm finds you more often :D

  • @ng5m0
    @ng5m0 2 роки тому

    how to add miliseconds?

  • @rohansachdeva627
    @rohansachdeva627 Рік тому

    why are you mixing kebap_case. Just stick to camelCase. Its painful for the eyes.. Anyways goodjob

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Рік тому

  • @tntalex28
    @tntalex28 Рік тому

    :)

  • @AlfiNR
    @AlfiNR 9 місяців тому

    dont dislike Bro Code channel

  • @zari_723
    @zari_723 Рік тому

    hi

  • @maxwong1768
    @maxwong1768 2 роки тому

    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) ;
    }

  • @RobotWorks
    @RobotWorks 2 роки тому

    I need help

    • @RobotWorks
      @RobotWorks 2 роки тому

      I typed the code multiple times on different versions of eclipse but they all told me that there is an error why is that?