2012-12-27 42 views
-4

我工作的一些項目,它的一部分創建數字時鐘。我想要這樣的時間:17:3:5會像17:03:05那樣出現。我的代碼在(工作)下面。但一些問題吧:我不知道我是否使用正確的計時器,也是我的節目看起來複雜,也許你可以幫助簡化代碼或給一些想法如何把它寫在一個簡單的方法,謝謝。Java中使用擺動

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Calendar; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.WindowConstants; 

public class SimpleDigitalClock { 

    public static void main(String[] args) { 

     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     DigitalClock myClock = new DigitalClock(); 
     f.add(myClock); 
     f.pack(); 
     f.setVisible(true); 
    } 

    static class DigitalClock extends JPanel { 

     String stringTime; 
     int hour, minute, second; 

     String correctionHour = ""; 
     String correctionMinute = ""; 
     String correctionSecond = ""; 

     public void setStringTime(String xyz) { 
      this.stringTime = xyz; 
     } 

     public int findMinimumBetweenTwoNumbers(int a, int b) { 
      return (a <= b) ? a : b; 
     } 

     DigitalClock() { 

      Timer t1 = new Timer(1000, new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 

        repaint(); 
       } 
      }); 
      t1.start(); 
     } 

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

      Calendar now = Calendar.getInstance(); 
      hour = now.get(Calendar.HOUR_OF_DAY); 
      minute = now.get(Calendar.MINUTE); 
      second = now.get(Calendar.SECOND); 

      if (hour < 10) { 
       this.correctionHour = "0"; 
      } 
      if (hour >= 10) { 
       this.correctionHour = ""; 
      } 

      if (minute < 10) { 
       this.correctionMinute = "0"; 
      } 
      if (minute >= 10) { 
       this.correctionMinute = ""; 
      } 

      if (second < 10) { 
       this.correctionSecond = "0"; 
      } 
      if (second >= 10) { 
       this.correctionSecond = ""; 
      } 
      setStringTime(correctionHour + hour + ":" + correctionMinute+ minute + ":" + correctionSecond + second); 
      g.setColor(Color.BLACK); 
      int length = findMinimumBetweenTwoNumbers(this.getWidth(),this.getHeight()); 
      Font myFont = new Font("SansSerif", Font.PLAIN, length/5); 
      g.setFont(myFont); 
      g.drawString(stringTime, (int) length/6, length/2); 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 

} 
+3

什麼是你的問題? – Doorknob

+2

可能代碼審查在codereview.stackexchange.com中更好? – Val

+1

你可以使用SimpleDateFormat的 「HH:MM:SS」(http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html)格式化你的時間。在1行。 –

回答

3

格式化日期是什麼SimpleDateFormat設計要做到:

DateFormat format = new SimpleDateFormat("hh:mm:ss"); 
String stringTime = format.format(new Date()); 
+0

只是想知道 - 每秒鐘調用新的Date()會不會是一個矯枉過正的行爲? –

+0

比我的回答更優雅的方法。 – MrSmith42

+0

@ TJ-你說得很對。創建對象會降低應用程序的時間和CPU的工作量。垃圾收集和內存回收耗費更多的時間和CPU的工作量。這裏的一種優化可能是每分鐘更新一次,而不是每秒更新一次。 – Reimeus

0

完全修飾類:

static class DigitalClock extends JPanel { 

    String stringTime; 

    public void setStringTime(String xyz) { 
     this.stringTime = xyz; 
    } 

    public int findMinimumBetweenTwoNumbers(int a, int b) { 
     return (a <= b) ? a : b; 
    } 

    DigitalClock() { 

     Timer t1 = new Timer(1000, new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       repaint(); 
      } 
     }); 
     t1.start(); 
    } 

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

     Calendar now = Calendar.getInstance(); 
     int hour = now.get(Calendar.HOUR_OF_DAY); 
     int minute = now.get(Calendar.MINUTE); 
     int second = now.get(Calendar.SECOND); 

     String correctionHour = (hour < 10) ? "0" : "": 
     String correctionMinute = (minute < 10) ? "0" : ""; 
     String correctionSecond = (second < 10) ? "0" : ""; 
     setStringTime(correctionHour + hour + ":" + correctionMinute+ minute + ":" + correctionSecond + second); 
     g.setColor(Color.BLACK); 
     int length = findMinimumBetweenTwoNumbers(this.getWidth(),this.getHeight()); 
     Font myFont = new Font("SansSerif", Font.PLAIN, length/5); 
     g.setFont(myFont); 
     g.drawString(stringTime, (int) length/6, length/2); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200); 
    } 
} 

}

1

其他的答案都談到了其他部分的你碼。

爲中心的顯示,可以使用下面的繪製代碼。

 g.setColor(Color.BLACK); 
     int length = Math.min(this.getWidth(), this.getHeight()); 
     Font myFont = new Font("SansSerif", Font.PLAIN, length/5); 
     g.setFont(myFont); 
     Graphics2D g2d = (Graphics2D) g; 
     FontMetrics fm = g2d.getFontMetrics(); 
     Rectangle2D r = fm.getStringBounds(stringTime, g2d); 
     int x = (this.getWidth() - (int) r.getWidth())/2; 
     int y = this.getHeight()/2; 
     g.drawString(stringTime, x, y);