我有一个JPanel
,上面有几个(25)JLabel
s,使用计时器,他们每2秒更换一次颜色(所有面板都变为相同的颜色),根据随机顺序,我只看到那个顺序因为我在每次更改时添加了文字打印,但颜色一起变化,即使我在每次更改之间添加了延迟(如果我的眼睛无法看到渐变)它没有帮助,经过漫长的等待后,它们都变色了。
如何让它们逐个改变颜色?
代码:
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 :(得分:1)
当您想要更改颜色时,需要在JFrame或JPanel上调用repaint()
。