How to bind stage resizing with resizing of components?

前端 未结 3 1630
春和景丽
春和景丽 2020-12-08 11:40

I have a JavaFX 2.0 application with FXML. I want the components (TextFields, ComboBoxes, layouts, and so on) to be resized when a window with an application is resized. So

3条回答
  •  孤街浪徒
    2020-12-08 12:10

    Some suggestions for building resizable guis =>

    • Make sure the root of your app is a Pane subclass, rather than a Group.
    • Unclamp the max size of buttons (e.g button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE).
    • Most of the time the layout panes can handle the resizing for you and you don't need bindings and listeners => i.e. only bind when you really need to.
    • UI controls and layout panes are Regions => all Regions have read-only height and width properties you can listen to and bind to.
    • You can not directly bind the height and width properties of a control to another value. Instead use the minWidth/Height, prefWidth/Height, maxWidth/Height properties.
    • The scene has height and width properties you can bind to if needed. If you do this, when the stage is resized, the scene will be resized, then your bound components will be resized. If the root of your scene is a layout pane rather than a group, this binding is usually unnecessary.
    • If you don't set a height and width on the scene, for a stand-alone app, the scene (and stage) will be sized to fit the preferred size of the scene's root node (which is usually what you want).
    • If all else fails you can start overriding Parent's layoutchildren method.

    Here is an example of a resizable JavaFX UI which implements some of the principles above.

提交回复
热议问题