我能确定JavaFX中的阶段/窗口插入吗?在Swing中,我可以简单地编写以下代码:
在JavaFX中,如何获取窗口的边框和标题栏的大小?
JFrame frame = new JFrame();
Insets insets = frame.getInsets();
在JavaFX中,如何获取窗口的边框和标题栏的大小?
JFrame frame = new JFrame();
Insets insets = frame.getInsets();
您可以通过查看场景相对于窗口的宽度和高度的范围来确定这些内容。
给定一个Scene scene;
,scene.getX()
和 scene.getY()
分别给出了Scene
在窗口内的x和y坐标。 它们分别等同于左侧和顶部的插入量。
右侧和底部略微棘手,但是……
scene.getWindow().getWidth()-scene.getWidth()-scene.getX()
给出正确的插入并类似地
scene.getWindow().getHeight()-scene.getHeight()-scene.getY()
给出底部插图。
当场景被放置在窗口中且窗口在屏幕上可见时,这些值才有意义。
如果您真的想要一个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);
}
}