简体   繁体   中英

JavaFX or FXML. What is the difference?

I have newly started to learn javafx to build desktop apps. But i don't understand one thing. Here is my problem: We can create a gui by writing codes, like that:


在此处输入图片说明


Or, we can create gui with scene builder, like this:

在此处输入图片说明


If the second method is available, why should we use writing codes, like where to put this label or button or whatever.?

I'm new, so i don't know if the second method has any weakness.

I mostly work in FXML myself, I like the WYSIWYG approach of the SceneBuilder, but I had cases where I went back to coded UI.

For example I have an application where the user answers up to 10 questions. The questions or their amount is not known at compile time. I create the labels and checkboxes for the questions at runtime in a GridPane. The GridPane is part of a FXML layout. This can be done solely in FXML using ListView and custom ListCells but it is substantially more work and I do not need Cell reusing because there are too few questions.

Also it can be necessary to have 10x10 buttons, putting 100 buttons in FXML is no fun. I used that when I implemented a game like this on Android.

Also when you have a lot of different views that are all very similar except for small positioning difference you might want to create them in code instead of making multiple nearly identical FXML files.

I'm new, so i don't know if the second method has any weakness.

Time permitting, write your UI separately using both approaches!

Seriously, if you're new and want to learn the pitfalls of either approach, there's generally no better way than to get hands on with a few scenarios, get a grasp on both toolkits and see which approach you prefer in different situations.

There are clear cases when one will become better than the other, but the "in-between" cases then often come down to personal preference (and in the case where it's not just you developing, team-based preference and adhering to your company's code style requirements as well.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM