我尝试用FXML制作一个应用程序。一开始我有一个舞台有2个场景,当我按下按钮时场景应该会改变 当我按下按钮时,出现此错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at application.Controller.lambda$0(Controller.java:43)
这是我的控制器类:
public class Controller{
public Stage primaryStage;
@FXML
private Button button;
public void plm(Stage primaryStage){
try {
System.out.println(primaryStage);
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,400,400);
//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
this.primaryStage = primaryStage;
System.out.println(this.primaryStage);
} catch(Exception e) {
e.printStackTrace();
}
}
@FXML
public void cc() throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("SampleB.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
// primaryStage.close();
}
这是我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<center>
<Button fx:id="button" mnemonicParsing="false" onAction="#cc" text="Button" BorderPane.alignment="CENTER" />
</center>
</BorderPane>