Ich versuche eine Anwendung mit JavaFX zu machen.Wie kann JavaFX Canvas in BorderPane eingebettet werden?
Was ich machen möchte, ist das Fenster mit MenuBar
auf der Oberseite und Canvas
in der Mitte. Ich möchte auch das Fenster in der Lage sein, die Größe zu ändern. auf den folgenden Link Bezug genommen, die zeigt, wie Canvas
Größe veränderbar zu machen, habe ich zuerst den folgenden Code:
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);
}
}
Dann bekam ich ein Fenster wie (1) (unter dem Bild sehen.)
Ich weiß, das ist, weil die tatsächliche Größe der Canvas
sollte kleiner sein als die Größe von borderPane
. Aber ich weiß nicht, wie man die CENTER-Region der BorderPane
bekommt.
ich versucht dann einen Wrapper Pane
in der Mitte des borderPane
Putting und betten die Canvas
hinein, die Breite und Höhe der Umhüllung Bindung Pane
zum Canvas
. Ich habe die 10 + Codezeilen, über den die Canvas
initialisiert (die von der Kommentarzeile zu starten mit (*)) wie folgt:
// 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);
Dann geschah etwas Seltsames. Als ich die Breite des Fensters vergrößerte, lief alles gut, wie ich vermutete. (Bild (2))
Obwohl ich die Breite verringerte, wurde die Breite des Canvas nicht verringert, mit anderen Worten, die Größe des Fensters angepasst. (Bild (3))
Und so ist die Höhe.
Hat jemand eine Idee, um dieses Problem zu beseitigen? Meine Java-Version ist 1.8.0_71.