2010-04-11 59 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)