实时时间钟表

目录

一、前提

二、代码

2.1 窗口

2.2 时间显示

三、代码整合


一、前提

        在之前我们学会了JDK时间相关类,那我们就来小小地利用它来写一个”小玩意儿”。

        没看过的快去看一遍,在     初识JDK时间相关类

二、代码

2.1 窗口

        首先,你是不是得有一个窗口,那就用得上Swing了。

import javax.swing.*;

public class test extends JFrame{

    private JPanel windonpanel;

    public test(){
        //设置窗口
        setTitle("实时钟表");
        setSize(600, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);        //窗口居中
        setAlwaysOnTop(true);               //窗口置顶
        setResizable(false);                //窗口不可调整大小,改回 true 即可调整大小

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run(){
                new test().setVisible(true);
            }
        });
    }
}

重点讲一下 :

SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run(){
                new test().setVisible(true);
            }
        });

这段代码的作用是创建并显示EnhancedClock窗口。SwingUtilities.invokeLater方法确保在事件调度线程(Event Dispatch Thread)上创建和显示窗口,这是Swing编程的最佳实践,可以避免潜在的线程安全问题。在run方法中,创建了一个EnhancedClock对象,并通过调用其setVisible(true)方法来显示窗口。

  • SwingUtilities.invokeLater(new Runnable() {...}):这个方法用于确保在事件调度线程上执行给定的Runnable。Swing组件应该在事件调度线程上创建和更新,以避免潜在的线程安全问题。
  • new Runnable() {...}:创建了一个新的Runnable对象,其run方法将在事件调度线程上执行。
  • new EnhancedClock().setVisible(true):创建了一个EnhancedClock对象,并通过调用其setVisible(true)方法来显示窗口。

注意: 在此处如果不继承 JFrame ,代码时会报错的。

            不要忘记导包!!!

2.2 时间显示

使用Swing的Timer类每秒触发一次事件

  • 在定时器的事件处理方法中更新时间显示

  • 使用Java 8的时间API(LocalDateTime和DateTimeFormatter)获取和格式化当前时间

运行效果

程序启动后会显示一个窗口,窗口中央以大字体显示当前系统时间,格式为"yyyy-MM-dd HH:mm:ss"。时间每秒更新一次,显示最新的时间。

//创建时间标签
clockLabel = new JLabel();
clockLabel.setFont(new Font("Arial", Font.BOLD, 48));
clockLabel.setHorizontalAlignment(JLabel.CENTER);
add(clockLabel);

//创建面板
timer = new Timer(1000, new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
           updateTime();
        }
});
//启动计时器
timer.start();
private void updateTime(){
        //获取时间并格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = java.time.LocalDateTime.now().format(formatter);
        //更新时间标签
        clockLabel.setText(time);
    }

三、代码整合

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.format.DateTimeFormatter;

public class EnhancedClock extends JFrame{
    private JPanel windonpanel;
    private JLabel clockLabel;
    private Timer timer;

    public EnhancedClock(){
        //设置窗口
        setTitle("实时钟表");
        setSize(600, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);        //窗口居中
        setAlwaysOnTop(true);               //窗口置顶
        setResizable(false);                //窗口不可调整大小

        //创建时间标签
        clockLabel = new JLabel();
        clockLabel.setFont(new Font("Arial", Font.BOLD, 48));
        clockLabel.setHorizontalAlignment(JLabel.CENTER);
        add(clockLabel);

        //创建面板
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateTime();
            }
        });

        //启动计时器
        timer.start();
    }

    private void updateTime(){
        //获取时间并格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = java.time.LocalDateTime.now().format(formatter);
        //更新时间标签
        clockLabel.setText(time);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run(){
                new EnhancedClock().setVisible(true);
            }
        });
    }

}

注意:我这里将  类名改为了  EnhancedClock

你可能感兴趣的:(java梦,java,jvm,spring)