从另一个场景打开一个新场景(有自己的控制器)(有自己的控制器) [英] Open a new scene(has its own controller) from another scene(has its own controller)

查看:324
本文介绍了从另一个场景打开一个新场景(有自己的控制器)(有自己的控制器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手并且仍然在学习我遇到了这个问题并找不到答案。

I'm new to JavaFX and still learning I have this problem and couldn't find an answer.

我有两个名为one.fxml的FXML文件和two.fxml,他们有自己的控制器类命名OneController.java和TwoController.java

I have two FXML files named "one.fxml" and "two.fxml" and they have their own controller classes naming "OneController.java" and "TwoController.java"

我想打开一个两个。 fxmlwith onButtonClick fromone.fxml。

I'm want to open a "two.fxml" with onButtonClick from "one.fxml".

one.fxml中的按钮代码为:

The Button Code in "one.fxml" is:

<Button fx:id="clearButton" layoutX="690.0" layoutY="309.0" minHeight="18.0" mnemonicParsing="false" prefHeight="40.0" prefWidth="196.0" text="Clear" onAction="#buttonclearClick"/>

我在OneController.java中有这个方法

and I have this method in "OneController.java"

private void buttonclearClick(final ActionEvent event)
{
  //code to be written?
}

我如何从one.fxml转移某些字段的值totwo.fxml

also how do I transfer values of the certain fields from "one.fxml" to "two.fxml"

like:

如果one.fxml也有此标记:

if "one.fxml" also has this tag:

<Text fx:id="textLabel" fill="#001f4b" layoutX="14.0" layoutY="377.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Powered by StackOverflow">

如何将此文本字段值(text =Powered by StackOverflow)传输到two。 fxml或TwoController.java

how do I transfer this text field value(text="Powered by StackOverflow") to "two.fxml" or TwoController.java

如何实现这个目标?

推荐答案

一个控制器

            private void buttonclearClick(final ActionEvent event)
            {
              try
              {

                FXMLLoader loader = new FXMLLoader(getClass().getResource("two.fxml"));
                Stage stage = new Stage();
                stage.initModality(Modality.APPLICATION_MODAL);
                //stage.initOwner(MainStage.stage);
                stage.setScene(new Scene((Pane)loader.load()));
                TwoController tsc = loader.<TwoController>getController();
                tsc.GettextVal(textLabel.getText()); // Function in TwoController
                stage.show();

              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
            }

两个控制器

在两个控制器中创建一个函数Ge​​ttextVal

Make a function GettextVal in two controller

          @FXML
          void initialize() 
          {
             //Initialize code here
          }

          public void GettextVal(String txtval)
          {
             System.out.println("text value from one controller - "+txtval);
          }

这篇关于从另一个场景打开一个新场景(有自己的控制器)(有自己的控制器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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