Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

timer swing

843807Apr 19 2010 — edited Apr 20 2010
hi guys, i am trying to create a simple timer program. the problem is when i click on the start/stop button the program only moves one second then another when i click again. can anybody help please?{import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class Timer extends JFrame implements Runnable,ActionListener
{
public final JButton startStopButton= new JButton("Start/Stop");
//JLabel startStopButton= new JLabel();

private int secs;
private int mins;
private int hrs;
private Thread clock;

public void destroy()
{
clock.stop();
}

public void init()
{

if(clock == null)
{
clock = new Thread(this);
clock.start();
}
}

public void actionPerformed(ActionEvent ae)

{
++secs;
if(secs == 60)
{
mins++;
secs = 0;
}
if(mins == 60)
{
hrs++;
secs = 0;
mins = 0;
}

startStopButton.setText(" "+ hrs + ": " + mins + ":" + secs +"");
System.out.println(" "+ hrs + ": " + mins + ":" + secs +"");
}


public void run()
{
while(true)
{
repaint();
try
{
clock.sleep(1000);

}
catch(InterruptedException e)
{
}
}
}


public void start()
{
clock.resume();
}

public void stop()
{
clock.suspend();
}
public Timer ()
{
startStopButton.addActionListener(this);
setTitle(" "+ Calendar.getInstance().getTime());
getContentPane().add(startStopButton);

setSize(100,50);
setVisible(true);}
public static void main(String[] arg)
{
new Timer().addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

});
//}
}
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2010
Added on Apr 19 2010
5 comments
123 views