如何在JavaFX中从SceneBuilder访问UI组件 [英] How to access UI components from SceneBuilder in JavaFX

查看:53
本文介绍了如何在JavaFX中从SceneBuilder访问UI组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(重复并已解决-参见下面的答案)

(DUPLICATE & SOLVED - see answer below)

我正在JavaFX中迈出第一步,似乎很难使用"SceneBuilder".我已经习惯了Android和QtCreator.在我看来,访问UI组件要容易得多.

I'm doing my first steps in JavaFX and it seems quite hard to use the "SceneBuilder". I'm used to Android and the QtCreator. It looks to me that there is accessing the UI components much easier.

类似于 findViewById(R.id.btnPushMe); 的<-Android代码

Something like findViewById(R.id.btnPushMe); <- Android Code

实际上我有一个解决方案,但是使用起来非常不舒服.看起来像这样:

Actually I got an solution but it is quite uncomfortable to use. This looks as this:

FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("../fmxl/main.fxml"));

AnchorPane pane = loader.load();
System.out.println("panechilds:" + pane.getChildren().size());

BorderPane border = (BorderPane) pane.getChildren().get(0);
System.out.println("borderchilds:" + border.getChildren().size());

xml ..

<AnchorPane fx:id="mAnchor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0"
            xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.progui.MainController">
    <children>
        <BorderPane layoutX="-1.0" prefHeight="600.0" prefWidth="800.0">
            <top>

               ...

先谢谢了马丁

这是一个重复的问题(但我不会删除它,因为我花了一些时间来找到答案-也许是因为JavaFX的询问不如Android问题多.)

This is a duplicate question (but I will not delete it, because I took some time to find the answer - maybe because JavaFX wasn't asked as much as Android questions were..)

AnchorPane anchor = (AnchorPane) scene.lookup("#mAnchor");

在此处找到:如何使用JavaFX中的ID?

推荐答案

您应该使用

You should use a controller class and access the UI elements there.

基本上,您可以这样做:

Basically you do:

<AnchorPane fx:id="mAnchor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0"
            xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.progui.MainController">
    <children>
        <BorderPane fx:id="border" layoutX="-1.0" prefHeight="600.0" prefWidth="800.0">
            <top>

               ...

然后您可以使用以下命令访问控制器中 fx:id 归因的元素

And then you can access the fx:id-attributed elements in the controller with

package app.progui ;

// ...

public class MainController {

    @FXML
    private BorderPane border ;


    public void initialize() {
        border.setStyle("-fx-background-color: antiquewhite;");
        // ...
    }

    // ...
}

控制器类中的字段名称必须与FXML文件中的 fx:id 值匹配.

The field names in the controller class must match the fx:id values in the FXML file.

可以在调用 FXMLLoader 的类中访问 fx:id 归因的元素,但是如果您需要这样做,通常表明您的整体设计是错误的.您可以这样做:

It is possible to access the fx:id-attributed elements in the class that invoked the FXMLLoader, but if you need to do this it is usually a sign that your overall design is wrong. You can do:

FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("../fmxl/main.fxml"));

AnchorPane pane = loader.load();
Map<String, Object> fxmlNamespace = loader.getNamespace();
BorderPane border = (BorderPane) fxmlNamespace.get("border");

假定上面截断的FXML中定义的 fx:id .

assuming the fx:id defined in the FXML snipped above.

这篇关于如何在JavaFX中从SceneBuilder访问UI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆