首页 > 解决方案 > 有没有办法使用 Java 向 Windows 通知区域添加“按钮”

问题描述

我做了一个小的锻炼提醒应用程序,它会在每个小时的顶部提醒我做一些锻炼——有没有办法在“我做了”/“我跳过它”这句话上添加一些按钮?或者其他的东西?下面的源代码,但可能与手头的问题无关:

在此处输入图像描述

import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class WorkoutReminder {
    static final String workouts[] = {
    "Pushups", "Curls", "Squats", "Crunches", "Lat raise", "Kneeling hammer Press",             "Reverse pushups",
     "Ab roller"
};
    static final int reps[] = {
            10,14,15,17,20
    };
    public static void main(String args[])throws AWTException, InterruptedException{
        if(SystemTray.isSupported()) {
            WorkoutReminder wr = new WorkoutReminder();
            wr.displayReminder();
        }
        else{
            System.out.println("Run this on windows plz <3");
        }
    }

    private static void displayReminder() throws AWTException, InterruptedException{
        while (true) {
            DateTimeFormatter dtfm = DateTimeFormatter.ofPattern("mm");
            DateTimeFormatter dtfh = DateTimeFormatter.ofPattern("HH");
            LocalDateTime currentTime = LocalDateTime.now();
            String min = dtfm.format(currentTime);
            int hour = Integer.parseInt(dtfh.format(currentTime));
            System.out.print(min);
            if(min.equals("00") && ( hour >=9 && hour <= 21)) {
                Random rand = new Random();
                String workout = workouts[Math.abs(rand.nextInt() % workouts.length)];
                int rep = reps[Math.abs(rand.nextInt() % reps.length)];

                SystemTray st = SystemTray.getSystemTray();
                Image logo = Toolkit.getDefaultToolkit().createImage("image.png");
                TrayIcon trayIcon = new TrayIcon(logo, "Workout you fat slob");
                trayIcon.setImageAutoSize(true);
                String iconText = "Workout reminder";
                String workoutText = "Let's get it: " + workout + " for " + rep + " reps\n";
                System.out.print("  --" + workoutText);
                trayIcon.setToolTip("YEEEET");
                st.add(trayIcon);
                trayIcon.displayMessage(workoutText, iconText, TrayIcon.MessageType.INFO);
                Thread.sleep(60000);
            }
            else{
                System.out.print(" - " + (60-Integer.parseInt(min)) + " minutes left\n");
                Thread.sleep(60000);
            }
        }
    }
}

标签: javawindowsnotifications

解决方案


我不知道您是否可以直接在 Java 中执行此操作(也许您可以,我只是不知道),但是您可以做的是从 java 调用一个可以执行此操作的 powershell 脚本。

有关创建这些脚本的更多信息:https ://eddiejackson.net/wp/?p=18877


推荐阅读