Screenshot of errorsProbleme mit zwei fxml-Dateien und Controller in JavaFX
Ich habe Hauptklasse. Es befindet sich im Paket "view"
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
try {
AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("start.fxml"));
Scene scene = new Scene(root, 757, 562);
primaryStage.setScene(scene);
primaryStage.setTitle("Course Work");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Es ist erfolgreich laden Datei start.fxml. Welche befindet sich auch im Paket "view".
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="562.0" prefWidth="757.0" styleClass="anchor-pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Start">
<children>
<Button layoutX="48.0" layoutY="466.0" mnemonicParsing="false" onAction="#start" text="Ок">
<font>
<Font size="14.0" />
</font>
</Button>
</children>
</AnchorPane>
Controller für diese start.fxml befindet sich im Paket "Controller".
public class Start {
private Stage stage;
@FXML
public void start(ActionEvent event) throws Exception {
try {
AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 757, 562);
stage.setScene(scene);
stage.setTitle("Course Work");
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Methode "Start" möchte ich neue Bühne mit sample.fxml erstellen. Allerdings hatte ich einige Probleme damit. Zum Beispiel - java.lang.NullPointerException. Wie kann ich es passen?
Was „Probleme“ Sie genau haben, werden? – m0skit0
Wo stellst du 'stage' in' Start' ein? – Edwardth
In Start.java Bühne Objekt ist nicht initialisiert. Ordnen Sie in der Main-Klasse primaryStage einem statischen Stage-Objekt zu und verwenden Sie es in der start-Klasse. Wenn Sie eine neue Bühne erstellen möchten, erstellen Sie eine neue Bühne um eine neue Bühne() – Shiva