简体   繁体   中英

How to align a line in JavaFX?

The documentation of line says that how make a line you need to specify the start X, Y and end X,Y of the line. This can be use but only if the scene is of fixed dimensions. Instead of specifying the start X, Y and end X,Y of the line I want to directly specify the length of the line, the orientation of the line and simply position it at the center of the scene using root.setCenter(line); I don't think there is any way to specify the start X, Y and end X,Y of the line and then align it at the center of the scene horizontally or vertically. Here is an illustration of what I'm trying to achieve: 线代表

So to solve this I'm using a BorderPane as my root and then I want to have a horizontal line and align it using root.setCenter(lines); but there is no such way to align a line I could find. How can I make specify this location of the line to be horizontally on a scene by just specifying Pos.CENTER like other nodes?

I found how to do it, as @Fabian said you need to use StackPane as the root of your scene and specify the start end X and Y and align it with StackPane. Here is the code I used:

    private StackPane root = new StackPane();
    private Scene scene = new Scene(root, 1366, 768);

    @Override
    public void start(Stage primaryStage) {
        Line line = new Line(0,100,100,100);
        line.setStrokeWidth(20);

        StackPane.setAlignment(line, Pos.CENTER);
        root.getChildren().add(line);

        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM