Java 设置Jbutton的背景

Java 设置Jbutton的背景,java,swing,jbutton,Java,Swing,Jbutton,我有5个按钮:b1、b2、b3、b4、b5。 默认情况下,它们的颜色为灰色。 当我点击任何按钮时,该按钮的背景变为白色。 当我单击另一个按钮时,我希望上一个单击的按钮将其背景更改为灰色,而这个新单击的按钮将其背景更改为白色。以下是我编写的代码: int liveButton = 0; //holds the value of the button that is last clicked. //0 indicates no button clicked (in the beginning) p

我有5个按钮:b1、b2、b3、b4、b5。 默认情况下,它们的颜色为灰色。 当我点击任何按钮时,该按钮的背景变为白色。 当我单击另一个按钮时,我希望上一个单击的按钮将其背景更改为灰色,而这个新单击的按钮将其背景更改为白色。以下是我编写的代码:

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

然而,它并没有像预期的那样工作。当我点击一个按钮时,它的背景会变成白色。但是,如果我在那之后单击其他按钮,前一个按钮的背景不会变为灰色。我尝试用新java.awt.Color(236233216)替换Color.GREY——灰色的rgb,但仍然不起作用。

我通过在声明“liveButton”变量后添加以下行来修复它:

Color buttonColor = b1.getBackground();
后来,在ChangeInUserList函数中,我用buttonColor替换了“Color.GRAY”。
成功了:)

请解释一下你到底想做什么

根据你所写的,我推测你试图一次只选择一个按钮。如果是这样,用JToggleButtons替换JButtons,并将它们放在一个按钮中。 例如(伪代码):

否则,如果确实要更改按钮的背景色:

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());
}
私有列表按钮;
专用按钮b1、b2、b3、b4、b5;
私有void initButtons()
{
buttons=new ArrayList(5);//要在中“保存”按钮的新列表
add(b1=newjbutton());
//等等等等。。。
add(b5=newjbutton());
}
公共无效setActiveButton(JButton按钮)
{
用于(JButton b:按钮)
{
b、 setBackgroundColor(颜色:灰色);
} 
按钮。setBackgroundColor(颜色。白色);
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt)
{
设置活动按钮(b1);
//或者更“通用”
//setActiveButton((JButton)evt.getSource();
}
如果确实需要为按钮上色,然后将颜色设置回其原始默认状态(系统灰色),请使用

这将删除以前的任何颜色设置


(我有一个应用程序,我需要点击几个按钮,跟踪我点击了哪些按钮,然后当我执行一个功能时,我会“松开”它们。我本可以使用切换按钮,但这一行更改来添加此功能比更改整个组件阵列更容易。此外,UI“感觉”是正确的。)您需要在按钮上同时使用
setBackground()
设置内容区域填充(false)
设置不透明(true)

您是否试图模拟按钮组的功能?e、 g.只能选择/切换一个按钮?顺便说一句:用java.awt.Color.whiteyes替换新的java.awt.Color(255255)!但我没有意识到ButtonGroup的存在。我假设buttongroup只用于单选按钮。是的,我一次只选择一个按钮。我想要的是点击的按钮被突出显示。
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());
}
button.setBackground(null);