简体   繁体   English

在运行的Java应用程序中更改CSS样式的方法

[英]Way of changing CSS styles in a running Java app

I'm working on JavaFX app that uses CSS styles that are included in file. 我正在使用JavaFX应用程序,该应用程序使用文件中包含的CSS样式。 What i want is to change CSS style after an action happens on button and i wrote some code that changes style of Button when i enter/exit it: 我想要的是在按钮上发生动作后更改CSS样式,并编写了一些代码,当我输入/退出Button时,它会更改Button样式:

public void actionMouseEntered() {
    buttonReflex1.getStyleClass().clear();
    buttonReflex1.getStyleClass().add("button_reflex_pressed");
    }
public void actionMouseExited(){
    buttonReflex1.getStyleClass().clear();
    buttonReflex1.getStyleClass().add("button_reflex");
}

And here you have my CSS file: 这是我的CSS文件:

.button_reflex{
    -fx-shape: "M 200 *a lot of numbers here* Z";
    -fx-background-color: radial-gradient(focus-angle 360deg, focus-distance 0%, center 50% 50%, radius 70%, reflect, lightblue, aqua 30%, blue);
    -fx-text-fill: linear-gradient(#e4e000 0%, #ff0000 50%, #e4e000 100%);
}
.button_reflex_pressed{
    -fx-shape: "M 200 *a lot of numbers here* Z";
    -fx-background-color: radial-gradient(focus-angle 360deg, focus-distance 0%, center 50% 50%, radius 70%, reflect, dodgerblue, deepskyblue 30%, darkblue);
    -fx-text-fill: linear-gradient(#e4e000 0%, #ff0000 50%, #e4e000 100%);
}

They differ in a color of buttons. 它们的按钮颜色不同。

Code i wrote above WORKS , but i think i didn't write it in a good way. 我在WORKS上方编写了代码,但我认为我编写的方式不是很好。 Could you tell me if my method of implementing it is correct, or if it isn't, please tell me how can i do it better, because i don't want to learn bad habits. 您能告诉我我实施该方法的方法是否正确,请告诉我我该如何做得更好,因为我不想学习不良习惯。

You do not need to change the style class. 您不需要更改样式类。 Changing the style class also is not the way best way to approach this. 更改样式类也不是解决此问题的最佳方法。 A PseudoClass should be used in this case. 在这种情况下,应使用PseudoClass Also you should avoid duplicating css properties that do not differ or differ only in colors. 另外,您应该避免复制没有不同或仅颜色不同的css属性。 Use a rule for both to specify this only once making it easier to modify; 为这两者使用规则仅一次指定此名称,以使其易于修改; in the latter case you can use lookedup colors: 在后一种情况下,您可以使用查阅颜色:

private static final PseudoClass MY_HOVER = PseudoClass.getPseudoClass("my-hover");

...
buttonReflex1.getStyleClass().setAll("button-reflex");
...

    boolean hovering = ...;
    buttonReflex1.pseudoClassStateChanged(MY_HOVER, hovering);
.button-reflex {
    -fx-background-color1: lightblue;
    -fx-background-color2: aqua;
    -fx-background-color3: blue;
    -fx-shape: "M 200 *a lot of numbers here* Z";
    -fx-background-color: radial-gradient(focus-angle 360deg, focus-distance 0%, center 50% 50%, radius 70%, reflect, -fx-background-color1, -fx-background-color2 30%, -fx-background-color3);
    -fx-text-fill: linear-gradient(#e4e000 0%, #ff0000 50%, #e4e000 100%);
}

.button-reflex:my-hover {
    /* only change colors here; keep the rest */
    -fx-background-color1: dodgerblue;
    -fx-background-color2: deepskyblue;
    -fx-background-color3: darkblue;
}

Note: The my-hover pseudoclass is only used here to demonstrate the general approach. 注意: my-hover伪类仅在此处用于演示通用方法。 JavaFX Node s provide some pseudoclasses by default which should be used instead: hover , pressed , ect... JavaFX Node默认情况下提供了一些伪类,应该使用它们来代替: hoverpressed ,ect ...

See JavaFX CSS Reference Guide: Node 请参阅《 JavaFX CSS参考指南:节点》

So first off all yes you can change the CSS while run time. 所以首先,是的,您可以在运行时更改CSS。

But you are using it in a wrong way. 但是您以错误的方式使用它。

So if you have your base CSS class for your Node. 因此,如果您拥有Node的基本CSS类。 For example: 例如:

.button {
    -fx-background-color: red;
}

and now if you want to have an effect for any JavaFx Event than there are some options like Hover, Pressed, Focused ... 现在,如果您想对任何JavaFx Event产生影响,则可以使用诸如悬停,按下,聚焦...

.button {
   -fx-background-color: red;
}

.button:hover {
   -fx-background-color: blue;
}

.button:pressed{
   -fx-background-color: green;
}

JavaFx is than handling everything for you and you dont have to change the style manually. JavaFx不仅可以为您处理所有事情,而且您无需手动更改样式。

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

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