无聊写个JFrame开发摸鱼提醒小玩意儿

声明

前几天加班太狠了今天调休一天,睡到下午醒了没什么事就写了一个这样的小玩意儿。
大佬看到之后不要把我拉到黑名单里,本人没有任何摸鱼行为,工作认真负责,能够高强度连续加班。(滑稽)

先上效果:

无聊写个JFrame开发摸鱼提醒小玩意儿_第1张图片
无聊写个JFrame开发摸鱼提醒小玩意儿_第2张图片

前一阵子收到微信群里的一条消息如下:

无聊写个JFrame开发摸鱼提醒小玩意儿_第3张图片

心想:嗯?!这玩意儿我为啥不做一个呢?

着手开始准备:

无聊写个JFrame开发摸鱼提醒小玩意儿_第4张图片
结构:
无聊写个JFrame开发摸鱼提醒小玩意儿_第5张图片

假期对象:Holiday

package eneity;

import java.util.Date;

public class Holiday {
    private String type;
    private Date dateDay;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDateDay() {
        return dateDay;
    }

    public void setDateDay(Date dateDay) {
        this.dateDay = dateDay;
    }

    public Holiday(String type, Date dateDay) {
        this.type = type;
        this.dateDay = dateDay;
    }
}

窗口+工具类:Swing

import eneity.Holiday;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.Timer;

class Swing extends JFrame  {
    private final static Date NOW_DATE = new Date();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat showSdf = new SimpleDateFormat("M月d日");
    private static SimpleDateFormat timeSdf = new SimpleDateFormat("HH");
    private static SimpleDateFormat sdfiTmes = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat yaer = new SimpleDateFormat("YYYY");
    private static SimpleDateFormat month = new SimpleDateFormat("MM");
    private static SimpleDateFormat day = new SimpleDateFormat("dd");


    Swing() throws  Exception{
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize( 700, 270);
        this.setLocationRelativeTo(null);//在屏幕中居中显示
        JPanel panel = new JPanel();
        this.add(panel);
        placeComponents(panel);
        //下班倒计时标题
        Date endDate = sdfiTmes.parse(yaer.format(NOW_DATE)+ month.format(NOW_DATE)+ day.format(NOW_DATE)+ "180000");
        this.setTitle("您正在加班!建议立即关机!");
        if(NOW_DATE.getTime()<endDate.getTime()){
            java.util.Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    setTitle("【您的下班时间是18:00,距离您下班还有"+getEndWrokTimes(endDate)+"】");
                }
            }, 0, 1000);
        }
        this.setVisible(true);
    }



    private static void placeComponents(JPanel panel) throws  Exception{
        int i = 15;
        int y = 25;
        List<Holiday> list = new ArrayList<>();
        list.add(new Holiday("中秋节",sdf.parse("2021-09-19")));
        list.add(new Holiday("国庆节",sdf.parse("2021-10-01")));
        list.add(new Holiday("元旦",sdf.parse("2022-01-01")));
        list.add(new Holiday("春节",sdf.parse("2022-02-01")));
        panel.setLayout(null);
        panel.add( getLabel("【国家摸鱼总办】"+showSdf.format(NOW_DATE)+","+getTimeText()+"摸鱼人,工作再累,一定不要忘记摸鱼哦。",i) );i+=y;
        panel.add( getLabel("有事没事起身去茶水间去厕所去廊道走走,别老在工位上坐着,钱是老板的,但命是自己的。",i) );i+=y;
        int zmDays = zm();
        if(zmDays==0){
            panel.add( getLabel("顶住今天!明天就是周六,加油!打工人!",i) );i+=y;
        }else{
            panel.add( getLabel("距离本周周末还有"+zm()+"天",i) );i+=y;
        }
        for (Holiday holiday :list){
            if(holiday.getDateDay().getTime()<NOW_DATE.getTime()){
                continue;
            }
            panel.add( getLabel(daysCount(holiday),i) );i+=y;
        }
    }

    /**
     * 封装方法
     * @param text 内容
     * @param y 位置Y
     * @return 返回label组件
     */
    private static JLabel getLabel(String text,int y){
        JLabel label = new JLabel(text);
        label.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
        label.setBounds(10,y,2000,30);
        return label;
    }


    /**
     *
     * @return 输出下班时分秒
     */
    private static String getEndWrokTimes(Date endDate){
        Date nowDate = new Date();
        long time = endDate.getTime()-nowDate.getTime();
        // 时
        int hour = Math.round(time / 1000 / 60 / 60 % 24);
        // 分
        int minute = Math.round(time / 1000 / 60 % 60);
        // 秒
        int second = Math.round(time / 1000 % 60);
        return String.format("%s时%s分%s秒", hour, minute, second);

    }


    /**
     * @return 问候语
     */
    private static String getTimeText()    {
        String time;
        if (belongCalendar(0,6)){
            time = "凌晨了";
        }else if(belongCalendar(6,11)){
            time = "早上好";
        }else if(belongCalendar(11,14)){
            time = "中午好";
        }else if (belongCalendar(14,19)){
            time = "下午好";
        }else if (belongCalendar(19,24)){
            time = "晚上好";
        }else{
            time = "你好";
        }
        return time;
    }

    /**
     * 判断是否在时间范围内
     * @param beginTime 开始时间
     * @param endTime  结束时间
     * @return 范围内?真:假
     */
    private static boolean belongCalendar(int beginTime, int endTime) {
        return (Integer.parseInt(timeSdf.format(NOW_DATE))>beginTime && Integer.parseInt(timeSdf.format(NOW_DATE))<= endTime);
    }


    /**
     *
     * @param holiday 假期对象
     * @return  输出文本:距离假期还有XX天
     */
    private static String daysCount(Holiday holiday){
        int days = (int) ((holiday.getDateDay().getTime() - NOW_DATE.getTime()) / (1000*3600*24));
        if(days==0){
            return "明天就是"+holiday.getDateDay()+"!";
        }else{
            return "距离"+holiday.getType()+"假期还有"+days+"天";
        }
    }


    /**
     *
     * @return 最近一次周末天数
     */
    private static int zm()  {
        int days = 0;
        Calendar calendar = new GregorianCalendar();
        for(int i=1;;i++){
            calendar.add(calendar.DATE,1);
            if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
                days = --i;
                break;
            }
        }
        return days;
    }

}

Main

public class Main{

    public static void main(String[] args) throws Exception{
        new Swing();
    }

}

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