使用IntelliJ 2018.1和Scene Builder 9.0.1更改场景

时间:2018-05-17 10:36:56

标签: java javafx

我创建了一个新的JavaFx项目。 在项目中,我有一个Main类,一个Controller类和一个设计文件。

当我点击“新产品”时,我想加载一个名为new_product.fxml的新fxml文件。

enter image description here

但是当我点击“新产品”按钮时出现错误:

Caused by: java.lang.NullPointerException
    at sample.Main.changeScene(Main.java:33)
    at sample.Controller.buttonNewProductOnMouseClicked(Controller.java:21)

在Main.java第33行是primaryStage。看起来我的控制器无法访问它?

package sample;

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

import java.io.IOException;

public class Main extends Application {

    Stage primaryStage;

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Scene primaryScene = new Scene(root, 300, 275);

        primaryStage.setScene(primaryScene);
        primaryStage.show();

    }
    public void changeScene(String fxml){
        Parent pane = null;
        try {
            pane = FXMLLoader.load(getClass().getResource(fxml));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene( pane );
        primaryStage.setScene(scene);
    }

    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.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="buttonProducts" mnemonicParsing="false" onMouseClicked="#buttonProductsOnMouseClicked" text="Products" />
            <Button fx:id="buttonNewProduct" mnemonicParsing="false" onMouseClicked="#buttonNewProductOnMouseClicked" text="New products">
               <HBox.margin>
                  <Insets left="5.0" />
               </HBox.margin></Button>
         </children>
      </HBox>
   </top>
   <center>
      <Label text="Label" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

设计文件的Controller

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class Controller {

    @FXML
    private Button buttonProducts;

    @FXML
    private Button buttonNewProduct;

    public void buttonProductsOnMouseClicked(javafx.scene.input.MouseEvent mouseEvent) {
        buttonProducts.setText("You have clicked on me!");
    }
    public void buttonNewProductOnMouseClicked(javafx.scene.input.MouseEvent mouseEvent) {
        buttonNewProduct.setText("You have clicked on me!");

        Main mainclass = new Main();
        mainclass.changeScene("new_product/new_product.fxml");
    }
}

0 个答案:

没有答案