有没有办法使用Java在Windows通知区域添加一个”按钮”?

huangapple go评论44阅读模式
英文:

Is there any way to add a "button" to the Windows Notification Area using 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[] = {
        "俯卧撑", "弯举", "深蹲", "仰卧起坐", "提拉", "跪着的锤子推举", "反向俯卧撑", "腹肌轮"
    };
    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:

有没有办法使用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[] = {
&quot;Pushups&quot;, &quot;Curls&quot;, &quot;Squats&quot;, &quot;Crunches&quot;, &quot;Lat raise&quot;, &quot;Kneeling hammer Press&quot;,             &quot;Reverse pushups&quot;,
&quot;Ab roller&quot;
};
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(&quot;Run this on windows plz &lt;3&quot;);
}
}
private static void displayReminder() throws AWTException, InterruptedException{
while (true) {
DateTimeFormatter dtfm = DateTimeFormatter.ofPattern(&quot;mm&quot;);
DateTimeFormatter dtfh = DateTimeFormatter.ofPattern(&quot;HH&quot;);
LocalDateTime currentTime = LocalDateTime.now();
String min = dtfm.format(currentTime);
int hour = Integer.parseInt(dtfh.format(currentTime));
System.out.print(min);
if(min.equals(&quot;00&quot;) &amp;&amp; ( hour &gt;=9 &amp;&amp; hour &lt;= 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(&quot;image.png&quot;);
TrayIcon trayIcon = new TrayIcon(logo, &quot;Workout you fat slob&quot;);
trayIcon.setImageAutoSize(true);
String iconText = &quot;Workout reminder&quot;;
String workoutText = &quot;Let&#39;s get it: &quot; + workout + &quot; for &quot; + rep + &quot; reps\n&quot;;
System.out.print(&quot;  --&quot; + workoutText);
trayIcon.setToolTip(&quot;YEEEET&quot;);
st.add(trayIcon);
trayIcon.displayMessage(workoutText, iconText, TrayIcon.MessageType.INFO);
Thread.sleep(60000);
}
else{
System.out.print(&quot; - &quot; + (60-Integer.parseInt(min)) + &quot; minutes left\n&quot;);
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

huangapple
  • 本文由 发表于 2020年8月3日 04:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63220735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定