JavaFX 8: Inserti di fase (spessore della decorazione della finestra)?


Come posso determinare gli inserti stage/window in JavaFX? In Swing potrei scrivere semplicemente:

JFrame frame = new JFrame();
Insets insets = frame.getInsets();

Quale sarebbe l'equivalente in JavaFX per ottenere la dimensione del bordo e la barra del titolo della finestra?

Author: tobain, 2014-11-03

1 answers

È possibile determinarli osservando i limiti della scena rispetto alla larghezza e all'altezza della finestra.

Dato un Scene scene;, scene.getX() e scene.getY() dare le coordinate x e y del Scene all'interno della finestra. Questi sono equivalenti agli inserti sinistro e superiore, rispettivamente.

La destra e la parte inferiore sono leggermente più complicate, ma

scene.getWindow().getWidth()-scene.getWidth()-scene.getX()

Fornisce gli inserti giusti e allo stesso modo

scene.getWindow().getHeight()-scene.getHeight()-scene.getY()

Fornisce gli inserti inferiori.

Questi valori avranno ovviamente solo un senso una volta che la scena è posto in una finestra e la finestra è visibile sullo schermo.

Se vuoi davvero un oggetto Insets puoi fare qualcosa di simile a quanto segue (che rimarrebbe valido anche se il bordo o la barra del titolo cambiassero dimensione dopo che la finestra è stata visualizzata):

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class WindowInsetsDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Label topLabel = new Label();
        Label leftLabel = new Label();
        Label rightLabel = new Label();
        Label bottomLabel = new Label();


        VBox root = new VBox(10, topLabel, leftLabel, bottomLabel, rightLabel);
        root.setAlignment(Pos.CENTER);

        Scene scene = new Scene(root, 600, 400);


        ObjectBinding<Insets> insets = Bindings.createObjectBinding(() -> 
        new Insets(scene.getY(), 
                primaryStage.getWidth()-scene.getWidth() - scene.getX(), 
                primaryStage.getHeight()-scene.getHeight() - scene.getY(), 
                scene.getX()),
                scene.xProperty(),
                scene.yProperty(),
                scene.widthProperty(),
                scene.heightProperty(),
                primaryStage.widthProperty(),
                primaryStage.heightProperty()
            );

        topLabel.textProperty().bind(Bindings.createStringBinding(() -> "Top: "+insets.get().getTop(), insets));
        leftLabel.textProperty().bind(Bindings.createStringBinding(() -> "Left: "+insets.get().getLeft(), insets));
        rightLabel.textProperty().bind(Bindings.createStringBinding(() -> "Right: "+insets.get().getRight(), insets));
        bottomLabel.textProperty().bind(Bindings.createStringBinding(() -> "Bottom: "+insets.get().getBottom(), insets));

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 5
Author: James_D, 2014-11-03 22:50:05