2013-06-02 74 views
0

我已經在互聯網上搜索試圖找到如何使一個自動更新時鐘gui程序沒有運氣。我嘗試了許多不同的方法,並以相同的結果結束。我正在使用swing Timer,因爲我已經讀過這是處理gui的最好方法。我嘗試過驗證(),重新驗證(),repaint()。 下面是部分代碼:JLabel沒有更新與計時器

import javax.swing.*; 
//import java.awt.event.*; 
import java.util.*; 
import javax.swing.Timer; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class ClockPanel extends JPanel { 
    Calendar now = Calendar.getInstance(); 
    int hour = now.get(Calendar.HOUR_OF_DAY); 
    int minute = now.get(Calendar.MINUTE); 
    int second = now.get(Calendar.SECOND); 
    int month = now.get(Calendar.MONTH) + 1; 
    int day = now.get(Calendar.DAY_OF_MONTH); 
    int year = now.get(Calendar.YEAR); 

    private String currentTime; 
    private JLabel current; 

    public ClockPanel() { 
     super(); 
     currentTime = getTime(); 
     current = new JLabel(currentTime); 
     add(current); 

     ActionListener updater = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       getTime(); 
       if(second == 60){ 
        current.setText(currentTime); 
        revalidate(); 

       } 
       getTime(); 
      } 
     }; 
     Timer timer = new Timer(1000, updater); 
     timer.start(); 
    } 

這是我結束了最後的代碼;我知道這是錯誤的,但我已經改變了這麼多好幾個小時,我放棄了。由於

編輯:這裏是你調用getTime()代碼

public String getTime() { 
     String time; 
     String period = "AM"; 
     String monthName = ""; 
     switch(month) { 
      case (1): 
       monthName = "January"; 
       break; 
      case (2): 
       monthName = "February"; 
       break; 
      case (3): 
       monthName = "March"; 
       break; 
      case (4): 
       monthName = "April"; 
       break; 
      case (5): 
       monthName = "May"; 
       break; 
      case (6): 
       monthName = "June"; 
       break; 
      case (7): 
       monthName = "July"; 
       break; 
      case (8): 
       monthName = "August"; 
       break; 
      case (9): 
       monthName = "September"; 
       break; 
      case (10): 
       monthName = "October"; 
       break; 
      case (11): 
       monthName = "November"; 
       break; 
      case (12): 
       monthName = "December"; 
       break; 
     } 
     if(hour >= 12){ 
      period = "PM"; 
     } 
     time = monthName + " " + day + ", " + year + " " 
       + hourCheck(hour) + ":" + minuteCheck(minute) + period; 
     return time; 
    } 

    private int hourCheck(int hour) { 
     if(hour > 12){ 
      hour -= 12; 
      return hour; 
     } 
     if(hour == 0) { 
      hour += 12; 
      return hour; 
     } 
     return hour; 
    } 

    private StringBuilder minuteCheck(int minute) { 
     StringBuilder time = new StringBuilder(); 

     if(minute < 10) { 
      return time.append("0").append(minute); 
     } 
     else { 
      time.append("").append(minute); 
     } 
     return time; 
    } 

} 
+0

代碼中的任何編譯器錯誤...?它似乎 – pinkpanther

+0

你可以發佈方法'getTime()'的代碼嗎? – Genzer

+0

編譯器內部沒有任何錯誤。我使用NetBeans,如果有幫助 – TechnicalCustoms

回答

3

的休息,但忽略了它所返回。當前時間保持原來的狀態。替換爲您的代碼:

ActionListener updater = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      String newTime = getTime(); 
      current.setText(newTime); 
     } 
    }; 

編輯:

你不僅忽略getTime()返回的值,但也getTime()總是返回同樣的事情,因爲你總是重用月,分,秒等已經被初始化了一次,當面板被構建時。你不使用標準的DateFormat類也會讓你的生活變得非常困難。

從面板除了一個JLabel這樣刪除的所有字段,並更改getTime()方法的東西:

private String getTime() { 
    // get the CURRENT date: 
    Date currentDate = new Date(); 

    // format it and return it: 
    return new SimpleDateFormat("MMMM dd, yyyy hh:mm a").format(currentDate); 
} 
+0

我有一個getTime()函數返回時間。如果你喜歡,我可以發佈整個代碼。它可能會使它更容易 – TechnicalCustoms

+0

@TechnicalCustoms發佈整個代碼以獲得更好的響應 – pinkpanther

+0

正如您所說:它返回時間。所以如果你忽略它的返回值,調用它不會改變任何東西。重新閱讀我的答案,並嘗試我發佈的代碼。另請參閱Genzer的示例,該示例已完成並按照您的要求進行。 –

2

你可以調整你的代碼,這和它的工作:

public class ClockPanel extends JPanel { 
    private final JLabel current; 

    public ClockPanel() { 
     current = new JLabel(now()); 
     add(current); 

     new Timer (1000, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       current.setText(now()); 
      } 
     }).start(); 
    } 

    private static String now() { 
     // implement your own 
     return new Date().toString(); 
    } 

    public static void main(String[] args) { 
     JFrame main = new JFrame("Simple Clock"); 
     main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     main.setSize(200, 200); 
     main.setContentPane(new ClockPanel()); 
     main.setVisible(true); 
    } 
} 
+0

+1爲SSCCE,並顯示在面板中的所有這些領域是不必要的。 –