如何将JavaFXCanvas嵌入到BorderPane中?

如何将JavaFXCanvas嵌入到BorderPane中?,java,javafx,java-8,javafx-8,Java,Javafx,Java 8,Javafx 8,我正在尝试使用JavaFX制作一个应用程序 我想做的是一个窗口,上面有MenuBar,中间有Canvas。 我还希望窗口能够调整大小。 参考下面的链接,该链接显示了如何使画布可调整大小,我首先编写了以下代码: 然后我得到了一个类似(1)的窗口(见下图) 我知道这是因为画布的实际大小小于边框窗格的大小。但是我不知道如何获得边框窗格的中心区域 然后,我尝试将一个包装窗格放在边框窗格的中心,并将画布嵌入其中,将包装窗格的宽度和高度绑定到画布。我将上面的10多行代码更改为初始化画布(从注释行(*)开始

我正在尝试使用JavaFX制作一个应用程序

我想做的是一个窗口,上面有
MenuBar
,中间有
Canvas
。 我还希望窗口能够调整大小。 参考下面的链接,该链接显示了如何使画布可调整大小,我首先编写了以下代码:

然后我得到了一个类似(1)的窗口(见下图)

我知道这是因为
画布
的实际大小小于
边框窗格
的大小。但是我不知道如何获得
边框窗格的中心区域

然后,我尝试将一个包装
窗格
放在
边框窗格
的中心,并将
画布
嵌入其中,将包装
窗格
的宽度和高度绑定到
画布
。我将上面的10多行代码更改为初始化
画布
(从注释行(*)开始),如下所示:

        // Create a wrapper Pane first
        BorderPane wrapperPane = new BorderPane();
        borderPane.setCenter(wrapperPane);
        // Put canvas in the center of the window
        Canvas canvas = new Canvas();
        wrapperPane.setCenter(canvas);
        // Bind the width/height property to the wrapper Pane
        canvas.widthProperty().bind(wrapperPane.widthProperty());
        canvas.heightProperty().bind(wrapperPane.heightProperty());
        // redraw when resized
        canvas.widthProperty().addListener(event -> draw(canvas));
        canvas.heightProperty().addListener(event -> draw(canvas));
        draw(canvas);
然后奇怪的事情发生了。当我加大窗户的宽度时,一切都像我想象的那样顺利。(图片(二))

然而,虽然我减小了宽度,但画布的宽度不会减小,换句话说,不会适合窗口的大小。(图片(3))

高度也是如此

有人有办法解决这个问题吗? 我的Java版本是1.8.0_71


使用普通的
窗格
来包装画布,而不是使用
边框窗格

    // Create a wrapper Pane first
    Pane wrapperPane = new Pane();
    borderPane.setCenter(wrapperPane);
    // Put canvas in the center of the window
    Canvas canvas = new Canvas();
    wrapperPane.getChildren().add(canvas);
    // Bind the width/height property to the wrapper Pane
    canvas.widthProperty().bind(wrapperPane.widthProperty());
    canvas.heightProperty().bind(wrapperPane.heightProperty());
    // redraw when resized
    canvas.widthProperty().addListener(event -> draw(canvas));
    canvas.heightProperty().addListener(event -> draw(canvas));
    draw(canvas);
    // Create a wrapper Pane first
    Pane wrapperPane = new Pane();
    borderPane.setCenter(wrapperPane);
    // Put canvas in the center of the window
    Canvas canvas = new Canvas();
    wrapperPane.getChildren().add(canvas);
    // Bind the width/height property to the wrapper Pane
    canvas.widthProperty().bind(wrapperPane.widthProperty());
    canvas.heightProperty().bind(wrapperPane.heightProperty());
    // redraw when resized
    canvas.widthProperty().addListener(event -> draw(canvas));
    canvas.heightProperty().addListener(event -> draw(canvas));
    draw(canvas);