2010-04-11 61 views
1

我有5個JButton:b1,b2,b3,b4,b5。 默認情況下,它們的顏色是灰色的。 當我點擊任何按鈕時,該按鈕的背景變成白色。 當我點擊另一個按鈕時,我希望先前單擊的按鈕將其背景更改爲灰色,並且此新近單擊的按鈕將其背景更改爲白色。這裏是我寫的代碼:設置Jbutton的背景

int liveButton = 0; //holds the value of the button that is last clicked. 
//0 indicates no button clicked (in the beginning) 

private void ChangeInUsersList(int clickedButton) { 
    switch(liveButton) { 
     case 1 : b1.setBackground(Color.GRAY); 
       break; 
     case 2 : b2.setBackground(Color.GRAY); 
       break; 
     case 3 : b3.setBackground(Color.GRAY); 
       break; 
     case 4 : b4.setBackground(Color.GRAY); 
       break; 
     case 5 : b5.setBackground(Color.GRAY); 
       break; 
     default: System.out.println("No button to change"); 
    } 
    liveButton = clickedButton;// store the clicked button to change its 
    //background later 
} 
private void b1ActionPerformed(java.awt.event.ActionEvent evt) { 
    ChangeInUsersList(1); 
    b1.setBackground(new java.awt.Color(255,255,255)); 
} 

private void b2ActionPerformed(java.awt.event.ActionEvent evt) { 
    ChangeInUsersList(2); 
    b2.setBackground(new java.awt.Color(255,255,255)); 
} 

private void b3ActionPerformed(java.awt.event.ActionEvent evt) { 
    ChangeInUsersList(3); 
    b3.setBackground(new java.awt.Color(255,255,255)); 
} 

private void b4ActionPerformed(java.awt.event.ActionEvent evt) { 
    ChangeInUsersList(4); 
    b4.setBackground(new java.awt.Color(255,255,255)); 
} 

private void b5ButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    ChangeInUsersList(5); 
    b5.setBackground(new java.awt.Color(255,255,255)); 
} 

但是,它沒有按預期工作。當我點擊一個按鈕時,它的背景會變成白色。但是,如果在此之後點擊其他按鈕,則前一個按鈕的背景不會變爲灰色。我嘗試更換Color.GREY新java.awt.Color(236,233,216) - rgb爲灰色但它仍然無法正常工作。

+0

你想模擬一個ButtonGroup的功能嗎?例如只有一個按鈕可以選擇/切換? btw:用java.awt.Color.white替換new java.awt.Color(255,255,255) – Tedil 2010-04-11 17:41:10

+0

yes!但我並沒有意識到ButtonGroup的存在。我認爲buttongroup只適用於單選按鈕。 – mithun1538 2010-04-12 20:22:24

回答

0

我固定它通過聲明 「liveButton」 可變後添加下列行:

Color buttonColor = b1.getBackground(); 

後來,ChangeInUsersList函數內部,我取代 「Color.GRAY」 與buttonColor。 它的工作:)

1

請解釋你到底想做什麼。

從你寫的我收集你試圖一次只選擇一個按鈕。如果是這樣用JToggleButtons替換你的JButtons並把它們放在一個ButtonGroup。 例如(僞代碼):

//[...] 
JToggleButton button2 = new JToggleButton(...) 
//[...] 
ButtonGroup group = new ButtonGroup(); 
//[...] 
group.add(button2); 
//[...] 

不然,如果你真的想改變按鈕的背景顏色:

private List<JButton> buttons; 
private JButton b1, b2, b3, b4, b5; 
private void initButtons() 
{ 
    buttons = new ArrayList<JButton>(5); // new List to "save" Buttons in 
    buttons.add(b1 = new JButton()); 
    // etc etc ... 
    buttons.add(b5 = new JButton()); 
} 

public void setActiveButton(JButton button) 
{ 
    for(JButton b : buttons) 
    { 
     b.setBackgroundColor(Color.GREY); 
    } 
    button.setBackgroundColor(Color.WHITE); 
} 

private void b1ActionPerformed(java.awt.event.ActionEvent evt) 
{ 
    setActiveButton(b1); 
    // or to be more "generic" 
    // setActiveButton((JButton) evt.getSource()); 
} 
+0

是的,我一次只選擇一個按鈕。我想要的是被點擊的按鈕被突出顯示。 – mithun1538 2010-04-12 20:21:43

2

如果你需要顏色的按鈕,然後設置顏色回到其原始默認狀態(系統灰色),使用

button.setBackground(null); 

這將刪除以前的任何顏色設置。我有一個應用程序,我需要點擊幾個按鈕,跟蹤我點擊了哪些,然後當我執行了一個功能,我「取消」它們。我可以使用切換按鈕,但是這個一行更改添加此功能比更改我的整個組件陣列更容易,另外,用戶界面「感覺」是正確的。)

0

您需要按鈕上的setBackground()setContentAreaFilled(false),setOpaque(true)