2012-03-21 30 views
3

我正在製作一個「來自地獄的圖形用戶界面」,並且我試圖讓JFrame的Flash顏色(快速更換背景)持續足夠長的時間來煩人。這是我的本錢:如何讓我的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效果,隨時分享! :)

+1

只是爲了確保你把這個考慮:http://en.wikipedia.org/wiki/Photosensitive_epilepsy – JRL 2012-03-21 00:12:46

+3

+1從地獄設計GUI。 – Adam 2012-03-21 00:12:47

+0

謝謝,@亞當:) – 2012-03-21 01:51:40

回答

3

這是我從地獄裏拿你的圖形界面看起來工作正常。這非常激烈。你如何執行更新?在另一個線程?

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

這工作偉大。謝謝! :D我最初並沒有使用線程,但現在我要讓所有四個控件都使用它們。 – 2012-03-21 03:59:22

2

我建議使用Swing Timer用於重複事件的Swing GUI的,也許this example可以幫助您與您的另一個夢想

JFrame是不可能的着色,但適用於ContentPane G.E. JFrame.getContentPane.setColor(Color.red)

+0

感謝您的鏈接!我確實知道第二件事。 :) – 2012-03-21 01:38:47

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))))); 
     } 
    } 
} 
} 
+0

哈哈瘋狂:)謝謝! – 2012-03-21 03:40:59