我想以编程方式应用此样式:
.rating:disabled > .container:disabled .button:disabled{
-fx-pref-height:15;
-fx-background-size: cover;
-fx-padding: 0;
}
我已经尝试过这个,但不起作用:
ratingHeigth.bind(mainBorderPane.prefHeightProperty().divide(0.0355));
vipRating.styleProperty().bind(Bindings.concat(".rating:disabled > .container:disabled .button:disabled{ -fx-pref-height: ", ratingHeigth.asString(), ";}"));
最佳答案
据我所知,这是未记录的行为(因此您可能不想依赖它),但您可以以与“查找颜色”类似的方式创建“查找大小” (已记录)。
在外部 CSS 样式表中,执行
.rating {
disabled-button-size: 15 ;
}
.rating:disabled > .container:disabled .button:disabled{
-fx-pref-height: disabled-button-size ;
-fx-background-size: cover;
-fx-padding: 0;
}
然后在Java中执行
vipRating.styleProperty().bind(ratingHeight.asString("disabled-button-size: %f ;"));
您的代码不起作用,因为内联样式只是将字符串指定的实际样式应用于您调用 setStyle(...)
的节点:内联样式不包含选择器。
上述解决方案的想法是在定义所需高度的 CSS 文件中定义“查找尺寸”(某种 CSS 变量)。然后使用 setStyle 更改“查找大小”的值。该值由子节点继承,因此只需在具有 rating
CSS 类的容器上使用 setStyle
设置它就足够了。
这是一个(更简单的)SSCCE。移动 slider ,按钮将改变大小:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.IntegerBinding;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LookedUpSizeTest extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Test");
StackPane stack = new StackPane(button);
BorderPane root = new BorderPane(stack);
Slider sizeSlider = new Slider(30, 350, 40);
stack.styleProperty().bind(sizeSlider.valueProperty().asString("button-size: %f ;"));
root.setBottom(sizeSlider);
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
样式.css:
.root {
button-size: 20 ;
}
.button {
-fx-pref-height: button-size ;
}
关于java - JavaFX 子类的内联样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609920/