2016-01-27 40 views
0

現在我有(正確地)更新GUI上的標籤從表單文本框從用戶輸入倒計時的方法:如何讓我的方法在特定的日期/時間執行?

private void bendStopCounter() throws AWTException { 

     Thread counter = new Thread() { 
      public void run() { 
      // timeAway is the user input int in miliseconds, hence the conversion 
       for (int i=(int)(timeAway/1000); i>0; i=i-1) { 
        updateGUI(i,lblBendingStopTimer); 
        try {Thread.sleep(1000);} catch(InterruptedException e) {}; 
       } 
      } 

       public void updateGUI(final int i, final Label lblBendingTimer) { 
         Display.getDefault().asyncExec(new Runnable() { 

          public void run() { 
           lblBendingTimer.setText("Time until bending stops: " + i/60 + " minutes and " + i%60 + " seconds."); 
          } 
         }); 
       } 
     }; 
     counter.start(); 
    } 

這是我的GUI監聽按鈕邏輯。據分析,從形式日期/時間和(正確)從BendingController類執行邏輯做一些事情:

private void addBendingListener() 
    { 
      executeBendingButton.addSelectionListener(new SelectionAdapter() { 
       @Override 
       public void widgetSelected(SelectionEvent e) { 
         System.out.println("Bending has started! lolool"); 
         //grabs UI values and pass them to controller 
         int day = dateTimeDMY.getDay(); 
         int month = dateTimeDMY.getMonth(); 
         int year = dateTimeDMY.getYear(); 
         int hour = dateTimeHMS.getHours(); 
         int minute = dateTimeHMS.getMinutes(); 
         int second = dateTimeHMS.getSeconds(); 
         Date date = parseDate(day, month, year, hour, minute, second); 

         try { 
           System.out.println("Waiting for " + date + " to happen..."); 

           timeAway = Long.parseLong(timeAwayInMinutes.getText()); 
           timeAway = TimeUnit.MINUTES.toMillis(timeAway); 

           timer.schedule(new BenderController(timeAway,cursorMoveIncrement), date); 

           timeAway = TimeUnit.MILLISECONDS.toMillis(timeAway); 

           bendStopCounter(); 
         } 
         catch (Exception exc) { 
           MessageDialog.openError(shell, "Error", "Hey, jerkwad, you see those 2 friggin' textboxs? Yeah, put valid numbers in them next time, asshole!"); 
           return; 
         }       
       } 

      }); 
    } 

我的錯誤是,如果用戶在未來的任何時候選擇一個開始日期/時間,計數器開始計數按下執行按鈕後立即關閉。 BenderController類邏輯將在特定時間執行,因爲它正在擴展TimerTask並使用如上所述的schedule()方法。我想讓bendStopCounter()方法使用與此類似的東西,並在選定的日期/時間的同一時間開始執行。

想法?

回答

1

你會使用像石英這樣的庫來做到這一點。它使用起來非常簡單,你可以使用Cron語法,這很容易找到信息,所以你可以制定具體的時間表。

https://quartz-scheduler.org/

+0

是啊,我看到類似性質的其他一些問題,這個建議。這是不可能做到的嗎?或者外部圖書館會更好嗎? – Arcus

+0

這是可能的,並且有幾種方法可以做到這一點,但在這種情況下,使用石英幾乎肯定會更好。 –

1

對於喜歡分鐘或與該程序(例如意外關機),您可以只使用java執行人重啓迷路沒問題甚至幾小時短期的東西。(SheduledExecutor

由於您似乎有一個要求,能夠提前計劃日/月/年 與石英一起提到威廉姆斯答案。它具有內置的工作存儲可能性,可以堅持安裝工作。

然而,對於執行人一些示例代碼:

public class ExecTest 
{ 
    public static void main(String[] args) 
    { 
     ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); //create an executor with 1 worker thread. 
     final ScheduledFuture future = 
       executor.scheduleAtFixedRate(new TimeCounter(System.out), 0, 1000, TimeUnit.MILLISECONDS); //execute Timecounter every 1k milliseconds 

     executor.schedule(
       new Runnable() 
       { 
        ScheduledFuture fut= future; //grabbing the final future 

        public void run() { 
         fut.cancel(false);   //cancel the endless counter using its future   
        } 
       } 
       , 6000, TimeUnit.MILLISECONDS);  //shedule it once in 6 seks 
    } 
} 

public class TimeCounter implements Runnable 
{ 
    private PrintStream display; 
    private long startTime; 

    public TimeCounter(PrintStream out) 
    { 
     startTime=System.currentTimeMillis(); 
     display= out; 
    } 

    public void run() 
    { 
     updateGUI((System.currentTimeMillis()-startTime)/1000); 

    } 

    private void updateGUI(final long i) 
    { 
     display.println("Time until bending stops: " + i/60 + " minutes and " + i%60 + " seconds."); 
    } 
} 
相關問題