JavaFX 8:Stage insets(窗口装饰厚度)?

tob*_*ain 6 java border stage window-decoration javafx-8

如何确定JavaFX中的阶段/窗口插入?在Swing中,我可以简单地写:

JFrame frame = new JFrame();
Insets insets = frame.getInsets();
Run Code Online (Sandbox Code Playgroud)

在JavaFX中获得边框大小和窗口标题栏的等价物是什么?

Jam*_*s_D 6

您可以通过查看相对于窗口宽度和高度的场景边界来确定这些.

给定一个Scene scene;,scene.getX()并scene.getY()给出Scene窗口内的x和y坐标.它们分别相当于left和top insets.

右边和底部有点棘手,但是

scene.getWindow().getWidth()-scene.getWidth()-scene.getX()
Run Code Online (Sandbox Code Playgroud)

给出正确的插图,同样如此

scene.getWindow().getHeight()-scene.getHeight()-scene.getY()
Run Code Online (Sandbox Code Playgroud)

给出了底层插图.

当场景放置在窗口中并且窗口在屏幕上可见时,这些值当然才有意义.

如果你真的想要一个Insets对象,你可以做类似下面的事情(如果在显示窗口后边框或标题栏改变了大小,它甚至会保持有效):

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);
    }
}
Run Code Online (Sandbox Code Playgroud)