英文:
Is there any way to add a "button" to the Windows Notification Area using Java
问题
以下是您要翻译的内容:
我已经制作了一个小的锻炼提醒应用程序,每小时在顶部提醒我做一些锻炼 - 有没有办法添加一些按钮,上面写着“我做了”/“我跳过了”之类的?下面是源代码,但可能与手头的问题无关:
import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
public class WorkoutReminder {
static final String workouts[] = {
"俯卧撑", "弯举", "深蹲", "仰卧起坐", "提拉", "跪着的锤子推举", "反向俯卧撑", "腹肌轮"
};
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("请在 Windows 上运行此应用程序。");
}
}
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, "锻炼提醒");
trayIcon.setImageAutoSize(true);
String iconText = "锻炼提醒";
String workoutText = "让我们开始吧: " + workout + ", " + rep + " 个重复\n";
System.out.print(" --" + workoutText);
trayIcon.setToolTip("嘿!");
st.add(trayIcon);
trayIcon.displayMessage(workoutText, iconText, TrayIcon.MessageType.INFO);
Thread.sleep(60000);
} else {
System.out.print(" - " + (60 - Integer.parseInt(min)) + " 分钟剩余\n");
Thread.sleep(60000);
}
}
}
}
英文:
I have made a small workout reminder app that reminds me at the top of every hour to do some workouts -- Is there any way to add some buttons to this saying "I did it" / "I skipped it?" or something? Source code below, but probably not relevant to the question at hand:
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);
}
}
}
}
答案1
得分: 1
我不知道你是否可以直接在Java中做到这一点(也许可以,我只是不知道),但你可以在Java中调用一个可以执行此操作的PowerShell脚本。
有关创建这些脚本的更多信息,请查看:https://eddiejackson.net/wp/?p=18877
英文:
I don't know if you can do this in Java directly (maybe you can, I just don't know), but what you could do is call a powershell script from java that can do this.
For more info on creating these scripts: https://eddiejackson.net/wp/?p=18877
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论