java - 有没有办法使用 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);
}
}
}
}
解决方案
我不知道您是否可以直接在 Java 中执行此操作(也许您可以,我只是不知道),但是您可以做的是从 java 调用一个可以执行此操作的 powershell 脚本。
有关创建这些脚本的更多信息:https ://eddiejackson.net/wp/?p=18877
推荐阅读
- docker - Azure Pipelines 中的 Docker 微服务 monorepo CI/CD
- c++ - 错误:从'转换
' 为非标量类型 - javascript - chrome.scripting.executeScript MV3 | 如何从回调范围设置本地范围属性值
- javascript - 为什么我会收到 Uncaught SyntaxError: Invalid left-hand side in assignment at HTMLButtonElement.?
- sorting - 按行长和字母顺序对字符串数组进行排序
- c# - 将逗号分隔的数字转换为非逗号分隔的小数
- visual-studio - 当我从“调试”切换到“发布”配置时,找不到#include 文件
- python - 重新排列二维列表
- python - 如何在 Django 应用程序中测试删除视图?
- swift - SwiftUI 发布的变量不更新视图