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

标签 java swing jlabel

我有一个 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/

相关文章:

java - 在 Java Swing 中换行文本

MS Paint 中 TextBox 的 java 代码

java - 如何在处理结束时显示 "Loading..."消息自动关闭(类似于Progress Bar)?

java - 使用以前的方法答案调用方法

java - Java API 中是否有 LayoutManager 允许我这样做?

java - 在处理中创建第二个小程序(窗口)

java - 无法覆盖 Nimbus 属性

java - Jsoup 可以模拟按钮按下吗?

java - 如何管理 Wordle Game (JavaFx) 中的重复字母?

java - 如何将ImageIcon 添加到JDialog 中?