简体   繁体   中英

JavaFX: Line goes automatically to center of the scene

I want to draw Line s in JavaFX but the program seems not to listen to the coordinates and directly draws it in the middle (ie center) with the same length.

Code:

package application;
    
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        Line line = new Line(200,200,250,250);
        StackPane root = new StackPane();
        root.setPadding(new Insets(15));
        final Scene scene = new Scene(root, 400, 250);
        scene.setFill(null);
        root.getChildren().addAll(line);
        stage.setTitle("JavaFX Line");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Result: Line in Center

A StackPane is a layout pane which means that it will place any node according to its own rules which may come as a surprise as in your case. Instead you could just use a Pane which will place your line at the place you have specified. It may be a good idea to place this Pane inside your StackPane so that it fills the whole space.

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