SimpleDateFormat时钟自动更新

时间:2014-03-29 22:03:32

标签: java date time clock simpledateformat

有没有办法在JPanel上自动更新SimpleDateFormat数字时钟?

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
time.setText(sdf.format(new Date()));

如果我运行此标签,则标签会在脚本启动时保留...

更新

到目前为止我写的是什么

import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;

public class test {

private JFrame frame = new JFrame("Time");
private JPanel panel = new JPanel();
private JLabel time = new JLabel();
private JButton exit = new JButton("exit");
private boolean exit_bool = false;

public test() {
    panel.setLayout(new GridBagLayout());
    panel.add(time);
    panel.add(exit);
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.setLocationRelativeTo(null);
    init(true, true);
    createHandler();
    startTime();
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            test viewable = new test();
        }
    });
}

public void init(boolean fullscreen, boolean mouse) {
    frame.setVisible(false);
    frame.dispose();
    frame.setUndecorated(true);
    frame.setVisible(true);
    if(fullscreen) {
        frame.setExtendedState(frame.MAXIMIZED_BOTH); 
    }
    if(!mouse) {
        frame.setCursor(java.awt.Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1,1,BufferedImage.TYPE_4BYTE_ABGR),new java.awt.Point(0,0),"NOCURSOR"));
    }
}

public void startTime() {
    while(!exit_bool) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    time.setText(sdf.format(new Date()));
    }
}

public void createHandler() {
    exit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitActionPerformed(evt);
        }
    });
}

private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                         
    exit_bool = true;
} 

}

1 个答案:

答案 0 :(得分:3)

类似的东西会起作用

package net.whatever;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Clock");
        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);

        final JLabel label = new JLabel("", SwingConstants.CENTER);
        label.setFont(label.getFont().deriveFont(25f));
        label.setSize(200, 100);
        frame.add(label);

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                String string = new SimpleDateFormat("HH:mm:ss").format(new Date());
                label.setText(string);
            }
        }, 0, 1000);
    }
}