I'm trying to display the current time in a JFrame
. How can I refresh the text in the JLabel
without opening a separate frame each time it has to update?
Here's all of my code so far...
Test
public class Test{
static String timeDisplay = "";
public static class time extends Thread{
static int timeHours = 7;
static int timeMins = 30;
static int timeSecs = 0;
@Override
public void run(){
while(true){
try{
time.sleep(1000);
timeSecs++;
if(timeSecs == 60){
timeMins++;
timeSecs = 0;
}
if(timeMins == 60){
timeHours++;
timeMins = 0;
}
if(timeHours < 10){
if(timeMins < 10){
if(timeSecs < 10){
timeDisplay = "0" + timeHours + ":" + "0" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = "0" + timeHours + ":" + "0" + timeMins + ":" + timeSecs;
}
}
else{
if(timeSecs < 10){
timeDisplay = "0" + timeHours + ":" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = "0" + timeHours + ":" + timeMins + ":" + timeSecs;
}
}
}
else{
if(timeMins < 10){
if(timeSecs < 10){
timeDisplay = timeHours + ":" + "0" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = timeHours + ":" + "0" + timeMins + ":" + timeSecs;
}
}
else{
if(timeSecs < 10){
timeDisplay = timeHours + ":" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = timeHours + ":" + timeMins + ":" + timeSecs;
}
}
}
System.out.println(timeDisplay);
//CountDown time = new CountDown(timeDisplay);
}
catch(Exception e){
System.out.println("Something went wrong :(");
}
}
}
}
public static void main(String[] args){
time time = new time();
time.start();
try {
TimeUnit.SECONDS.sleep(1);
CountDown window = new CountDown(timeDisplay);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 500);
window.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
CountDown
public class CountDown extends JFrame{
private static final long serialVersionUID = 1L;
static JLabel label = new JLabel();
public CountDown(String time){
super("Title");
setLayout(new FlowLayout());
add(label);
label.setText("Current Time: " + time);
Handler eventHandler = new Handler();
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==""){
string = String.format("label 1: %s", event.getActionCommand());
}
}
}
}
My intentions for this program was to make a frame that displayed the current time. It's using local time from the program, not the actual time. Thanks in advance, and feel free to let me know if I should change anything in my code to make it better.