简体   繁体   English

我应该使用SceneBuilder还是在代码中实现布局?

[英]Should I use SceneBuilder or implement layout in code?

I'm quite new to JavaFX and Java in general, but I've experienced something weird. 我对JavaFX和Java很新,但我经历了一些奇怪的事情。 At first, I took a Java course and there we were creating layout for application using SceneBuilder. 起初,我参加了Java课程,然后我们使用SceneBuilder为应用程序创建布局。 I've finished the course and I'm trying to write some simple applications on my own. 我已经完成了课程,我正在尝试自己编写一些简单的应用程序。 But whenever I look for some solutions for problems on the Internet, everywhere layout is implemented in code, that means: using something like this: 但每当我在互联网上寻找解决问题的方法时,无处不在的布局都是在代码中实现的,这意味着:使用类似这样的东西:

Label secondLabel = new Label("I'm a Label on new Window");
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(secondLabel);

So my question is: how I should create layout? 所以我的问题是:我应该如何创建布局? Should I stick to the SceneBuilder or code it in Java as apparently everyone does? 我应该坚持使用SceneBuilder还是用Java编写代码,因为显然每个人都这样做?

Use the right tool for the right job 使用正确的工具来完成正确的工作

There are benefits for using both approaches and you cannot tell in general which one is better without analyzing the scene you're trying to create 使用这两种方法都有好处,如果不分析您尝试创建的场景,通常无法判断哪一种更好

Pros of FXML FXML的优点

  • Fast to design 快速设计
  • WYSYWYG Editor(SceneBuilder) WYSYWYG编辑器(SceneBuilder)
  • Scenes can be modified without the need to recompile the app 无需重新编译应用程序即可修改场景
  • Nice seperation of view and controller with injection 很好的分离视图和控制器与注射
  • In most cases fxml structure resembles the object structure closely making it easy to read. 在大多数情况下,fxml结构类似于对象结构,使其易于阅读。

Cons (compared to java code) 缺点(与java代码相比)

  • loading a fxml is slower than java code 加载fxml比java代码慢
  • no loops possible. 没有循环可能。 When you need to insert something 10 times into a scene you need at least 10 tags; 当你需要在场景中插入10次时,你需要至少10个标签; you have to insert the thing 100 times? 你必须插入100次? Thats at least 100 tags. 这至少有100个标签。 A single tag may not even suffice, eg for a GridPane there is no way to insert multiple nodes to multiple columns per <fx:include> tag. 单个标记甚至可能不够,例如对于GridPane ,无法将每个<fx:include>标记的多个节点插入多个列。
  • Storing nodes into a collection requires assiging different fx:id s to all the elements and then listing all of them at a different location. 将节点存储到集合中需要为所有元素分配不同的fx:id ,然后在不同的位置列出所有元素。
  • No compile-time checks. 没有编译时检查。 You want to add a Integer to the child list of a parent? 您想将一个Integer添加到父级的子列表中吗? You can write a fxml that attemts to do that without getting an error - that is until you attempt to load it. 您可以编写一个fxml来执行此操作而不会出现错误 - 直到您尝试加载它为止。 The java compiler would immediately complain about something like that. java编译器会立即抱怨类似的东西。
  • There is just no way to invoke some methods from fxml. 没有办法从fxml调用某些方法。
  • Loading fxmls relies on reflection 加载fxmls依赖于反射

You can of course use java code in the controller class which allows you to combine fxml+java code to a certain level. 您当然可以在控制器类中使用java代码,它允许您将fxml + Java代码组合到某个级别。 Eg repetetive structures could be created in the initialize() method. 例如,可以在initialize()方法中创建重复结构。

Scene builder is amazing when you are building project with JavaFX+FXML,i cant recommend it enough. 当您使用JavaFX + FXML构建项目时,场景构建器是惊人的,我不能推荐它。

When you use fxml your classes become so much cleaner, and you can focus on further extending functionality of your containers/controls.You are still gonna be using javafx classes and your controllers might become quite big. 当你使用fxml时,你的类变得更加清晰,你可以专注于进一步扩展容器/控件的功能。你仍然会使用javafx类,你的控制器可能会变得非常大。 Its good to know the concepts of how to structure layout and when to use what kind of container, so when you just started to learn basics, implement your layout in code, and then move to using SceneBuilder and FXML. 很高兴知道如何构造布局的概念以及何时使用什么样的容器,所以当你刚开始学习基础知识时,在代码中实现你的布局,然后转向使用SceneBuilder和FXML。

Once you implement more complex layouts in your code, you can translate these skills into other languages/platforms. 在代码中实现更复杂的布局后,您可以将这些技能转换为其他语言/平台。

There are also cases where you cant do what you want via SceneBuilder, for instance some dynamic layout thats beign created at runtime and would be too tricky and messy to include into separated custom components/fxml files. 在某些情况下,您无法通过SceneBuilder执行您想要的操作,例如在运行时创建的一些动态布局,并且包含在单独的自定义组件/ fxml文件中会非常棘手和混乱。 Then you write it down in your code with complex logic on top of it. 然后在代码中将其写下来,并在其上面加上复杂的逻辑。

Implement it using SceneBuilder/FXML directly. 直接使用SceneBuilder / FXML实现它。 JavaFX was designed to use XML UI definition files instead of programming your UI in Java code. JavaFX旨在使用XML UI定义文件,而不是在Java代码中编写UI。 It is a separation like in Android. 这是一个像Android一样的分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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