更改不同控制器中节点的 Id

Changing Id of nodes in different controllers

我在应用程序中有此代码class

scene.getStylesheets().add(getClass().getResource(CONSTANTS.DESIGN_CSS.directory).toExternalForm());

和这个 CSS 文件

@CHARSET "UTF-8";

#mainList .list-cell {
    -fx-background-color: null;
    -fx-font-size: 24px;
    -fx-text-fill: linear-gradient( from 0.0% 0.0% to 0.0% 50.0%, reflect, rgba(255,0,0,0.28) 0.0, rgba(102,243,255,0.58) 50.0, rgba(179,179,179,0.45) 70.0, rgba(179,179,179,0.45) 100.0);
}
#mainList .list-cell:hover {
    -fx-text-fill:linear-gradient( from 0.0% 0.0% to 100.0% 100.0%, reflect, rgb(255,255,255) 0.0, rgb(255,255,77) 100.0);
}

#stoperButton .button { 
    -fx-background-color:  linear-gradient( from 50.0% 100.0% to 100.0% 100.0%, rgb(0,0,0) 0.0, rgb(255,255,255) 100.0);
}

#stoperButton .button:hover {
    -fx-background-color: #9ACD32;
}

在加载的第一个控制器中,我可以轻松地做到这一点并且它有效:

listView.setId("mainList");

但这(在不同的控制器中)让我没有错误但没有效果:

buttSTOP.setId("stoperButton");

跟改变场景根有关系吗?

选择器

#stoperButton .button { ... }

匹配样式为 class "button" 的节点,并且是具有 css id "stoperButton".

的节点的后代

我想你想要的只是

#stoperButton {
    -fx-background-color:  linear-gradient( from 50.0% 100.0% to 100.0% 100.0%, rgb(0,0,0) 0.0, rgb(255,255,255) 100.0);
}

#stoperButton:hover {
    -fx-background-color: #9ACD32;
}