JavaFX:设置场景最小尺寸,不包括装饰 [英] JavaFX: Set Scene min size excluding decorations
问题描述
我知道使用JavaFX,您可以使用 stage.setMinWidth()
和 stage.setMinHeight()$ c设置舞台最小尺寸但是,$ c>,这将包括窗口边框(使用最小化,最大化和关闭按钮)。
I know that with JavaFX you can set the Stage min size using stage.setMinWidth()
and stage.setMinHeight()
, however, this will include the window border (with the minimize, maximize, and close buttons).
如何在设置最小尺寸时将其排除?
How can I exclude this when setting the minimum size?
推荐答案
您可以显示 Stage
未修饰,这将删除所有border:
You can show the Stage
undecorated, which will remove all the borders:
primaryStage.initStyle(StageStyle.UNDECORATED);
如果你想装饰
如果你想装饰它你可以将场景
的大小设置为首选的最小尺寸,然后显示窗口( Stage
将具有能够显示 Scene
的大小,其大小实际上是您所需的最小大小),然后将最小尺寸设置为当前尺寸。
If you want to have it decorated you can set the size of the Scene
to the preferred minimum size, then show the window (the Stage
will have the size to be able to display the Scene
with that size which one actually is your needed minimum size), then set the minimal size to the current size.
示例:
public class Main extends Application {
private final int PREF_MIN_WIDTH = 500;
private final int PREF_MIN_HEIGHT = 500;
@Override
public void start(Stage primaryStage) {
try {
Scene scene = new Scene(new HBox(), PREF_MIN_WIDTH, PREF_MIN_HEIGHT);
primaryStage.setScene(scene);
primaryStage.showingProperty().addListener((observable, oldValue, showing) -> {
if(showing) {
primaryStage.setMinHeight(primaryStage.getHeight());
primaryStage.setMinWidth(primaryStage.getWidth());
primaryStage.setTitle("My mininal size is: W"+ primaryStage.getMinWidth()+" H"+primaryStage.getMinHeight());
}
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
这将生成如下窗口:
这篇关于JavaFX:设置场景最小尺寸,不包括装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!