2012-01-31 33 views
1

我有一個JPanel,它有幾個(25)JLabel s,使用計時器,他們每2秒更換一次顏色(所有面板更改爲相同的顏色),根據隨機順序,我只看到該順序,因爲我在每次更改中添加了文本打印,但是顏色一起改變,即使在每次更改之間添加了延遲(如果我眼睛無法看到漸進的變化),並沒有幫助,他們在更長時間的等待之後一起改變了顏色。製作幾個`JPanel`s一個接一個地改變顏色,而不是全部與Java Swing一起

如何讓它們逐個改變顏色?

代碼:

public class Billboard extends JFrame{ 


    private JPanel board; 
    private ArrayList<Panel> panels; 


    public Billboard() 
    { 
     super("Billboard"); 
     this.board = new JPanel(); 
     setBounds(100,100,250,160); 
     this.panels = new ArrayList<Panel>(0); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container con=this.getContentPane(); // inherit main frame 
     con.add(board); // add the panel to frame 
     //The timer that is responsible to changing the colors 
     ColorGenerator cg = ColorGenerator.getInstance(); 

     RandomNotifier note = new RandomNotifier(); 
     cg.setNotificator(note); 

     JLabel l; 
     Panel p; 
     for (int i=0; i<25; i++) { 
      // create label 
      l= new JLabel ("    "); 
      l.setOpaque(true); 
      // add label to screen 
      board.add(l); 
      // create panel 
      p = new Panel("p" + i,l); 
      // link ColorGenerator to panel 
      cg.addObserver(p); 
      // add panel to panels list 
      panels.add(p); 
     } 
     setVisible(true); // display the frame 
     //starts the timer 
     cg.start(); 
    } 

    public static void main(String args[]) {new Billboard();} 
} 



public class Panel implements Observer{ 

    private String _name; 
    private Color _color; 
    private JLabel _label; 

    public Panel (String name, JLabel l) { 
     this._name = name; 
     this._color= new Color(0,0,0); 
     this._label = l; 
     this._label.setBackground(this._color); 
     checkRep(); 
    } 

    //updates the color of the label 
    public void update(Observable o, Object arg) { 
     ColorGenerator cg = (ColorGenerator) o; 
     setColor(cg.getColor()); 
     this._label.setBackground(this.getColor()); 
     System.out.println(this.getName() + " has changed its color."); 
    } 

    private void setColor(Color c) { 
     this._color = c; 
    } 

    private Color getColor() { 
     return this._color; 
    } 
} 

public class ColorGenerator extends Observable { 

    private Notifier _notifier; 
    private Color _color; 
    private Timer _timer; 
    private ArrayList<Panel> _observers; 

    //hold the link to the only ColorGenerator 
    private static ColorGenerator _cg = null; 

    public static ColorGenerator getInstance() { 
     if (_cg == null) { 
      _cg = new ColorGenerator(); 
     } 
     return _cg; 
    } 

    protected ColorGenerator() { 
     ActionListener changeColorTask = new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       Random rand_gen = new Random(); 
       _color = new Color (rand_gen.nextInt(256),rand_gen.nextInt(256),rand_gen.nextInt(256)); 
       setChanged(); 
       notifyObservers(); 
      } 
     }; 
     this._color = new Color(0,0,0); 
     this._timer = new Timer(2000, changeColorTask); 
     this._timer.setInitialDelay(0); 
     this._observers = new ArrayList<Panel>(); 
     this._notifier = null; 
    } 

    public void start() { 
     this._timer.start(); 
    } 


    public Color getColor() { 
     return this._color; 
    } 

    @Override 
    public void addObserver(Observer o) { 
      //... 
    } 

    @Override 
    public void deleteObserver(Observer o) { 
     //... 
    } 

    @Override 
    public void deleteObservers() { 
      //... 
    } 

    @Override 
    public int countObservers() { 
     //... 
    } 

    @Override 
    public void notifyObservers(){ 
     if (!hasChanged()) 
      return; 
     if (this._notifier == null) { 
      System .out.println ("Notificator has not set for ColorGenerator, can't notify observers"); 
      return; 
     } 
     this._notifier.notifyAll(this,this._observers); 
     clearChanged(); 
    } 

    public void setNotifier (Notifier n) { 
     if (n == null) 
      return; 
     this._notifier = n; 
    } 
} 
+0

請向我們展示ColorGenerator的代碼。 – 2012-01-31 15:44:53

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-01-31 15:55:29

+0

RandomNotifier是什麼樣的? – 2012-01-31 16:52:18

回答

1

你需要調用repaint()上的JFrame或JPanel的,當你想的顏色改變。

相關問題