JavaFX를 사용하여 응용 프로그램을 만들려고합니다.BorderPane에 JavaFX Canvas를 포함하는 방법은 무엇입니까?
내가 만들고 싶은 것은 MenuBar
이 상단에 있고 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);
}
}
I (1)과 같은 창을 가지고 : Canvas
의 크기를 재조정 할 수 있도록하는 방법을 보여줍니다 다음 링크를 참조 , 내가 먼저 다음 코드를했다 (아래 그림을 참조하십시오.)
Canvas
의 실제 크기가 borderPane
크기보다 작기 때문에이 사실을 알고 있습니다. 하지만 BorderPane
의 CENTER 지역을 얻는 방법을 모르겠습니다.
borderPane
의 중심에 래퍼
Pane
바꾸어 시도하고
Canvas
랩퍼
Pane
의 폭과 높이를 결합, 그것으로
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
를 초기화하는 위의 코드의 10+ 라인을 변경했습니다. 창 너비를 늘리면 모든 것이 잘 풀렸다. (그림 (2))
그러나 너비는 줄 였지만 캔버스의 너비는 줄이지 않았습니다. 즉, 창의 크기에 맞습니다. (그림 (3))
높이도 마찬가지입니다.
누구나이 문제를 해결할 생각이 있습니까? 자바 버전은 1.8.0_71입니다.