单击时更改javafx按钮颜色? [英] Changing javafx button color on click?

查看:1012
本文介绍了单击时更改javafx按钮颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用按下的伪选择器设置颜色:

I know I can set a color by using the pressed pseudo selector:

myButton:pressed{}

问题是,我试图在代码中通过覆盖样式表中的css背景颜色来执行此操作:

Problem is, im trying to do this in code by overriding the css background color from my stylesheet by doing:

myButton.setStyle("fx-background-color: #FFF");

后者失败确实会改变颜色。可能是因为一旦我设置我的样式表,我不能覆盖它?如何在点击时更改按钮颜色?

The latter fails do change the color though. Could it be that once ive set my stylesheet that I cant override it? How can I change the button color on click?

推荐答案

我必须做类似的事情(这里是简化的,只有将样式更改为按钮的代码部分)我做了这个,我希望它对你有所帮助

I had to do a similar thing(here is simplified, there is just the part of code which change the style to the button) and i did this, i hope it will be helpful to you

button.setOnAction((ActionEvent e) -> {
    button.getStyleClass().removeAll("addBobOk, focus"); 
    //In this way you're sure you have no styles applied to your object button
    button.getStyleClass().add("addBobOk");
    //then you specify the class you would give to the button
});

CSS:

.addBobOk{
        -fx-background-color:#90EE90;
        -fx-background-radius: 5,5,4;
        -fx-background-insets: 0 0 -1 0,0,1;
        -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
        -fx-text-alignment: center;
}
.addBobOk:hover{

        -fx-background-color:#64EE64;
        -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
        -fx-text-alignment: center;
}
.busy{

        -fx-background-color:#B3B3B3; 
        -fx-text-alignment: center;
}
.busy:hover{

        -fx-background-color:cdcbcb;                       
        -fx-text-alignment: center;
}

这篇关于单击时更改javafx按钮颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆