javafx - JavaFX:尝试在主 fxml 中将元素从另一个加载的 fxml 访问到主 fxml 时出现空指针异常
问题描述
存在一个主 fxml。单击中心按钮时,中心 fxml 文件被加载。单击按钮时,访问文本字段中的数据并设置文本标签。setText 标签不起作用。无法访问。出现空指针异常。
测试.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author root
*/
public class Test extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
主文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<BorderPane prefHeight="374.0" prefWidth="534.0" style="-fx-background-color: grey;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.mainController">
<top>
<HBox prefHeight="51.0" prefWidth="200.0" style="-fx-background-color: white;" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
<children>
<TextField fx:id="tf" prefHeight="52.0" prefWidth="318.0" />
<Button mnemonicParsing="false" onMouseClicked="#handleButtonAction" prefHeight="53.0" prefWidth="52.0" text="Button" />
</children>
</HBox>
</top>
<center>
<BorderPane fx:id="borderpane1" BorderPane.alignment="CENTER">
<center>
<HBox fx:id="hb" prefHeight="100.0" prefWidth="200.0" style="-fx-background-color: white;" />
</center>
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</BorderPane>
</center>
<left>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" onMouseClicked="#handleButtonAction1" prefHeight="43.0" prefWidth="99.0" text="Center" />
</children>
</VBox>
</left>
</BorderPane>
中心.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="336.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.mainController">
<children>
<Label fx:id="label" prefHeight="65.0" prefWidth="229.0" text="Label">
<font>
<Font size="56.0" />
</font>
</Label>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
主控制器.java
package test;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.StringProperty;
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.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class mainController implements Initializable {
public StringProperty nlt(){
return label.textProperty();
}
@FXML
Label label;
@FXML
TextField tf;
@FXML
BorderPane borderpane1;
@FXML
HBox hb;
@FXML
private void handleButtonAction(MouseEvent event)throws IOException {
System.out.println("You clicked me!");
FXMLLoader Home;
Home = new FXMLLoader(getClass().getResource("center.fxml"));
hb.getChildren();
String s=tf.getText();
System.out.println(s);
label.setText(s);
}
@FXML
private void handleButtonAction1(MouseEvent event) throws IOException {
FXMLLoader Home;
Home = new FXMLLoader(getClass().getResource("center.fxml"));
//borderpane1.setCenter(Home);
hb.getChildren().add((Node)Home.load());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
无法更改标签。错误=>空指针异常,java.lang.RuntimeException:java.lang.reflect.InvocationTargetException.....
解决方案
推荐阅读
- c# - 如何在 C# 中使用 WorksheetFunction.CountIf?
- flutter - Flutter - 在日期时间选择器时间轴中仅显示月份
- rust - Rust:用函数修改引用的引用对象;这是否包含UB?
- sql - 记录特定用户访问的单个数据库 postgresql 中所有表的查询
- c# - 如何在 WPF 中的控件上使用 Light?
- python - 对部分多索引数据框的熊猫操作
- javascript - 从 JS 动态创建 svg 不适用于剪辑路径
- c++ - 新行上的 clang-format columnlimit 返回类型
- python-3.x - 升级到 Mac OS 11 (Big Sur) 后,OpenCV 无法与 PyCharm 一起使用
- javascript - 使用 Object.entries 遍历对象