Javafx:初始化

时间:2018-04-30 11:18:51

标签: java javafx nullpointerexception

我目前正在用javaFx编写一个小的Gui应用程序。 我有一个类应用程序扩展应用程序,它是HauptFenster.fxml的控制器。 这个类使用菜单栏和tabPane构建一个Window。 然后我从Uebersicht.fxml添加SplitPane。 Uebersicht.fxml的控制器是Uebersicht.java。 到此为止一切正常。 但是,如果我单击一个调用Method的按钮,App中的所有字段都为null。 因此,在调用方法“start”之后,JavaFX线程将删除所有字段。 你可以用调试器看到这个。为什么会这样? P.S:我删除了所有不必要的代码,因此它只显示错误,程序没有像这里显示的任何实际意义。

App.Java:

    import java.io.IOException;
    import javafx.application.Application;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.control.SplitPane;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    import view2.Uebersicht;

    public class App extends Application {

        private Stage primaryStage;
        private BorderPane rootLayout;
        private SplitPane uebersichtPane;
        private Uebersicht uebersicht;
        private ObservableList<Tab> tabs;


        public App() {

        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            this.primaryStage = primaryStage;
            this.primaryStage.setTitle("FunktionsApp");

            initHauptfenster();
            initÜbersicht();
        }

        private void initHauptfenster() {
            try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(App.class.getResource("Hauptfenster.fxml"));
                rootLayout = (BorderPane) loader.load();

                tabs= ((TabPane)  rootLayout.getCenter()).getTabs();

                // Show the scene containing the root layout.
                Scene scene = new Scene(rootLayout);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void initÜbersicht() {
            try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(App.class.getResource("Uebersicht.fxml"));
                uebersichtPane= (SplitPane) loader.load();
                tabs.get(0).setContent(uebersichtPane);
                uebersicht = loader.getController();
                uebersicht.initText();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @FXML 
        private void deleteFunktion() {
            uebersicht.initText();
        }
        @FXML
        public void speichern() {

            uebersicht.initText();
        }
        @FXML
        public void laden() {
            uebersicht.initText();
        }
        public static void main(String[] args) {
            launch(args);
        }
    }

Uebersicht.java

import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import view2.App;

public class Uebersicht {

    @FXML
    private Label lblStamm;
    @FXML
    private Label lblF;
    @FXML
    private Label lblF1;
    @FXML
    private Label lblF2;
    @FXML
    private Canvas canvas;


    public void initText() {
            lblStamm.setText("F(x)=");
            lblF.setText("f(x)=");
            lblF1.setText("f'(x)=");
            lblF2.setText("f''(x)=");
    }
}

HauptFenster.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="630.0" prefWidth="820.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view2.App">
   <center>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab text="Übersicht" />
          <Tab text="Kurvendiskussion" />
            <Tab text="Eingabe" />
        </tabs>
      </TabPane>
   </center>
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="Datei">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#speichern" text="Speichern" />
                  <MenuItem mnemonicParsing="false" onAction="#laden" text="Laden" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#deleteFunktion" text="Funktion löschen" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

Uebersicht.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<SplitPane dividerPositions="0.7264367816091954" orientation="VERTICAL" prefHeight="437.0" prefWidth="552.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view2.Uebersicht">
  <items>
      <BorderPane>
         <center>
            <Canvas fx:id="canvas" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
      <ScrollPane vbarPolicy="NEVER">
         <content>
            <BorderPane>
               <center>
                  <GridPane BorderPane.alignment="CENTER">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" percentWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints vgrow="NEVER" />
                      <RowConstraints minHeight="0.0" prefHeight="6.0" vgrow="NEVER" />
                      <RowConstraints vgrow="NEVER" />
                        <RowConstraints minHeight="0.0" prefHeight="6.0" vgrow="NEVER" />
                        <RowConstraints vgrow="NEVER" />
                        <RowConstraints minHeight="0.0" prefHeight="6.0" vgrow="NEVER" />
                        <RowConstraints vgrow="NEVER" />
                    </rowConstraints>
                     <children>
                        <Label fx:id="lblStamm" text="Label" wrapText="true">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="lblF" text="Label" wrapText="true" GridPane.rowIndex="2">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="lblF1" text="Label" wrapText="true" GridPane.rowIndex="4">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="lblF2" text="Label" wrapText="true" GridPane.rowIndex="6">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Separator prefWidth="200.0" GridPane.rowIndex="1" />
                        <Separator prefWidth="200.0" GridPane.rowIndex="3" />
                        <Separator prefWidth="200.0" GridPane.rowIndex="5" />
                     </children>
                     <padding>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </padding>
                  </GridPane>
               </center>
            </BorderPane>
         </content>
      </ScrollPane>
  </items>
</SplitPane>

0 个答案:

没有答案