javafx如何将变量值从一个控制器传输到另一个控制器 [英] javafx how to transfer variable values from one controller to another

查看:792
本文介绍了javafx如何将变量值从一个控制器传输到另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javafx的新手,我想将变量值从一个控制器转移到另一个控制器,我不知道如何做到这一点。所以请帮助我。

i am new to javafx and i want to transfer variable values from one controller to another and i have no idea how to do this. so please help me.

例如:

我想显示来自的用户名>第一个登录窗口第二个仪表板窗口那么我应该怎么做才能将userid保存在一个变量中并将其发送到第二个窗口并显示在 label

i want to display username from first login window to second dashboard window so what should i do to save userid in one variable and send it to second window and display there in a label.

代码测试:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
 * FXML Controller class
 *
 * @author wabcon
 */
public class AdmissionController implements Initializable {

int userid=0;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    userid=10001;

    }    

}

我怎么可能将此 userid 发送到下一个窗口控制器。
请帮帮我。
谢谢。

how could i send this userid to next window controller. please help me. Thank you.

推荐答案

我假设您正在为自定义组件执行此操作。

因此,您为自定义组件创建了一个类,并将该类设置为控制器:

So, you create a class for your custom component and set that class as the controller:

public class CustomControl extends AnchorPane implements Initializable {
    String customId;

    public CustomControl() {
        //if you want to set a FXML
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/res/customControl.fxml"));
        //Defines this class as the controller
        fxmlLoader.setRoot(this);
        //this.getStylesheets().add("/res/style.css"); <- if you want to set a css
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

    }
        public String getCustomId() {
            return customId;
        }
    public void setCustomId(String customId) {
        return this.customId = customId;
    }
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
          //Initializes the controller
    }
}

在您的MainController上:

On your MainController:

CustomControl c = new CustomControl();
c.setCustomId("StackOverflow");

这篇关于javafx如何将变量值从一个控制器传输到另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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