I have been struggling on this problem for a while so I thought I'd seek out some help.
I am creating an application for a game which requires me to make a dynamic countdown timer. By dynamic I mean being able to put your desired countdown timer in as the consumer. The problem I'm having is making my code wait 1000 milliseconds to execute the update code with the correct time. I am attempting to use the sleep
functionality to do this...
This is not the application I am making this is just to simplify my problem down as much as I could for anyone willing to help me. Everything in here is directly from WindowBuilder in Eclipse IDE. The issue I'm having is getting the Thread thread = new Thread();
work with Thread.sleep(1000);
for the full 1 second delay.
package test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.Font;
public class test {
private JFrame frame;
/**
*
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.RED);
JLabel lblNewLabel = new JLabel("Test");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 62));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread thread = new Thread();
for(int i = 60;i>=0;i--){
thread.sleep(500);
lblNewLabel.setText("Test" + i);
}
}
}
If you throw this code in your IDE an error that reads Unhandled exception InterruptedException
comes up. If I add in throws declarations the code just messes all up truthfully I don't know what the problem is there.
How can I fix or work around this?