我一直在想如何處理另一個FXML文件中的FXML文件。但我有一個例外。 :( 這是我的內FXML(NewInside.fxml鑑於封裝):java.lang.NullPointerException在使用FXML處理FXML中的JavaFx中
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="305.0" prefWidth="360.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewInsideController">
<children>
<Button fx:id="newBtn" layoutX="30.0" layoutY="202.0" mnemonicParsing="false" onAction="#btnClick" text="Button" />
<TextField fx:id="newTxt" layoutX="30.0" layoutY="134.0" prefHeight="25.0" prefWidth="280.0" />
<Label fx:id="newLbl" layoutX="30.0" layoutY="62.0" prefHeight="17.0" prefWidth="280.0" />
</children>
</AnchorPane>
這是它的控制器(NewInsideController.java在控制器封裝):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class NewInsideController implements Initializable{
@FXML public static Label newLbl;
@FXML public static TextField newTxt;
@FXML public static Button newBtn;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
private void btnClick(ActionEvent e){
System.out.println("Button is clicked!");
newLbl.setText(newTxt.getText());
}
}
這是外FXML(鑑於包New.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="380.0" prefWidth="529.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewController">
<children>
<fx:include source="NewInside.fxml" />
</children>
</AnchorPane>
這是它的控制器(NewController.java在控制器封裝):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class NewController implements Initializable {
/*@FXML private Label newLbl;
@FXML private Button newBtn;
@FXML private TextField newTxt;
*/
@Override
public void initialize(URL location, ResourceBundle resources) {
}
/*@FXML
public void btnClick(ActionEvent e){
newLbl.setText(newTxt.getText());
}*/
}
最後,這是Main.java在應用程序包:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/view/New.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
當我點擊按鈕,它給了我很長的例外: 「在線程異常‘的JavaFX應用程序線程的’java。 lang.RuntimeException:java.lang.reflect.InvocationTargetException .... ..... 由...引起:java.lang.NullPointerException at controller.NewInsideController.btnClick(NewInsideController.java:27) ... 57 more 「
請幫幫我! :(
我只是個初學者,但只是一種猜測:從中刪除靜態字FXML變量? – Inge 2015-02-07 09:08:31