Java 停止按钮在swing中单击时不会突出显示

Java 停止按钮在swing中单击时不会突出显示,java,swing,user-interface,java-8,awt,Java,Swing,User Interface,Java 8,Awt,我试图让我的JButton在单击时停止背景更改。我一直在阅读和测试其他人的答案,但没有人能帮到我。我对Java非常陌生,所以具体的细节会很有帮助,下面是我的代码和正在发生的事情的演示。另外,如果这有什么关系的话,我正在运行ubuntu 注意:如果我按住鼠标并将鼠标悬停在上面,它也会触发背景变化 import javax.swing.*; import java.awt.*; public class RaisedButton extends JButton{ private Stri

我试图让我的JButton在单击时停止背景更改。我一直在阅读和测试其他人的答案,但没有人能帮到我。我对Java非常陌生,所以具体的细节会很有帮助,下面是我的代码和正在发生的事情的演示。另外,如果这有什么关系的话,我正在运行ubuntu

注意:如果我按住鼠标并将鼠标悬停在上面,它也会触发背景变化

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;
        }
    }
}
演示正在发生的事情


谢谢

在您的
foundations()
方法中,更改

this.setRolloverEnabled(true);
要将其设置为false,请执行以下操作:

this.setRolloverEnabled(false);
或者删除分配以回退到默认值
false


来自JavaDoc for

的详细信息请尝试将其放入RaisedButton构造函数中:

    setUI(new BasicButtonUI());
添加使用
setModel(新的FixedStateButtonModel())
foundations()

更改了它,但问题仍然存在。从javadoc中可以看到:“某些外观可能无法实现滚动效果;它们将忽略此属性。”是否尝试设置自定义?谢谢!我试过了,这次似乎奏效了。我以前的尝试一定是做错了什么。