0

我在整个互联网上搜索,试图找到如何制作一个自动更新时钟 gui 程序,但没有运气。我尝试了许多不同的方法,以相同的结果结束。我正在使用摇摆计时器,因为我已经读过这是处理 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;
    }

}
4

2 回答 2

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);
}
于 2013-06-02T09:12:56.550 回答
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);
    }
}
于 2013-06-02T09:23:27.930 回答