Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that you have only one thread in your application. You should have at least two : one for the UI that will update the text and one for the computation of the time.</p> <p>If you have only one thread it hangs in the while(true) loop and Swing never get to update the view.</p> <p>I refactored your code using two threads :</p> <ol> <li><p>One counting until the end of time and updating the fields to keep time in memory</p></li> <li><p>Another one that uses a java.util.Timer#scheduleAtFixedRate() method wich is invoqued every 100 millisec to update the view.</p></li> </ol> <p><code>Timer.java</code> (avoid naming classes like ones in the Java API)</p> <pre><code> public class Timer { boolean shouldCount=false; int int_sec=0; int int_min=0; int int_mil=0; public Timer() { } public static void main(String[] args) { TimeFrame t = new TimeFrame(); JFrame f = new JFrame("Timer"); f.setSize(300,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); f.getContentPane().add(new TimeFrame()); f.setVisible(true); } public void count(){ Thread thread = new Thread(new Runnable() { @Override public void run() { long now = System.currentTimeMillis(); while(true){ if(shouldCount){ if(System.currentTimeMillis()-now&gt;=100){ now=System.currentTimeMillis(); int_mil++; if(int_mil&gt;9){ int_mil=0; int_sec++; if(int_sec&gt;=60){ int_sec=1; int_min++; } } } } } } }); thread.start(); } } } </code></pre> <p><code>TimeFrame</code> (I would rather call it <code>TimePanel</code> since it extends <code>JPanel</code>)</p> <pre><code>public class TimeFrame extends JPanel{ JLabel time; Timer t ; JButton pause ; JButton start ; public TimeFrame(){ t= new Timer(this); time = new JLabel("Time goes here", JLabel.CENTER); pause = new JButton ("Pause"); start = new JButton ("Start"); start.addActionListener(new starts()); pause.addActionListener(new starts()); add(time); add(start); add(pause); java.util.Timer updateTimer= new java.util.Timer(); updateTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { t.update(int_sec,int_min,int_mil); } }, 0, 100); } public void update(int s, int minute,int m){ String sec = Integer.toString(s); String min = Integer.toString(minute); String mil = Integer.toString(m); if (s&lt;=10){ sec="0"+sec; } System.out.println(min+":"+sec+","+mil); time.setText(min+":"+sec+","+mil); } public class starts implements ActionListener{ boolean firstTime=true; public void actionPerformed(ActionEvent event){ if (firstTime){ t.count(); firstTime = false; } if(event.getSource() == start){ t.shouldCount=true; }else{ t.shouldCount=false; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload