hi all,ive created a timer and im almost managed to get it working. The problem i have is the timer display.The time countsdown,but the display of the time is incorrect.
Below shows a working timer,but if you compile the program,you can see whats wrong if it.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Countdown extends JFrame implements ActionListener{
JLabel count = new JLabel(" 00 : 00 : 00 "); //secs display
JTextField[] tf = new JTextField[3];
JButton btn = new JButton("Go!"); //button to start the clock
int ctr; //int counter
String output = "", temp;
public Countdown() {
super("Game Timer");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel msg = new JLabel("Enter the length of time, you want to play:");
JLabel lblSec = new JLabel("sec:");
btn.addActionListener(this);
count.setFont(new Font("Arial",Font.BOLD, 20));
for(int i = 0; i < tf.length; i++) tf = new JTextField(3);
Container frame = getContentPane();
JPanel jp = new JPanel(new BorderLayout());
JPanel top = new JPanel(new FlowLayout());
JPanel mid = new JPanel(new FlowLayout());
JPanel bottom = new JPanel(new FlowLayout());
top.add(msg);
mid.add(lblSec);
mid.add(tf[2]);
bottom.add(btn);
bottom.add(count);
jp.add(top,BorderLayout.NORTH);
jp.add(mid,BorderLayout.CENTER);
jp.add(bottom,BorderLayout.SOUTH);
frame.add(jp);
setLocation(700,200);
setContentPane(frame);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
startTheClock();
}
private void startTheClock() {
for(int i = 0; i < tf.length; i++)
{
if(tf.getText().equals(""))
{
tf.setText("0");
}
}
Integer.parseInt(tf[2].getText())*1000;
final java.util.Timer tmr = new java.util.Timer();
tmr.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
temp = ""+(ctr%(60*1000)/1000);
if(temp.length() == 1) temp = "0"+temp;
output += temp+" ";
count.setText(output);
ctr -= 1000;
if(ctr < 0) tmr.cancel();
}
},0,1000);
}
public static void main(String args[]){
new Countdown();
}
}
Below shows a working timer,but if you compile the program,you can see whats wrong if it.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Countdown extends JFrame implements ActionListener{
JLabel count = new JLabel(" 00 : 00 : 00 "); //secs display
JTextField[] tf = new JTextField[3];
JButton btn = new JButton("Go!"); //button to start the clock
int ctr; //int counter
String output = "", temp;
public Countdown() {
super("Game Timer");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel msg = new JLabel("Enter the length of time, you want to play:");
JLabel lblSec = new JLabel("sec:");
btn.addActionListener(this);
count.setFont(new Font("Arial",Font.BOLD, 20));
for(int i = 0; i < tf.length; i++) tf = new JTextField(3);
Container frame = getContentPane();
JPanel jp = new JPanel(new BorderLayout());
JPanel top = new JPanel(new FlowLayout());
JPanel mid = new JPanel(new FlowLayout());
JPanel bottom = new JPanel(new FlowLayout());
top.add(msg);
mid.add(lblSec);
mid.add(tf[2]);
bottom.add(btn);
bottom.add(count);
jp.add(top,BorderLayout.NORTH);
jp.add(mid,BorderLayout.CENTER);
jp.add(bottom,BorderLayout.SOUTH);
frame.add(jp);
setLocation(700,200);
setContentPane(frame);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
startTheClock();
}
private void startTheClock() {
for(int i = 0; i < tf.length; i++)
{
if(tf.getText().equals(""))
{
tf.setText("0");
}
}
Integer.parseInt(tf[2].getText())*1000;
final java.util.Timer tmr = new java.util.Timer();
tmr.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
temp = ""+(ctr%(60*1000)/1000);
if(temp.length() == 1) temp = "0"+temp;
output += temp+" ";
count.setText(output);
ctr -= 1000;
if(ctr < 0) tmr.cancel();
}
},0,1000);
}
public static void main(String args[]){
new Countdown();
}
}