首页 > 编程语言 >JAVA-手机日期和时间显示

JAVA-手机日期和时间显示

时间:2022-11-06 16:47:38浏览次数:56  
标签:20 JAVA 50 SimpleDateFormat 日期 new 手机 jf JLabel

image

package com.itheima;

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

public class ShowDateTime02 {
    public static void main(String[] args) {
        JFrame jf=new JFrame();
        jf.setTitle("猜数字游戏");
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);        //取消窗体的默认布局


        //提示日期
        JLabel dataLable=new JLabel("日期");
        dataLable.setBounds(50,50,100,20);
        jf.add(dataLable);

        Date d=new Date();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日");
        String dateString = sdf.format(d);


        //按照格式显示日期的字符串
        JLabel showDateLable=new JLabel(dateString);
        showDateLable.setBounds(50,80,200,20);
        jf.add(showDateLable);

        //提示时间
        JLabel timeLable = new JLabel("时间");
        timeLable.setBounds(50,150,100,20);
        jf.add(timeLable);

        SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm");
        String timeString = sdf2.format(d);

        //按照格式显示时间的字符串
        JLabel showtimeLable = new JLabel(timeString);
        showtimeLable.setBounds(50,180,200,20);
        jf.add(showtimeLable);










        //添加按钮到窗体中
        jf.setVisible(true);


    }
}

执行结果

![image](/i/l/?n=22&i=blog/2426413/202211/2426413-20221106163333004-320099359.png)

标签:20,JAVA,50,SimpleDateFormat,日期,new,手机,jf,JLabel
From: https://www.cnblogs.com/cy-xt/p/16862946.html

相关文章