java FXMLLoader 如何通过 FXID 访问组件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26962788/
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
FXMLLoader how to access the components by FXID?
提问by JavaPanic
I'm trying to figure out how to work with JavaFx. I built the application interface in Scene Builder. But I can not get access to the component, since all loaded into the Parent.
我想弄清楚如何使用 JavaFx。我在 Scene Builder 中构建了应用程序界面。但是我无法访问该组件,因为所有组件都已加载到父级中。
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
If I change "Parent" on "Pane" I can get access to the getChildren(), but it is not clear how to get control if i know fx:id...
如果我更改“Pane”上的“Parent”,我可以访问 getChildren(),但是如果我知道 fx:id ,我不清楚如何获得控制权...
The question is even simpler. I added Label or TextField in Scene Builder. How do I change it's text from the code if I know the fx:id?
问题就更简单了。我在 Scene Builder 中添加了 Label 或 TextField。如果我知道 fx:id,如何更改代码中的文本?
I am in despair.
我很绝望。
回答by James_D
You should create a controller class for your FXML document, in which you can perform any functionality you need to perform involving the UI components. You can annotate fields in that class with @FXML
and they will be populated by the FXMLLoader
, matching the fx:id
attribute to the field name.
您应该为 FXML 文档创建一个控制器类,您可以在其中执行您需要执行的涉及 UI 组件的任何功能。您可以使用 注释该类中的字段,@FXML
它们将由 填充FXMLLoader
,将fx:id
属性与字段名称匹配。
Work through the tutorialfor more details, and have a look at the Introduction to FXML documentation.
Simple example:
简单的例子:
Sample.fxml:
示例.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController">
<Label fx:id="countLabel"/>
<Button fx:id="incrementButton" text="Increment" onAction="#increment"/>
</VBox>
SampleController.java:
示例控制器.java:
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class SampleController {
private int count = 0 ;
@FXML
private Label countLabel ;
@FXML
private void increment() {
count++;
countLabel.setText("Count: "+count);
}
}
SampleMain.java:
SampleMain.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SampleMain extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Sample.fxml")), 250, 75);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
回答by Adam
FXMLLoader.getNamespace()can be used, this is a map of named components.
可以使用FXMLLoader.getNamespace(),这是命名组件的映射。
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
TextField foo = (TextField)loader.getNamespace().get("exampleFxId");