gpt4 book ai didi

java - 使用 Java Swing 使多个 `JPanel` 的颜色一一改变,而不是全部改变

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:25 25 4
gpt4 key购买 nike

我有一个 JPanel,上面有几个 (25) JLabel,使用计时器,它们每 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;
}
}

最佳答案

当您想要更改颜色时,需要在 JFrame 或 JPanel 上调用 repaint()

关于java - 使用 Java Swing 使多个 `JPanel` 的颜色一一改变,而不是全部改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com