简体   繁体   English

JavaFX ToggleButton阴影

[英]JavaFX ToggleButton Shadow

Hello Fellow StackOverflow Users, this is my first post/question and hopefully i'm not violating any rules/ect. 您好,StackOverflow用户,您好,这是我的第一个帖子/问题,希望我没有违反任何规则/ ect。

Unfortunately I am unable to post a picture, but I will put this here for a reference. 不幸的是,我无法发布图片,但我将其放在此处以供参考。 http://i.imgur.com/M0uckkg.jpg (Before Button Click) http://i.imgur.com/M0uckkg.jpg (单击按钮之前)

OT: On the picture above, I am attempting to make the button's background completely transparent besides the text. OT:在上面的图片中,我试图使按钮的背景除文本外完全透明。

http://i.imgur.com/bFOEOVC.jpg (After Button Click) http://i.imgur.com/bFOEOVC.jpg (单击按钮后)

Everything works out until I click the button then shadows appear on the side. 一切正常,直到我单击按钮,然后阴影出现在侧面。

Here is what I have tried... 这是我尝试过的...

CSS 的CSS

.toggle-button {
    -fx-text-fill: black;
    -fx-base: transparent;
    -fx-background: transparent;
    -fx-focus-color: transparent;
    -fx-border-color: transparent;
    -fx-effect: null;
}

.toggle-button:selected {
    -fx-text-fill: black;
    -fx-base: transparent;
    -fx-background: transparent;
    -fx-focus-color: transparent;
    -fx-border-color: transparent;
    -fx-effect: null;
}

The java code is just a plain simple application with a ToggleButton. Java代码只是带有ToggleButton的简单应用程序。

Special Thanks in Advance! 特别鸣谢! - Bart -巴特

I wouldn't recommend modifing the main color palette ( -fx-base and others) for just one control. 我不建议只为一个控件修改主调色板( -fx-base和其他)。 This makes sense if you're trying to style all of them. 如果您要为所有样式设置样式,则这很有意义。

Looking at how is defined in modena.css : 查看modena.css定义:

.toggle-button {
    -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, 
        -fx-inner-border, -fx-body-color;
    -fx-text-fill: -fx-text-base-color;
}
.toggle-button:selected {
    -fx-background-color: -fx-shadow-highlight-color,
        linear-gradient(to bottom, derive(-fx-outer-border, -20%), ...),
        linear-gradient(to bottom, derive(-fx-color, -22%) 0%, ...);
}
.toggle-button:selected:focused {
    -fx-background-color: -fx-focus-color, 
        linear-gradient(to bottom, derive(-fx-color, -22%) 0%, ...),
        -fx-faint-focus-color, linear-gradient(to bottom, derive(-fx-color, -22%) 0%, ...);
}

you will need to change all of them: -fx-shadow-highlight-color , -fx-outer-border ,... 您将需要全部更改: -fx-shadow-highlight-color-fx-outer-border ,...

Instead, I'll just override the style of the toggle button with your requirements: 相反,我将使用您的要求覆盖切换按钮的样式:

.toggle-button,
.toggle-button:focused,
.toggle-button:selected,
.toggle-button:selected:focused {
    -fx-background-color: transparent;
    -fx-text-fill: black;
}

NOTE I've edited my answer since, as @eckig suggests, it's redundant to apply the same color to the different comma separated values. 注意我已经编辑了答案,因为正如@eckig所建议的,将相同的颜色应用于不同的逗号分隔值是多余的。

Special Thanks to both of you, Jose & Eckig. 特别鸣谢Jose和Eckig。 I managed to edit my code with both your suggestions and it fixed the issue right away! 我设法根据您的建议编辑了代码,并立即解决了该问题!

Here is a reference for anyone in the future. 这是将来任何人的参考。

Button Code 按钮代码

ToggleButton button= new ToggleButton("Login");
button.getStyleClass().add("custom");

CSS 的CSS

.custom,
    custom:selected {
    -fx-background-color: transparent;
    -fx-text-fill: black;
}

If you want to remove the button-like optics completely, I would suggest you to remove all existing styles and add your own style class as the only style class: 如果要完全删除类似按钮的光学元件,建议您删除所有现有样式,并添加自己的样式类作为唯一的样式类:

ToggleButton toggleButton = new ToggleButton("Login");
toggleButton.getStyleClass().setAll("my-custom-button");

Now you can apply only the styles you need: 现在,您只能应用所需的样式:

.my-custom-button {}

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

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