2013-10-06 60 views
0

所以我的任務(意外,作業!)是製作一個代表兩行數字時鐘的GUI。第一行是時鐘本身(hh:mm aa),第二行將日期顯示爲滾動文本(EEEE - MMMM dd,yyyy)。我已經設法讓所有這些顯示出來,但我無法弄清楚如何讓我的日期用我的電腦時鐘進行更新 - 這意味着我在1:47 PM運行它,並且它永遠不會變爲1:48下午。我一直在閱讀一些,看起來我的問題的答案是使用線程並使其具有try{Thread.sleep(1000)}或沿着這些線,但經過幾個小時的實驗後,我無法弄清楚如何應用它到我有什麼:無法弄清楚如何讓我的數字時鐘保持時間

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Date; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 

public class InnerClasses extends JFrame { 
public InnerClasses() { 
    this.setLayout(new GridLayout(2, 1)); 

    add(new TimeMessagePanel()); 
    add(new DateMessagePanel()); 
} 

/** Main method */ 
public static void main(String[] args) { 
    Test frame = new Test(); 
    frame.setTitle("Clock"); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(280, 100); 
    frame.setVisible(true); 
} 

static class TimeMessagePanel extends JPanel { 
    DateFormat timeFormat = new SimpleDateFormat("hh:mm aa"); 
    Date time = new Date(); 
    private String timeOutput = timeFormat.format(time); 
    private int xCoordinate = 105; 
    private int yCoordinate = 20; 
    private Timer timer = new Timer(1000, new TimerListener()); 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawString(timeOutput, xCoordinate, yCoordinate); 
    } 

    class TimerListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      repaint(); 
     } 
    } 
} 

static class DateMessagePanel extends JPanel { 
    DateFormat dateFormat = new SimpleDateFormat("EEEE - MMMM dd, yyyy"); 
    Date date = new Date(); 
    private String dateOutput = dateFormat.format(date); 
    private int xCoordinate = 0; 
    private int yCoordinate = 20; 
    private Timer timer = new Timer(250, new TimerListener()); 

    public DateMessagePanel() { 
     timer.start(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     if (xCoordinate > getWidth() - 50) { 
      xCoordinate = -50; 
     } 
     xCoordinate += 5; 
     g.drawString(dateOutput, xCoordinate, yCoordinate); 
    } 

    class TimerListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      repaint(); 
     } 
    } 
} 
} 

任何有識之士將不勝感激!

回答

0

1)將所有的計算邏輯(什麼是計算邏輯:獲得新的日期實例,格式化等等,什麼原位缺口計算邏輯:增加面板的佈局,設置JFrame的可視等)recompute()方法

2)調用

recompute(); 
repaint(); 

在TimerListener

+0

讓我們來看看,如果我能得到這個展現出來吧... '公共無效重新計算(){ \t \t \t日期newDate =新的日期(); \t \t \t timeOutput = timeFormat.format(newDate); \t}' 嗯,我似乎無法讓它在這裏跳線,但這就是我最終做的(然後如你所說在TimeListener中調用它),它似乎工作。所以除非你覺得我錯過了一些我沒有注意到的重要東西,我認爲這樣做 - 謝謝你的幫助! – Jerm

0

你已經有一個計時器,並重新粉刷,以畫新的時候,您需要確保您更新時間String到新的時間。

+0

是的,我想你的意思是類似於下面提出的答案。感謝您的輸入! – Jerm

0

我看到它的方式可以將問題分成3部分。

  1. 顯示給定的時間

  2. ,無論文本已經給它滾動(你可以找到這幾個選項也一樣)的組件的標籤。這可能意味着您專門爲動畫位運行線程。

  3. 更新上述兩個組件的「數據」的後臺線程(確保將其設置爲守護進程線程)。

要在後臺線程,通過1 & 2.你似乎已經想通了「休眠」部分的情況下,所以現在你只需要更新的情況下,文本數據。

此外,請確保在事件調度線程(EDT)上執行任何數據更新(如果您計劃直接更新Swing組件的內容)。

HTH

+0

我繼續使用上面的答案,因爲它似乎是最容易集成到我現有的代碼中,但爲了學習,我想嘗試理解您的建議。對於第2步,你是說'把'protected void paintComponent(Graphics g)'和它裏面的內容放到一個線程中?因爲我相信我早些時候嘗試過,並得到編譯器錯誤。 – Jerm

+0

paintComponent()仍然是組件本身的一部分。線程或計時器用於修改需要繪製的內容,然後調用重繪。這種修改可能是文本的換位,或者可能準備將用於繪畫的圖像柵格([] []像素數組)。修改完成後,您可以在組件上調用repaint()。 –