Java如何动态展示实时日期和时间?
- 内容介绍
- 文章标签
- 相关推荐
本文共计411个文字,预计阅读时间需要2分钟。
Java 动态显示当前系统日期和时间;示例代码如下:
javapackage com.xin.test;
import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.SimpleDateFormat;
import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;
public class DateTimeDisplay extends JPanel implements ActionListener { private JLabel dateTimeLabel; private SimpleDateFormat dateFormat;
public DateTimeDisplay() { dateTimeLabel=new JLabel(); dateTimeLabel.setFont(new Font(Serif, Font.BOLD, 24)); dateTimeLabel.setForeground(Color.BLUE); add(dateTimeLabel);
dateFormat=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
Timer timer=new Timer(1000, this); timer.start(); }
@Override public void actionPerformed(ActionEvent e) { dateTimeLabel.setText(dateFormat.format(new java.util.Date())); }
public static void main(String[] args) { // 创建 JFrame 实例 // ... }}
Java 动态显示当前系统的日期、时间;如图所示:
package com.xin.test; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JLabel; import javax.swing.Timer; import javax.swing.JFrame; public class NowTime extends JFrame { private static final long serialVersionUID = 4306803332677233920L; // 添加 显示时间的JLabel public NowTime() { JLabel time = new JLabel(); time.setForeground(Color.BLUE); time.setBounds(30, 0, 900, 130); time.setFont(new Font("微软雅黑", Font.BOLD, 80)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(null); this.setTitle("now time"); this.setBounds(500, 200, 930, 200); this.setVisible(true); this.add(time); this.setTimer(time); } // 设置Timer 1000ms实现一次动作 实际是一个线程 private void setTimer(JLabel time) { final JLabel varTime = time; Timer timeAction = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // 转换日期显示格式 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); varTime.setText(df.format(new Date(timemillis))); } }); timeAction.start(); } // 运行方法 public static void main(String[] args) { new NowTime(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计411个文字,预计阅读时间需要2分钟。
Java 动态显示当前系统日期和时间;示例代码如下:
javapackage com.xin.test;
import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.SimpleDateFormat;
import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;
public class DateTimeDisplay extends JPanel implements ActionListener { private JLabel dateTimeLabel; private SimpleDateFormat dateFormat;
public DateTimeDisplay() { dateTimeLabel=new JLabel(); dateTimeLabel.setFont(new Font(Serif, Font.BOLD, 24)); dateTimeLabel.setForeground(Color.BLUE); add(dateTimeLabel);
dateFormat=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
Timer timer=new Timer(1000, this); timer.start(); }
@Override public void actionPerformed(ActionEvent e) { dateTimeLabel.setText(dateFormat.format(new java.util.Date())); }
public static void main(String[] args) { // 创建 JFrame 实例 // ... }}
Java 动态显示当前系统的日期、时间;如图所示:
package com.xin.test; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JLabel; import javax.swing.Timer; import javax.swing.JFrame; public class NowTime extends JFrame { private static final long serialVersionUID = 4306803332677233920L; // 添加 显示时间的JLabel public NowTime() { JLabel time = new JLabel(); time.setForeground(Color.BLUE); time.setBounds(30, 0, 900, 130); time.setFont(new Font("微软雅黑", Font.BOLD, 80)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(null); this.setTitle("now time"); this.setBounds(500, 200, 930, 200); this.setVisible(true); this.add(time); this.setTimer(time); } // 设置Timer 1000ms实现一次动作 实际是一个线程 private void setTimer(JLabel time) { final JLabel varTime = time; Timer timeAction = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // 转换日期显示格式 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); varTime.setText(df.format(new Date(timemillis))); } }); timeAction.start(); } // 运行方法 public static void main(String[] args) { new NowTime(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

