【问题标题】:How can I make my JFrame background change colors wildly?我怎样才能让我的 JFrame 背景疯狂地改变颜色?
【发布时间】:2017-12-29 06:20:33
【问题描述】:

我正在制作一个“来自地狱的 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 效果有一两个想法,请随时分享! :)

【问题讨论】:

标签: java swing


【解决方案1】:

这是我对你的 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();

【讨论】:

  • 这很好用。谢谢! :D 我最初没有使用线程,但现在我要让我的所有四个控件都使用它们。
【解决方案2】:

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

JFrame 不能着色,但适用于 ContentPane g.e.JFrame.getContentPane.setColor(Color.red)

【讨论】:

  • 感谢您的链接!我确实知道第二件事。 :)
【解决方案3】:

终于来点好玩的了……试试看吧。像使用任何 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)))));
        }
    }
}
}

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2017-08-12
    • 2019-08-05
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多