在JavaFX / FXML / SceneBuilder中在屏幕之间传递参数 [英] Passing Parameters between screens in JavaFX/FXML/SceneBuilder

查看:431
本文介绍了在JavaFX / FXML / SceneBuilder中在屏幕之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SceneBuilder和FXML构建一个简单的概念验证程序,它由两个屏幕组成。第一个屏幕只是一个文本字段和一个按钮,可以将您带到第二个屏幕,第二个屏幕只有一个标签,理想情况下,当按钮被按下时,它将显示文本字段内的任何内容。每个屏幕都有自己的FXML文件和它自己的控制器。关于FXMLLoader,我已经阅读过,关闭和横向阅读,因为我的研究指出这是完成这项工作的理想方式,但我似乎无法辨别如何正确使用它。最终我想在角色扮演游戏的一种角色创建游戏前屏幕中实现这一点,其中玩家统计卷/库存选择从初始屏幕移动到用于计算/处理的模型,或者到第二个屏幕控制器进行显示。

I'm attempting to build a simple proof of concept program using SceneBuilder and FXML, consisting of two screens. The first screen is just a text field and a button that takes you to the second screen, and the second screen has just a label that, ideally, will display whatever was inside of the text field when the button was hit. Each screen has its own FXML file and it's own controller. I've read up, down, and sideways about FXMLLoader, as my research points to that being the ideal way to get this done, but I still cant seem to discern how to properly use it. Ultimately I'd like to implement this in a sort of "character creation" pre-game screen for a role playing game, where the players stat rolls/inventory choices are moved from the initial screen to either a model for calculating/processing, or to the second screens controller for display.

推荐答案

好的,这是各种答案,请耐心等待,我发现了一个视频一个Sahil Huseynzade,我认为最终为我点击了这个。我当时认为我可以使用FXMLLoader作为几乎所有类型的导入,我可以在创建控制器期间以某种方式使用它以便随意访问其他控制器。我在下面复制了一些我使用FXMLLoader的代码,以及一些关于我认为FXMLLoader如何工作的注释。对不起,如果从技术的角度来看这些解释是愚蠢的或措辞不当,或者我是一般的新手/笨蛋。我确实有一个问题,我希望在一个答案中问它是不好的形式,是否有办法使信息在Windows中动态更新?就像我可以让一个窗口有一个骰子滚动按钮,谁的结果自动出现在另一个窗口?

Okay, this is an answer of sorts, bear with me, I found a video by a one Sahil Huseynzade that I think finally made this click for me. I was thinking that I could use FXMLLoader as almost an import of sorts, that I could somehow use during the creation of my controller in order to access other controllers at will. I've copied below some code I got working with an FXMLLoader, with some comments pertaining to how I think FXMLLoader is working. Sorry if the explanations are obtuse or poorly worded from a technical standpoint, or if I've been a general novice/dunce throughout all of this. I did have a further question though, I expect that it's bad form to ask it within an answer, is there a way to make it so that information is updated dynamically across windows? Like could I have one window have a dice rolling button, who's results appear on another window automatically?

package twoscreentest;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class Screen1Controller implements Initializable {

    @FXML
    public Label label;
    @FXML
    public Button button;
    @FXML
    private TextField textCharName;

    public String testtext;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        label.setText(Byte.toString((byte) (Math.floor(Math.random()*6)+1)));
    }    

    @FXML
    private void btnSend(ActionEvent event) throws IOException {
        PlayerInfo player = new PlayerInfo(textCharName.getText(), Double.parseDouble(label.getText()));  // Creating the temporary container.
        //((Node)event.getSource()).getScene().getWindow().hide(); This is to close the previous window, for testing it was easier to disable it.
        FXMLLoader loader = new FXMLLoader(); // Creating a the FXMLLoader
        loader.setLocation(getClass().getResource("Screen2.fxml")); // Telling it to get the proper FXML file.
        loader.load(); // Loading said FXML.
        Parent p = loader.getRoot(); // Setting up the window, I think I get how this works,
        //probably...
        Stage stage = new Stage();
        stage.setScene(new Scene(p));
        Screen2Controller screen2controller = loader.getController(); // This right here I 
        //don't entirely get, I know that I'm using the loader to get the controller, but
        //what's with the "NameofControllerClass variablename"?
        screen2controller.setCurrentInfo(player); // Setting the empty container in
        // the second class' variables to the ones in the the temporary container that
        // I created earlier.
        screen2controller.testlabel.setText(testtext); // An additional test, setting a label
        // on the second screen to a variable I set in this controller with a separate button.
        stage.show(); // Create the second screen.

    }

    @FXML
    private void btnSend2(ActionEvent event) {

        testtext = "Hello world";
    }

}

这篇关于在JavaFX / FXML / SceneBuilder中在屏幕之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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