1

我有 5 个 JButton: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(236,233,216) 替换Color.GREY - RGB 为灰色,但它仍然不起作用。

4

4 回答 4

2

如果您确实需要为按钮着色,然后将颜色设置回其原始默认状态(系统灰色),请使用

button.setBackground(null);

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

(我有一个应用程序,我需要单击几个按钮,跟踪我单击了哪些按钮,然后当我执行一个功能时,我“取消单击”它们。我可以使用切换按钮,但是这一行更改为添加此功能比更改我的整个组件数组更容易。另外,UI“感觉”是正确的。)

于 2012-09-24T15:12:21.320 回答
1

请解释你到底想做什么。

根据您所写的内容,我了解到您试图一次只选择一个按钮。如果是这样,请将您的 JButtons 替换为 JToggleButtons 并将它们放在一个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());
}
于 2010-04-11T18:09:28.753 回答
0

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

Color buttonColor = b1.getBackground();

后来,在 ChangeInUsersList 函数中,我用 buttonColor 替换了“Color.GRAY”。它奏效了:)

于 2010-04-11T17:40:52.763 回答
0

您的按钮上同时需要setBackground(), setContentAreaFilled(false), 。setOpaque(true)

于 2017-03-23T08:23:02.337 回答