只有 2 个节点的 javafx borderpane

javafx borderpane with only 2 nodes

我目前正在开发一个需要 canvas 和旁边标签的程序。 我试过使用 Borderpane 来完成这个,但失败了。 我的一个问题是,BorderPane 不需要一个或五个对象来初始化。 我只需要两个。 我的另一个问题是,哦 canvas(center) 显示,即使我用 5 个对象初始化 BorderPane。 这是我的代码:

    primaryStage.setTitle("Drawing Operations Test");
    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent event) {
            try {
                scheduler.shutdown();
                stop();
            } catch (Exception ex) {
                System.err.println("stop failed: " + ex);
                ex.printStackTrace();
            }
        }
    });

   // Group root = new Group();
    Canvas canvas = new Canvas(size, size);
    gc = canvas.getGraphicsContext2D();
    label.setTextFill(Color.web("#000000"));
    label.setFont(new Font("Arial", 30));
    label.setContentDisplay(ContentDisplay.RIGHT);


    //BorderPane.setCenter(canvas);
    //BorderPane.setRight(label);
    //BorderPane.setAlignment(canvas, Pos.CENTER);
    //BorderPane.setAlignment(label, Pos.BASELINE_RIGHT);



    BoxBlur blur = new BoxBlur();
    blur.setWidth(1);
    blur.setHeight(1);
    blur.setIterations(1);
    gc.setEffect(blur);

    drawShapes(gc, size);


            BorderPane.setAlignment(canvas,Pos.TOP_CENTER);
    // Set the alignment of the Bottom Text to Center

    // Set the alignment of the Right Text to Center
    BorderPane.setAlignment(label,Pos.CENTER_RIGHT);

            BorderPane root = new BorderPane(canvas,label);

    // Set the Size of the VBox
    root.setPrefSize(400, 400);     
    // Set the Style-properties of the BorderPane
    root.setStyle("-fx-padding: 10;" +
            "-fx-border-style: solid inside;" +
            "-fx-border-width: 2;" +
            "-fx-border-insets: 5;" +
            "-fx-border-radius: 5;" +
            "-fx-border-color: blue;");

    // Create the Scene
    Scene scene = new Scene(root);
    // Add the scene to the Stage
    primaryStage.setScene(scene);
    // Set the title of the Stage
    primaryStage.setTitle("A simple BorderPane Example");
    // Display the Stage
    primaryStage.show();

我该如何解决这个问题?

以下适合我。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class FxTest00 extends Application {

    public void start(Stage mainStage) throws Exception {
        mainStage.setTitle("JavaFX Skeleton");
        BorderPane root = new BorderPane();
        Canvas canvas = new Canvas(300, 200);
        Label label = new Label("FX Label");
        root.setCenter(canvas);
        root.setRight(label);
        Scene theScene = new Scene(root);
        mainStage.setScene(theScene);
        mainStage.show();
    }

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

您只需使用

即可实现您的要求
BorderPane root = new BorderPane();
root.setCenter(canvas);
root.setRight(label);

BorderPane root = new BorderPane(canvas);
root.setRight(label);

BorderPane root = new BorderPane(canvas, null, label, null, null);

documentation 有一个创建具有三个节点的边框窗格的示例,并明确指出 "Any of the positions may be null."