import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Prtimer implements ActionListener
{
JPanel panel;
JFrame frame;
JButton start;
JButton stop;
JLabel time;
public Prtimer()
{
// window objects
frame = new JFrame("TIMER!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
time = new JLabel("00:00");
panel.add(time);
start = new JButton("Start");
start.add(Box.createRigidArea(new Dimension(100, 100)));
panel.add(start);
start.setActionCommand("Start");
start.addActionListener(this);
stop = new JButton("Stop");
stop.add(Box.createRigidArea(new Dimension(100, 100)));
panel.add(stop);
stop.setActionCommand("Stop");
stop.addActionListener(this);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
public void count(boolean go)
{
int min = 0;
int sec = 0;
while (go)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
sec++;
if (sec == 60)
{
min++;
sec = 0;
}
panel.remove(time);
time.setText(min + ":" + sec);
panel.add(time);
}
}
public void actionPerformed(ActionEvent ev)
{
String click = ev.getActionCommand();
if (click == "Start")
{
count(true);
}
if (click == "Stop")
{
count(false);
}
}
public static void main(String [] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame.setDefaultLookAndFeelDecorated(false);
Prtimer Screen = new Prtimer();
}
});
}
}
The main source of error is my count method and I was hoping someone would be able to tell me what might be going wrong.
Thank You!


Sign In
Create Account


Back to top









