JFrame的JLabel中显示电脑时间

package august_25;


import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class DataEx  extends JFrame{
private JPanel timePanel;
private JLabel timeLabel;
private JLabel displayArea;
private String DTF = "yyyy-MM-dd-HH:mm:ss";
private String time;
private int ONE_SECOND =1000;
public DataEx() {
timePanel = new JPanel();
timeLabel = new JLabel("时间");
displayArea = new JLabel();
configTimeArea();
timePanel.add(timeLabel);
timePanel.add(displayArea);
this.add(timePanel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(new Dimension(200, 70));
this.setLocationRelativeTo(null);
setVisible(true);
}

private void configTimeArea() {
// TODO Auto-generated method stub
Timer timer  =new Timer();
timer.scheduleAtFixedRate(new JLabelTimerTask(), new Date(), ONE_SECOND);
}
protected class JLabelTimerTask extends TimerTask {
SimpleDateFormat dateFormatter = new SimpleDateFormat(DTF);

@Override
public void run() {
// TODO Auto-generated method stub
time = dateFormatter.format(Calendar.getInstance().getTime());
displayArea.setText(time);
}

}


public static void main(String[] args) {
// TODO Auto-generated method stub
new DataEx();
}


}

你可能感兴趣的:(JAVA)