如何让我的JFrame背景大幅改变颜色?

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

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

我建议使用Swing Timer重复事件到Swing GUI,也许这个例子可以帮助你实现另一个你的梦想

JFrame无法着色,但适用于ContentPane ge JFrame.getContentPane.setColor(Color.red)

最后,有趣的事情……试一试。 像任何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))))); } } } }