我正在使用 CSS 在 JavaFX 中制作自定义主题,并且我正在尝试使所有 HBox 和 VBox 具有中心对齐。我到处找找父母或HBoxes是否有造型课,但我似乎找不到。我知道我可以这样做,HBox.setAligent(Pos.CENTER);但我不想为我拥有的每个 HBox 或 VBox 键入该命令。我该怎么做才能使所有 HBox 都具有中心对齐,而不必使用自定义类并将其放入 CSS 文件中?
如文档中所述,HBox(和VBox,以及任何不是Control子类的节点)的样式类默认为空。
但是,您可以使用类型选择器。引用文档:
Node 的 getTypeSelector 方法返回一个类似于 CSS 类型选择器的字符串。默认情况下,此方法返回类的简单名称。
所以你只需要类名作为选择器。SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AlignmentTest extends Application {
@Override
public void start(Stage primaryStage) {
ListView<String> listView = new ListView<>();
TextField textField = new TextField();
Button button = new Button("Click here");
Label label = new Label("A label");
VBox root = new VBox(5, new HBox(5, listView, button), new HBox(5, label, textField));
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add("alignment-test.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
使用对齐测试.css:
HBox, VBox {
-fx-alignment: center ;
-fx-padding: 10 ;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15759 次 |
| 最近记录: |