Please help me with my Java Countdown Timer
Hello generous java coders!I am really in a desperate situation now. I have a working gui code here:
Code Java:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class CountdownTimer extends JFrame {
JLabel promptLabel, timerLabel;
int counter;
JTextField tf;
JButton button;
Timer timer;
public CountdownTimer() {
setLayout(new GridLayout(2, 2, 5, 5));
promptLabel = new JLabel("Enter time: ", SwingConstants.CENTER);
add(promptLabel);
tf = new JTextField(5);
add(tf);
button = new JButton("Start timing");
add(button);
timerLabel = new JLabel("Waiting...", SwingConstants.CENTER);
add(timerLabel);
event e = new event();
button.addActionListener(e);
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void run() {
throw new UnsupportedOperationException("Not supported yet.");
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
int count = (int)(Double.parseDouble(tf.getText()));
timerLabel.setText("Time left: " + count);
TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
timer.start();
}
}
public class TimeClass implements ActionListener {
int counter;
public TimeClass(int counter) {
this.counter= counter;
}
public void actionPerformed(ActionEvent tc) {
counter--;
if(counter >= 1) {
timerLabel.setText("Time left: " + counter);
}
else {
timer.stop();
timerLabel.setText("Done!");
Toolkit.getDefaultToolkit().beep();
}
}
}
public static void main(String args[]) {
CountdownTimer gui = new CountdownTimer();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(250, 100);
gui.setTitle("Timer Program");
gui.setVisible(true);
}
}
My task is to make a gui countdown timer for a speech contest. Please help me convert it into a gui wherein when the user creates a new "session". User should be able to input the speaker's name, duration of speech(this is where the code above comes in. The code I have pasted above is only a Second Timer please help me convert it into a Minute timer as well. example: 7:00 -> 6:59 -> 6:58 so on and so forth), the speech title and a drop down box with two values: formal speech/table topics). Lastly, user should also be able to save time to a file. I am really desperate for you help guys. Please do help me :( Please please
Re: Please help me with my Java Countdown Timer
What would the user input for a minute timer?
Then how would that value be saved and displayed?
Re: Please help me with my Java Countdown Timer
Quote:
Originally Posted by
Norm
What would the user input for a minute timer?
Then how would that value be saved and displayed?
Hello, thank you so much for your response. The user should be able to input the name of the speaker, the speech duration,title of the speech and a drop down box these inputs should be saved into a file. Thank you!
Re: Please help me with my Java Countdown Timer
What happened to the input and display of the time?
For the other stuff, take a look at how to write a Swing GUI:
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
And how to write to files:
Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)