[英]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编写代码,因为显然每个人都这样做?
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 使用这两种方法都有好处,如果不分析您尝试创建的场景,通常无法判断哪一种更好
GridPane
there is no way to insert multiple nodes to multiple columns per <fx:include>
tag. GridPane
,无法将每个<fx:include>
标记的多个节点插入多个列。 fx:id
s to all the elements and then listing all of them at a different location. fx:id
,然后在不同的位置列出所有元素。 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. 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.