如何让我的JFrame背景变色?

时间:2012-03-21 00:01:57

标签: java swing

我正在制作一个“来自地狱的GUI”,我正在努力使JFrame闪烁颜色(快速更改背景)一段时间足以令人讨厌。这就是我所拥有的:

int changes = gen.nextInt(2000) + 5000;
int red;
int green;
int blue;
Color color;

for (int i = 0; i < changes; i++)
{   
    color = new Color(gen.nextInt(256), gen.nextInt(256),
    gen.nextInt(256));
    // I first tried this...
    frameMain.getContentPane().setBackground(color);
    // Then I tried this, which only
    // appeared to change the color once and then proclaim
    // that it was done:
    panel1.setBackground(color);
    panel2.setBackground(color);
    panel3.setBackground(color);
}

注意:如果你知道如何轻松地让整个JFrame及其所有内容改变颜色(不仅仅是背景),那将是疯狂和令人敬畏的,所以让我们继续。

任何指导表示赞赏!希望我没有错过任何愚蠢的事情......

...如果您对一个荒谬的GUI效果有一两个想法,请随时分享! :)

3 个答案:

答案 0 :(得分:3)

这是我对你的GUI的看法,似乎工作正常。这非常激烈。你是如何进行更新的?在另一个线程?

final JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.getContentPane().setLayout(new GridLayout(3, 1, 20, 20));
final JPanel[] panels = new JPanel[3];
for (int i = 0; i < panels.length; i++) {
    panels[i] = new JPanel();
    panels[i].setOpaque(true);
    frame.getContentPane().add(panels[i]);
}
frame.setVisible(true);
ActionListener action = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Random gen = new Random();

        Color color = new Color(gen.nextInt(256), gen.nextInt(256),
                gen.nextInt(256));
        frame.getContentPane().setBackground(color);

        for (int i = 0; i < panels.length; i++) {
            color = new Color(gen.nextInt(256), gen.nextInt(256),
                    gen.nextInt(256));
            panels[i].setBackground(color);
        }
    }
};

Timer t = new Timer(100, action);
t.setRepeats(true);
t.start();

答案 1 :(得分:2)

我建议将Swing Timer用于Swing GUI的重复事件,也许this example可以帮助您实现另一个梦想

JFrame无法上色,但适用于ContentPane g.e。JFrame.getContentPane.setColor(Color.red)

答案 2 :(得分:2)

最后,有趣的事情......试一试。像任何JFrame一样使用它。

class JFrameWild extends JFrame {
private static final long serialVersionUID = 666L;
public JFrameWild(String string) {
    super(string);
    Thread thread = new Thread(new Runnable() {
        public void run() {
            while (true) {
                yoyoMama(JFrameWild.this);
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    thread.setDaemon(true);
    thread.start();
}
private void yoyoMama(Object object) {
    if (object instanceof Container) {
        Container c = (Container) object;
        Component[] components = c.getComponents();
        for (Component component : components) {
            yoyoMama(component);
            // put extra "wild" stuff here
            component.setBackground((new Color((int) (Math.random() * (double) (0xFFFFFF)))));
        }
    } else {
        if (object instanceof Component) {
            Component component = (Component) object;
            // put extra "wild" stuff here
            component.setBackground((new Color((int) (Math.random() * (double) (0xFFFFFF)))));
        }
    }
}
}