JavaFX 中的 fx:id 和 id: 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23686325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:41:40  来源:igfitidea点击:

What's the difference between fx:id and id: in JavaFX?

javaxmljavafxscenebuilder

提问by Analyst

Maybe a really newbie's question....

也许一个真正的新手的问题......

I'm starting learning JavaFX in a FMXL Application using the Scene Builder, by reading this tutorials:

通过阅读本教程,我开始使用 Scene Builder 在 FMXL 应用程序中学习 JavaFX:

http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm

http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm

So once i applied some changes, an issue with this 2 IDs came up... I might have missed or confused something about them...

因此,一旦我应用了一些更改,就会出现这 2 个 ID 的问题......我可能错过了或混淆了它们......

Can anyone tell me in which cases they are used one or another?

谁能告诉我在哪些情况下使用它们?

采纳答案by Patrick

idyou use to set a CSS IDto your Component, for example <Text id="welcome-text" .../>and in your stylesheet you have something like #welcome-text { font-size: 16pt; }so this will be applied to your Text.

id例如,您用来为您的组件设置CSS ID<Text id="welcome-text" .../>并且在您的样式表中,您有类似的内容,#welcome-text { font-size: 16pt; }因此这将应用于您的Text.

fx:idyou use if you want to work with your Components in your Controller class, where you annotate them with @FXML Text myWelcomeText.

fx:id如果你想在你的控制器类中使用你的组件,你可以使用,在那里你用@FXML Text myWelcomeText.

回答by Lucas Z.

The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.

fx:id 是与 fxml 中组件关联的标识,用于构建控制器,该 id 用于 css。

回答by Cobbles

I took a look at an FXML document generated using the JavaFX Scene Builder. You access controls from Java Controller with the fx:id. (edit) I stand corrected, the id does matter.

我查看了使用JavaFX Scene Builder生成的 FXML 文档。您可以使用 fx:id 从 Java 控制器访问控件。(编辑)我更正了,id 很重要。

You can apply css from the FXML document like this:

您可以像这样从 FXML 文档中应用 css:

<Slider id="css_id" fx:id="myslider" styleClass="style_name" .../>

(Replace slider with any control)

(用任何控件替换滑块)

And Java controller interaction:

与Java控制器交互:

@FXML
Slider myslider;

回答by Amita Patil

In JavaFX idis used to set a CSS ID to a component. And fx:idis used for accessing that component in code (i.e. in a controller class). fx:idworks like a components name.

在 JavaFXid中用于为组件设置 CSS ID。并且fx:id用于在代码中访问该组件(即在控制器类中)。fx:id像组件名称一样工作。