简体   繁体   English

按钮背景颜色更改不起作用

[英]Button background color change not working

I have a problem with the background color of a button. 我的按钮背景色有问题。

When clicked, it changes to a color that I didn't specify in my code. 单击后,它会变为我在代码中未指定的颜色。 When I move my cursor away from the button (still holding the mouse button) it changes color to the one I defined in the code. 当我将光标移离按钮(仍然按住鼠标按钮)时,它的颜色将更改为我在代码中定义的颜色。

I am changing color with the following ChangeListener method: 我正在使用以下ChangeListener方法更改颜色:

btn1.getModel().addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();

        if (model.isRollover()) {
            btn1.setBackground(new Color(166,166, 166));
        } else if (model.isPressed()) {
            btn1.setBackground(new Color(255, 64, 0));
        } else {
            btn1.setBackground(new Color(201, 201, 201));
        }
    }
});

Here is a screen shot of all situations: 这是所有情况的屏幕截图:

  1. Normal 正常
  2. Hover 徘徊
  3. Clicked (bug) 已点击(错误)
  4. Clicked and moved cursor away from button 单击并将光标从按钮移开

Pressing the mouse on top of a button makes the model both armed and pressed. 按下按钮上方的鼠标会使模型处于待命状态和按下状态。 As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. 只要鼠标保持按下状态,即使鼠标移到按钮外部,模型也会保持按下状态。 On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button. 相反,仅在鼠标保持在按钮范围之内时,模型才处于设防状态。 ButtonModel 按钮模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM