JLabel没有使用计时器进行更新

时间:2013-06-02 09:08:46

标签: java swing timer

我在互联网上搜索试图找到如何自动更新时钟gui程序,没有运气。我尝试了许多不同的方法,结果相同。我正在使用swing计时器,因为我已经读过这是处理gui的最好方法。我尝试过validate(),revalidate(),repaint()。 以下是代码的一部分:

import javax.swing.*;
//import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClockPanel extends JPanel { 
    Calendar now = Calendar.getInstance();
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE); 
    int second = now.get(Calendar.SECOND);
    int month = now.get(Calendar.MONTH) + 1;
    int day = now.get(Calendar.DAY_OF_MONTH);
    int year = now.get(Calendar.YEAR);

    private String currentTime;
    private JLabel current;

    public ClockPanel() {
        super();
        currentTime = getTime();
        current = new JLabel(currentTime);
        add(current);

        ActionListener updater = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                getTime();
                if(second == 60){
                    current.setText(currentTime);
                    revalidate();

                }
                getTime();
            }
        };
        Timer timer = new Timer(1000, updater);
        timer.start();
    }

这是我最终的最终代码;我知道这是错的,但我已经把它改变了好几个小时,我放弃了。感谢

编辑:这是代码的其余部分

public String getTime() {
        String time;
        String period = "AM";
        String monthName = "";
        switch(month) {
            case (1):
                monthName = "January";
                break;
            case (2):
                monthName = "February";
                break;
            case (3):
                monthName = "March";
                break;
            case (4):
                monthName = "April";
                break;
            case (5):
                monthName = "May";
                break;
            case (6):
                monthName = "June";
                break;
            case (7):
                monthName = "July";
                break;
            case (8):
                monthName = "August";
                break;
            case (9):
                monthName = "September";
                break;
            case (10):
                monthName = "October";
                break;
            case (11):
                monthName = "November";
                break;
            case (12):
                monthName = "December";
                break;
        }
        if(hour >= 12){
            period = "PM";
        }
        time = monthName + " " + day + ", " + year + " "
                + hourCheck(hour) + ":" + minuteCheck(minute) + period;
        return time;
    }

    private int hourCheck(int hour) {
        if(hour > 12){
            hour -= 12;
            return hour;
        }
       if(hour == 0) {
            hour += 12;
            return hour;
        }
       return hour;
    }

    private StringBuilder minuteCheck(int minute) { 
       StringBuilder time = new StringBuilder();

       if(minute < 10) {
           return time.append("0").append(minute);
       }
       else {
           time.append("").append(minute);
       }
       return time;
    }

}

2 个答案:

答案 0 :(得分:3)

您正在调用getTime(),但忽略它返回的内容。因此,当前时间保持原样。用以下代码替换您的代码:

    ActionListener updater = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            String newTime = getTime();
            current.setText(newTime);
        }
    };

编辑:

您不仅忽略getTime()返回的值,而且getTime()也始终返回相同的内容,因为您始终重用已初始化的月,分,秒等当面板建成时,只有一次。如果不使用标准的DateFormat类,你也会让生活变得非常困难。

从面板中删除除JLabel之外的所有字段,并将getTime()方法更改为以下内容:

private String getTime() {
    // get the CURRENT date:
    Date currentDate = new Date();

    // format it and return it:
    return new SimpleDateFormat("MMMM dd, yyyy hh:mm a").format(currentDate);
}

答案 1 :(得分:2)

您可以将代码调整为此状态,并且可以正常运行:

public class ClockPanel extends JPanel { 
    private final JLabel current;

    public ClockPanel() {
        current = new JLabel(now());
        add(current);

        new Timer (1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                current.setText(now());
            }
        }).start();
    }

    private static String now() {
        // implement your own
        return new Date().toString();
    }

    public static void main(String[] args) {
        JFrame main = new JFrame("Simple Clock");
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(200, 200);
        main.setContentPane(new ClockPanel());
        main.setVisible(true);
    }
}