how to put a timer on a JLabel to update itself every second

后端 未结 4 1322
青春惊慌失措
青春惊慌失措 2020-12-17 04:54

I created a game and in my swing GUI interface I want to put a timer. The way I do this at the moment is have a field with the current time , gotten with System.curren

4条回答
  •  旧巷少年郎
    2020-12-17 05:36

    This is how I would set my JLabel to update with time & date.

    Timer SimpleTimer = new Timer(1000, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            jLabel1.setText(SimpleDay.format(new Date()));
            jLabel2.setText(SimpleDate.format(new Date()));
            jLabel3.setText(SimpleTime.format(new Date()));
        }
    });
    SimpleTimer.start();
    

    This is then added to your main class and the jlabel1/2/3 get updated with the timer.

提交回复
热议问题