How do specify a width percentage in JavaFX 2 using FXML?

前端 未结 3 1740
清歌不尽
清歌不尽 2020-11-30 07:09

I am looking for a way to say set a maxWidth size to 80% in FXML.

Much like in web development.

<

3条回答
  •  情歌与酒
    2020-11-30 07:30

    It seems like many answers have already been provided and they should work. However, there is a way to set percentages:

    
        
    
    

    This would help you detect the dimensions of the current screen, the application is being displayed on. Now that we have the display dimensions, we can play with it in FXML as follows:

     Your FXML elements inside the root... 
    

    Note that I use visualBounds, since this would get me the available space on the screen, since I don't want an overlap with the taskbar in Windows for example. For fullscreen applications, you would just use 'bounds'.

    Now, to come to your point of using percentages, you can actually play with the value of the prefheight and prefWidth. You can put calculations inside the ${}.

    Optionally:

    If you want to have all your elements use relative sizes, just refer to them, using their ID and width or height property, and make your calculation.

     more elements.. 
    

    Hope this helps!

提交回复
热议问题