javafx边框只有2个节点

时间:2018-02-01 19:45:45

标签: java canvas javafx layout borderpane

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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分: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);

documentation有一个创建包含三个节点的边框窗格的示例,并明确说明&#34;任何位置都可以为空。&#34;

答案 1 :(得分: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);
    }
}