2014-01-05 35 views
1

我試圖做一個數字時鐘,然後添加到JPanel,現在,當我打電話DrawStringtaskPerformer方法,它吸引,但裏面的時候,它不會繪製字符串!爲什麼會這樣,我該如何去實施這個課程,以便每一秒重繪?更新的DrawString在一定的時間間隔

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;  
import javax.swing.JComponent; 
import javax.swing.JPanel; 
import java.util.Date; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 
import javax.swing.Timer; 

class DrawDate extends JPanel { 
public Date ddate; 
public Calendar ccalendar = new GregorianCalendar(); 
Date time; 

public DrawDate() { 
    this.ddate = new Date(); 
    time = this.ccalendar.getTime(); 
} 

public void paint(Graphics g) { 
final Graphics2D g2 = (Graphics2D)g; 
Font font = new Font("Arial", Font.PLAIN, 50); 
g2.setFont(font); 
//THIS DRAWS THE STRING 
g2.drawString(time.toString() , 100, 100); 

ActionListener taskPerformer = new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
     Calendar ccalendar = new GregorianCalendar(); 
     Date time = ccalendar.getTime(); 
      //THIS DOES NOT DRAW A STRING. 
      g2.drawString(time.toString() , 200, 100); 

    } 
}; 
final Timer timer = new Timer(1000, taskPerformer); 
timer.start(); 


} 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    super.setBackground(new Color(100,100,100)); 
    paint(g); 
} 

} 
+0

http://www.oracle.com/technetwork/java/painting-140037.html?printOnly=1可能是值得一讀 – 2014-01-05 09:44:53

回答

4

基本上,你有一個上下文問題。 g2ActionListener內沒有上下文,但也有需要首先要解決的其他問題......

不要創建paint方法中的ActionListenerTimer,其實你應該避免壓倒一切paint可言。

paint是假設調用paintComponent,而是因爲你已經忘了叫super.paint,不會出現這種情況,因此您paintComponent方法永遠不會被調用。但是,你應該避免從paintComponent方法

而是內調用paint,在類似的構造函數創建TimerActionListener。當Timer滴答/觸發actionPerformed方法時,只需更新要繪製的值,然後致電repaint

paintComponent方法中,進行實際的渲染。

看看Performing Custom PaintingPainting in AWT and Swing更多細節

+0

奇怪的是,原來的代碼重寫了這兩種方法,並在' paintComponent(..)'它通過設置BG顏色導致無限循環..當然,OP應該通過你添加的兩個鏈接。 –

+1

@AndrewThompson問題是,他們從來沒有調用super.paint,因此paintComponent不會被調用......尚未... – MadProgrammer

+0

糟糕!我*表示*寫*「..它**會通過設置BG顏色導致無限循環」* - 這是我的故事,我堅持。 ;) –

3

在代碼中的註釋看到的提示..

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Date; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 

class DrawDate extends JPanel { 

    public Date ddate; 
    public Calendar ccalendar = new GregorianCalendar(); 
    Date time; 

    public DrawDate() { 
     this.ddate = new Date(); 
     time = this.ccalendar.getTime(); 

     ActionListener taskPerformer = new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        Calendar ccalendar = new GregorianCalendar(); 
        Date time = ccalendar.getTime(); 
        //THIS DOES DRAW A STRING. 
        repaint(); 
      } 
     }; 
     final Timer timer = new Timer(1000, taskPerformer); 
     timer.start(); 

     // this will invoke repaint() 
     // if called within a paint/paintComponent - infinite loop 
     super.setBackground(new Color(100,100,100)); 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     final Graphics2D g2 = (Graphics2D)g; 
     Font font = new Font("Arial", Font.PLAIN, 50); 
     g2.setFont(font); 

     // update the time 
     time = new Date(); 
     //THIS DRAWS THE STRING 
     g2.drawString(time.toString() , 10, 100); 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(600,150); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       JOptionPane.showMessageDialog(null, new DrawDate()); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
3

試試這個:

datePanel.add(新ClockPane( ));

public class ClockPane extends JPanel { 

     private JLabel clock; 

     public ClockPane() { 
      setLayout(new BorderLayout()); 
      clock = new JLabel(); 
      clock.setHorizontalAlignment(JLabel.CENTER); 
      clock.setFont(UIManager.getFont("Label.font").deriveFont(Font.BOLD, 32f)); 
      tickTock(); 
      add(clock); 

      Timer timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        tickTock(); 
       } 
      }); 
      timer.setRepeats(true); 
      timer.setCoalesce(true); 
      timer.setInitialDelay(0); 
      timer.start(); 
     } 

     public void tickTock() { 
      clock.setText(DateFormat.getDateTimeInstance().format(new Date())); 
     } 
    } 
+0

我喜歡使用JLabel ... – MadProgrammer

+0

感謝您的讚賞 – Aarav