我目前正在开发一个需要画布和标签的程序。我曾尝试使用 Borderpane 来完成此操作,但失败了。我的一个问题是,BorderPane 不需要初始化一个或五个对象。我只需要两个。我的另一个问题是,即使我用 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();
我该如何解决这个问题?