【问题标题】:Change Label text during runtime in javafx在 javafx 运行时更改标签文本
【发布时间】:2018-03-13 19:33:47
【问题描述】:

我在启动方法中加载 FXML1,当单击按钮时,我打开 FXML2。这是我的问题:按下按钮时我可以更改标签“标签”的文本吗?

主要:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class FXMLExample extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

控制器: 在初始化方法中是注释: label.setText("Hello World"); 如果我取消注释并尝试它,我会得到一个异常。

package fxmlexample;

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.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

/**
* FXML Controller class
*/
public class FXMLController implements Initializable {

@FXML
Button button;
@FXML
Label label;

public void pressButton(ActionEvent event) throws Exception {
    if (event.getSource() == button) {
        Parent root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }
}

/**
 * Initializes the controller class.
 *
 * @param url
 * @param rb
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    //   label.setText("Hello World");
}

}

FXML1:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlexample.FXMLController">
<children>
     <Button fx:id="button" layoutX="264.0" layoutY="185.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="31.0" prefWidth="73.0" text="Click me!" />
</children>
</Pane>

FXML2:

 <?xml version="1.0" encoding="UTF-8"?>

 <?import javafx.scene.control.Label?>
 <?import javafx.scene.layout.Pane?>

 <Pane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlexample.FXMLController">
   <children>
       <Label fx:id="label" layoutX="244.0" layoutY="177.0" prefHeight="46.0" prefWidth="112.0" style="-fx-background-color: lime;" />
   </children>
 </Pane>

【问题讨论】:

  • 我猜“更改 fxml”意味着修改从 fxml 创建的场景,而不是 fxml 文件本身?这当然是可能的,但似乎有一些陷阱会导致服务器用户感到困惑,例如创建一个新的控制器实例以与FXMLLoader 创建的另一个控制器实例进行通信,或多次加载 fxml 并尝试使用新的控制器来操作先前加载的场景...请创建一个 minimal reproducible example。这样会更容易提出建议,而不是简单地回答“是”。
  • 是的,正在修改场景。我正在尝试做一个例子。 @fabian

标签: javafx initialization scenebuilder


【解决方案1】:

label 仅在控制器中为 FXML2 初始化(因为 FXML 文件有一个带有 fx:id="label" 的元素)。在两个控制器上调用 initialize() 方法。因此,当在 FXML1 的控制器上调用 initialize() 时,label 为 null,您会得到一个空指针异常。

为每个 FXML 文件使用不同的控制器类:

package fxmlexample;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

/**
* FXML Controller class
*/
public class FXMLController1 implements Initializable {

    @FXML
    Button button;
    public void pressButton(ActionEvent event) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();

    }

    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

}

和

package fxmlexample;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
* FXML Controller class
*/
public class FXMLController2 implements Initializable {

    @FXML
    Label label;



    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
           label.setText("Hello World");
    }

}

最后,修改每个 FXML 文件中的fx:controller 属性以指向正确的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多