如何让我的JFrame背景大幅改变颜色? [英] How can I make my JFrame background change colors wildly?

查看:112
本文介绍了如何让我的JFrame背景大幅改变颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a "GUI from Hell", and I'm trying to make the JFrame flash colors (change background rapidly) for a time long enough to be annoying. This is what I've got:

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及其所有内容都会改变颜色(不仅仅是背景),这将是疯狂和令人敬畏的,所以让我们继续。

Note: If you know how to easily make the entire JFrame and all of its contents change color (not just the background), that would be crazy and awesome, so let us in on that.

任何指导都表示赞赏!希望我不仅仅错过了一些愚蠢的东西......

Any guidance is appreciated! Hopefully I haven't just missed something silly...

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

推荐答案

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

Here's my take on your GUI from hell which seems to work OK. It's pretty intense. How are you performing the updates? On another thread?

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();

这篇关于如何让我的JFrame背景大幅改变颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆