JavaFX CSS中心对齐

贾博伊茨

我正在使用CSS在JavaFX中创建自定义主题,并且试图使所有HBoxes和VBoxes具有中心对齐。我到处都在寻找是否有针对父母或HBoxes的造型课,但我似乎找不到。我知道我可以做,HBox.setAligent(Pos.CENTER);但是我不想为我拥有的每个HBox或VBox键入该命令。如何使所有HBox都具有中心对齐,而不必使用自定义类并将其放入CSS文件中?

詹姆斯_D

文档中所述,HBox(和VBox,以及不是Control子类的任何节点)的样式类默认为空。

但是,您可以使用类型选择器引用文档:

Node的getTypeSelector方法返回一个类似于CSS Type Selector的字符串。默认情况下,此方法返回类的简单名称。

因此,您只需要将类名用作选择器即可。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);
    }
}

与alignment-test.css:

HBox, VBox {
    -fx-alignment: center ;
    -fx-padding: 10 ;
}

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章