2013-04-25 69 views
1

我想爲我的JFrame添加一個顏色指示器。當我點擊一個按鈕時,它應該變成紅色,如果我不點擊一個按鈕,它應該是綠色的。我應該如何實現這一點?如何將顏色指示器(LED外觀)添加到JFrame?

+0

,您可以添加一個JLabel,你想在JFrame的頂部添加,在裝修? - 靠近最小化和最大化按鈕? – 2013-04-25 12:57:35

+0

你想要在標題中嗎?你想要什麼作爲一個指標?更具體的 – 2013-04-25 13:04:10

+0

我希望它在jframe中的任何位置,非常小可以是圓形的。 – RSF 2013-04-25 13:14:04

回答

8
JLabel lblLed = new JLabel("•"); 
lblLed.setForeground(Color.Green); 

一個的ChangeListener添加到您的JButton,並在StateChanged()方法補充一點:

if (buttonIsPressed) { 
    lblLed.setForeground(Color.Red); 
} else { 
    lblLed.setForeground(Color.Green); 
} 
在Jframe的內容
+1

unicode +1;另見['ColorIcon'](http://stackoverflow.com/a/3072979/230513)。 – trashgod 2013-04-25 17:25:56