Dynamic Clock in java

后端 未结 7 1098
我在风中等你
我在风中等你 2020-12-16 02:22

I want to implement a clock within my program to diusplay the date and time while the program is running. I have looked into the getCurrentTime() method and

7条回答
  •  被撕碎了的回忆
    2020-12-16 03:22

    Note the method scheduleAtFixedRate is used here

            // Current time label
            final JLabel currentTimeLabel = new JLabel();
            currentTimeLabel.setFont(new Font("Monospace", Font.PLAIN, 18));
            currentTimeLabel.setHorizontalAlignment(JTextField.LEFT);
    
            // Schedule a task for repainting the time
            final Timer currentTimeTimer = new Timer();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    currentTimeLabel.setText(TIME_FORMATTER.print(System.currentTimeMillis()));
                }
            };
    
            currentTimeTimer.scheduleAtFixedRate(task, 0, 1000);
    

提交回复
热议问题