简体   繁体   English

在挥杆中单击时停止按钮突出显示

[英]Stop button from being highlighted when clicked in swing

I trying to get my JButton to stop its background change when clicked. 我试图让我的JButton在点击时停止其背景更改。 I have been reading and testing answers from other questions like such but none have helped me. 我一直在阅读和其他测试回答问题,这样的 ,但没有帮助我。 I am very new to Java so specific details would be helpful, here is my code and a demonstration of what is happening. 我是Java的新手,所以具体的细节会有所帮助,这是我的代码和正在发生的事情的演示。 Also, I am running ubuntu if that matters at all. 另外,如果重要的话,我正在运行ubuntu。

Note: if I hold my mouse down and hover over it, it will also trigger the background change. 注意:如果我按住鼠标并将鼠标悬停在它上面,它也会触发背景更改。

import javax.swing.*;
import java.awt.*;

public class RaisedButton extends JButton{

    private String      text     =     "";
    private String      type     =     "default";


    public RaisedButton(String text, String type) {
        this.text = text;
        this.type = type;
        __init__();
    }

    private void foundations() {
        NotoFont noto = new NotoFont(true);
        this.setText(this.text.toUpperCase());
        this.setRolloverEnabled(false);
        this.setFocusPainted(false);
        this.setBorderPainted(false);
        this.setFont(noto.get());
        this.setPreferredSize(new Dimension(108 + this.text.length() * 4, 37));
    }

    private void default_type() {
        this.foundations();
        this.setBackground(Color.decode("#FFFFFFF"));
        this.setForeground(Color.decode("#000000"));
    }

    private void primary_type() {
        this.foundations();
        this.setBackground(Color.decode("#00BCD4"));
        this.setForeground(Color.decode("#FFFFFF"));
    }

    private void secondary_type() {
        this.foundations();
        this.setBackground(Color.decode("#FF4081"));
        this.setForeground(Color.decode("#FFFFFF"));
    }

    private void disabled_type() {
        this.foundations();
        this.setBackground(Color.decode("#E5E5E5"));
        this.setForeground(Color.decode("#B2A4A4"));
    }

    private void __init__() {
        switch(this.type.toLowerCase()) {
            case "default":
                default_type();
                break;
            case "primary":
                primary_type();
                break;
            case "secondary":
                secondary_type();
                break;
            case "disabled":
                disabled_type();
                break;
        }
    }
}

Demonstration of what's occurring... 展示正在发生的事情......

示范

Thanks! 谢谢!

Use FixedStateButtonModel by adding setModel(new FixedStateButtonModel()); 通过添加setModel(new FixedStateButtonModel());使用FixedStateButtonModel setModel(new FixedStateButtonModel()); to foundations() . foundations()

尝试将它放在RaisedButton构造函数中:

    setUI(new BasicButtonUI());

Within you foundations() method, either change 在你的foundations()方法中,要么改变

this.setRolloverEnabled(true);

to set it false as: 设置为false为:

this.setRolloverEnabled(false);

or remove the assignment to fallback to default value false 或删除分配以回退到默认值false

Details from the JavaDoc for setRolloverEnabled 来自JavaDoc for setRolloverEnabled详细信息

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

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