-1

我目前正在开发一个需要画布和标签的程序。我曾尝试使用 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();

我该如何解决这个问题?

4

2 回答 2

1

您可以简单地实现您的要求

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);

文档有一个创建具有三个节点的边框窗格的示例,并明确指出“任何位置都可能为空”。

于 2018-02-01T20:16:11.210 回答
0

以下对我有用。

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);
    }
}
于 2018-02-01T20:15:56.223 回答