JavaFx:是否可以检查项目是否具有特定的样式类?

时间:2022-01-28 12:45:53

Im making buttons that need to switch between two colors every time they are pressed. I wanted to do that by comparing the style class to see if it matches either the "green" or "red" css class. Like so.

我制作的按钮每次按下时都需要在两种颜色之间切换。我想通过比较样式类来查看它是否匹配“绿色”或“红色”css类。像这样。

if(clickedBtn.getStyleClass() == "green") {
            clickedBtn.getStyleClass().add("red");
        } else {
        clickedBtn.getStyleClass().add("green");
        }

This doesn't work as it doesn't recognize "green" as anything. Is there a simpler way of doing this? I just need a graphic display with selectable seats. Thanks

这不起作用,因为它不会将“绿色”识别为任何东西。有更简单的方法吗?我只需要一个带可选座椅的图形显示器。谢谢

1 个解决方案

#1


2  

.getStyleClass() retruns a ObservableList containing the style classes. This will never be the same object as a string literal, so the == check always yields false. The proper way of checking, if a node has a style class would be invoking the contains method of the list:

.getStyleClass()重新构造一个包含样式类的ObservableList。这永远不会是与字符串文字相同的对象,因此== check总是产生false。正确的检查方法是,如果节点具有样式类,则将调用列表的contains方法:

if (clickedBtn.getStyleClass().contains("green")) {

Since you probably want red and green to be mutual exclusive. you should also remove the style classes:

因为你可能希望红色和绿色是相互排斥的。你还应该删除样式类:

if(clickedBtn.getStyleClass().remove("green")) {
    clickedBtn.getStyleClass().add("red");
} else {
    clickedBtn.getStyleClass().remove("red");
    clickedBtn.getStyleClass().add("green");
}

Using pseudo classes would probably be a bit more convenient however:

然而,使用伪类可能会更方便一些:

private final static PseudoClass GREEN = PseudoClass.getPseudoClass("green");
private final static PseudoClass RED = PseudoClass.getPseudoClass("red");

...

boolean isGreen = clickedBtn.getPseudoClassStates().contains(GREEN);
clickedBtn.pseudoClassStateChanged(GREEN, !isGreen);
clickedBtn.pseudoClassStateChanged(RED, isGreen);

#1


2  

.getStyleClass() retruns a ObservableList containing the style classes. This will never be the same object as a string literal, so the == check always yields false. The proper way of checking, if a node has a style class would be invoking the contains method of the list:

.getStyleClass()重新构造一个包含样式类的ObservableList。这永远不会是与字符串文字相同的对象,因此== check总是产生false。正确的检查方法是,如果节点具有样式类,则将调用列表的contains方法:

if (clickedBtn.getStyleClass().contains("green")) {

Since you probably want red and green to be mutual exclusive. you should also remove the style classes:

因为你可能希望红色和绿色是相互排斥的。你还应该删除样式类:

if(clickedBtn.getStyleClass().remove("green")) {
    clickedBtn.getStyleClass().add("red");
} else {
    clickedBtn.getStyleClass().remove("red");
    clickedBtn.getStyleClass().add("green");
}

Using pseudo classes would probably be a bit more convenient however:

然而,使用伪类可能会更方便一些:

private final static PseudoClass GREEN = PseudoClass.getPseudoClass("green");
private final static PseudoClass RED = PseudoClass.getPseudoClass("red");

...

boolean isGreen = clickedBtn.getPseudoClassStates().contains(GREEN);
clickedBtn.pseudoClassStateChanged(GREEN, !isGreen);
clickedBtn.pseudoClassStateChanged(RED, isGreen);