2011-12-12 69 views
1

我正在爲一所學校的項目工作,而我很困難。我已經寫了一個程序顯示當前時間,但我無法弄清楚如何隨着時間的變化來更新顯示。如果有人能幫助我,或者指引我走向某個方向,那麼您的幫助將不勝感激。我將發佈我在下面寫的內容。爪哇獲取時鐘更新

import javax.swing.*; 
import java.awt.*; 
import java.util.*; 


public class Project2 extends JFrame{ 
public static void main(String[] args){ 
    Project2 myFrame = new Project2(); 
    myFrame.pack(); 
    myFrame.setTitle("Digital Clock"); 
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    myFrame.setLocationRelativeTo(null); 
    myFrame.setVisible(true); 


}//main() 

public Project2(){ 
System.out.println(currentTime()); 
JPanel p1 = new JPanel(); 
p1.setLayout(new FlowLayout()); 
JLabel time = new JLabel(currentTime()); 
time.setFont(new Font("TimesRoman", Font.BOLD, 20)); 
time.setForeground(Color.blue); 
p1.add(time); 
this.setLayout(new BorderLayout()); 
this.add(p1, BorderLayout.CENTER); 

} 

public String currentTime(){ 
    Calendar calendar = Calendar.getInstance(); 
    int hours = calendar.get(Calendar.HOUR_OF_DAY); 
    int minutes = calendar.get(Calendar.MINUTE); 
    int seconds = calendar.get(Calendar.SECOND); 
    int aP = calendar.get(Calendar.AM_PM); 
    String currentTime = hours+":"+checkTime(minutes)+":"+checkTime(seconds)+" "+amP(aP); 
    return currentTime; 
} 

public String checkTime(int t){ 
    String time1; 
    if (t < 10){ 
     time1 = ("0"+t); 
     } 
    else{ 
     time1 = (""+t); 
     } 
    return time1; 
} 

public String amP(int ap){ 
    String amPm; 
    if(ap == 0) 
     amPm = "AM"; 
    else 
     amPm = "PM"; 
    return amPm; 
} 



}//Project2 

回答

1

提示:看一看TimerTimerTask類。然後使用這些類來更新JLabel的文本。

+0

我可以有另一個提示什麼的,我現在很失落。我看了一下Timer類,但我不確定如何實現到我的程序中。 – Gnosis

+0

-1在Swing中使用的Timer是javax.swing.Timer,而不是util中的Timer – kleopatra

1

聲明的JPanel和JLabel的全局變量,並在 構造

ActionListener taskPerformer = new ActionListener() { 
       public void actionPerformed(ActionEvent evt) { 
        System.out.println(currentTime()); 
        time.setText(currentTime()); 
       } 
      }; 
      Timer t = new Timer(1000, taskPerformer); 
      t.start(); 

這裏的末尾添加以下行是完整的代碼:

import javax.swing.*; 
import javax.swing.Timer; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.*; 

public class DigitalClock extends JFrame { 

    private JPanel p1; 
    private JLabel time; 

    public static void main(String[] args) { 
     DigitalClock myFrame = new DigitalClock(); 
     myFrame.pack(); 
     myFrame.setTitle("Digital Clock"); 
     myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     myFrame.setLocationRelativeTo(null); 
     myFrame.setVisible(true); 

    }// main() 

    public DigitalClock() { 
     System.out.println(currentTime()); 
     p1 = new JPanel(); 
     p1.setLayout(new FlowLayout()); 
     time = new JLabel(currentTime()); 
     time.setFont(new Font("TimesRoman", Font.BOLD, 20)); 
     time.setForeground(Color.blue); 
     p1.add(time); 
     this.setLayout(new BorderLayout()); 
     this.add(p1, BorderLayout.CENTER); 
     ActionListener taskPerformer = new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       System.out.println(currentTime()); 
       time.setText(currentTime()); 
      } 
     }; 
     Timer t = new Timer(1000, taskPerformer); 
     t.start(); 
    } 

    public String currentTime() { 
     Calendar calendar = Calendar.getInstance(); 
     int hours = calendar.get(Calendar.HOUR_OF_DAY); 
     int minutes = calendar.get(Calendar.MINUTE); 
     int seconds = calendar.get(Calendar.SECOND); 
     int aP = calendar.get(Calendar.AM_PM); 
     String currentTime = hours + ":" + checkTime(minutes) + ":" 
       + checkTime(seconds) + " " + amP(aP); 
     return currentTime; 
    } 

    public String checkTime(int t) { 
     String time1; 
     if (t < 10) { 
      time1 = ("0" + t); 
     } else { 
      time1 = ("" + t); 
     } 
     return time1; 
    } 

    public String amP(int ap) { 
     String amPm; 
     if (ap == 0) 
      amPm = "AM"; 
     else 
      amPm = "PM"; 
     return amPm; 
    } 

}// Project2 
+0

我不能做正確的事情,因爲我在嘗試編譯時遇到錯誤。它說錯誤:預計t.start(); – Gnosis

+0

你能發佈你的最新代碼嗎? – Pradeep

+0

現在它回到了我第一次發佈時的狀態,與其他人建議的一些事情捆綁在一起,但是那不讓我在哪裏。 – Gnosis

0

我與普拉迪普同意,你必須使用一個定時器,例如你可以定義一個定時器,並使該方法每1秒鐘呼叫一次,在這種方法中你可以更新顯示的時間...