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

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

Is there any way to add a "button" to the Windows Notification Area using Java

问题

以下是您要翻译的内容:

我已经制作了一个小的锻炼提醒应用程序,每小时在顶部提醒我做一些锻炼 - 有没有办法添加一些按钮,上面写着“我做了”/“我跳过了”之类的?下面是源代码,但可能与手头的问题无关:

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

  1. import java.awt.*;
  2. import java.time.LocalDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. import java.util.Random;
  5. public class WorkoutReminder {
  6. static final String workouts[] = {
  7. "俯卧撑", "弯举", "深蹲", "仰卧起坐", "提拉", "跪着的锤子推举", "反向俯卧撑", "腹肌轮"
  8. };
  9. static final int reps[] = {
  10. 10, 14, 15, 17, 20
  11. };
  12. public static void main(String args[]) throws AWTException, InterruptedException {
  13. if (SystemTray.isSupported()) {
  14. WorkoutReminder wr = new WorkoutReminder();
  15. wr.displayReminder();
  16. } else {
  17. System.out.println("请在 Windows 上运行此应用程序。");
  18. }
  19. }
  20. private static void displayReminder() throws AWTException, InterruptedException {
  21. while (true) {
  22. DateTimeFormatter dtfm = DateTimeFormatter.ofPattern("mm");
  23. DateTimeFormatter dtfh = DateTimeFormatter.ofPattern("HH");
  24. LocalDateTime currentTime = LocalDateTime.now();
  25. String min = dtfm.format(currentTime);
  26. int hour = Integer.parseInt(dtfh.format(currentTime));
  27. System.out.print(min);
  28. if (min.equals("00") && (hour >= 9 && hour <= 21)) {
  29. Random rand = new Random();
  30. String workout = workouts[Math.abs(rand.nextInt() % workouts.length)];
  31. int rep = reps[Math.abs(rand.nextInt() % reps.length)];
  32. SystemTray st = SystemTray.getSystemTray();
  33. Image logo = Toolkit.getDefaultToolkit().createImage("image.png");
  34. TrayIcon trayIcon = new TrayIcon(logo, "锻炼提醒");
  35. trayIcon.setImageAutoSize(true);
  36. String iconText = "锻炼提醒";
  37. String workoutText = "让我们开始吧: " + workout + ", " + rep + " 个重复\n";
  38. System.out.print(" --" + workoutText);
  39. trayIcon.setToolTip("嘿!");
  40. st.add(trayIcon);
  41. trayIcon.displayMessage(workoutText, iconText, TrayIcon.MessageType.INFO);
  42. Thread.sleep(60000);
  43. } else {
  44. System.out.print(" - " + (60 - Integer.parseInt(min)) + " 分钟剩余\n");
  45. Thread.sleep(60000);
  46. }
  47. }
  48. }
  49. }
英文:

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通知区域添加一个”按钮”?

  1. import java.awt.*;
  2. import java.time.LocalDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. import java.util.Random;
  5. public class WorkoutReminder {
  6. static final String workouts[] = {
  7. &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;,
  8. &quot;Ab roller&quot;
  9. };
  10. static final int reps[] = {
  11. 10,14,15,17,20
  12. };
  13. public static void main(String args[])throws AWTException, InterruptedException{
  14. if(SystemTray.isSupported()) {
  15. WorkoutReminder wr = new WorkoutReminder();
  16. wr.displayReminder();
  17. }
  18. else{
  19. System.out.println(&quot;Run this on windows plz &lt;3&quot;);
  20. }
  21. }
  22. private static void displayReminder() throws AWTException, InterruptedException{
  23. while (true) {
  24. DateTimeFormatter dtfm = DateTimeFormatter.ofPattern(&quot;mm&quot;);
  25. DateTimeFormatter dtfh = DateTimeFormatter.ofPattern(&quot;HH&quot;);
  26. LocalDateTime currentTime = LocalDateTime.now();
  27. String min = dtfm.format(currentTime);
  28. int hour = Integer.parseInt(dtfh.format(currentTime));
  29. System.out.print(min);
  30. if(min.equals(&quot;00&quot;) &amp;&amp; ( hour &gt;=9 &amp;&amp; hour &lt;= 21)) {
  31. Random rand = new Random();
  32. String workout = workouts[Math.abs(rand.nextInt() % workouts.length)];
  33. int rep = reps[Math.abs(rand.nextInt() % reps.length)];
  34. SystemTray st = SystemTray.getSystemTray();
  35. Image logo = Toolkit.getDefaultToolkit().createImage(&quot;image.png&quot;);
  36. TrayIcon trayIcon = new TrayIcon(logo, &quot;Workout you fat slob&quot;);
  37. trayIcon.setImageAutoSize(true);
  38. String iconText = &quot;Workout reminder&quot;;
  39. String workoutText = &quot;Let&#39;s get it: &quot; + workout + &quot; for &quot; + rep + &quot; reps\n&quot;;
  40. System.out.print(&quot; --&quot; + workoutText);
  41. trayIcon.setToolTip(&quot;YEEEET&quot;);
  42. st.add(trayIcon);
  43. trayIcon.displayMessage(workoutText, iconText, TrayIcon.MessageType.INFO);
  44. Thread.sleep(60000);
  45. }
  46. else{
  47. System.out.print(&quot; - &quot; + (60-Integer.parseInt(min)) + &quot; minutes left\n&quot;);
  48. Thread.sleep(60000);
  49. }
  50. }
  51. }
  52. }

答案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:

确定