gpt4 book ai didi

java - 如何将 JavaFX Canvas 嵌入到 BorderPane 中?

转载 作者:行者123 更新时间:2023-11-29 04:44:45 24 4
gpt4 key购买 nike

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

我想做的是顶部有MenuBar,中间有Canvas的窗口。我还希望窗口能够调整大小。引用以下链接,其中显示了如何使 Canvas 可调整大小,我首先编写了以下代码:

JavaFX Tip 1: Resizable Canvas – DLSC

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CanvasInBorderPane extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception {
BorderPane borderPane = new BorderPane();

// Put menu bar on the top of the window
MenuBar menuBar = new MenuBar(new Menu("File"), new Menu("Edit"),
new Menu("Help"));
borderPane.setTop(menuBar);

// Put canvas in the center of the window (*)
Canvas canvas = new Canvas();
borderPane.setCenter(canvas);
// Bind the width/height property so that the size of the Canvas will be
// resized as the window is resized
canvas.widthProperty().bind(borderPane.widthProperty());
canvas.heightProperty().bind(borderPane.heightProperty());
// redraw when resized
canvas.widthProperty().addListener(event -> draw(canvas));
canvas.heightProperty().addListener(event -> draw(canvas));
draw(canvas);

Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* Draw crossed red lines which each each end is at the corner of window,
* and 4 blue circles whose each center is at the corner of the window,
* so that make it possible to know where is the extent the Canvas draws
*/
private void draw(Canvas canvas) {
int width = (int) canvas.getWidth();
int height = (int) canvas.getHeight();
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, width, height);
gc.setStroke(Color.RED);
gc.strokeLine(0, 0, width, height);
gc.strokeLine(0, height, width, 0);
gc.setFill(Color.BLUE);
gc.fillOval(-30, -30, 60, 60);
gc.fillOval(-30 + width, -30, 60, 60);
gc.fillOval(-30, -30 + height, 60, 60);
gc.fillOval(-30 + width, -30 + height, 60, 60);
}
}

然后我得到了一个像(1)这样的窗口(见下图。)

我知道这是因为 Canvas 的实际大小应该小于 borderPane 的大小。但我不知道如何获取 BorderPane 的中心区域。

然后我尝试将包装器 Pane 放在 borderPane 的中心并将 Canvas 嵌入其中,绑定(bind)宽度和高度包装器 PaneCanvas。我更改了上面 10 多行初始化 Canvas 的代码(从带有 (*) 的注释行开始),如下所示:

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

然后奇怪的事情发生了。当我增加窗口的宽度时,一切都如我所想的那样顺利。 (图(二))

然而,虽然我减少了宽度,但Canvas的宽度并没有减少,换句话说,适合窗口的大小。 (图(3))

高度也是如此。

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

The screenshots of the window

最佳答案

使用普通的 Pane 来包裹 Canvas ,而不是 BorderPane:

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

关于java - 如何将 JavaFX Canvas 嵌入到 BorderPane 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37678704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com