Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用自己的FXMLs动态加载新选项卡_Java_Dynamic_Javafx_Tabs_Fxml - Fatal编程技术网

Java 使用自己的FXMLs动态加载新选项卡

Java 使用自己的FXMLs动态加载新选项卡,java,dynamic,javafx,tabs,fxml,Java,Dynamic,Javafx,Tabs,Fxml,我想写一个应用程序,它有几个选项。用户可以在菜单中选择一个选项,并创建一个选项卡,该选项卡从另一个fxml文件加载其视图(,该文件还具有另一个视图控制器)。当前我的代码如下所示: Tab t = new Tab("My New Tab"); try { t.setClosable(true); t.setId("test"); t.setContent(FXMLLoader. load(getClass(). getResource("/pac

我想写一个应用程序,它有几个选项。用户可以在菜单中选择一个选项,并创建一个选项卡,该选项卡从另一个fxml文件加载其视图(,该文件还具有另一个视图控制器)。当前我的代码如下所示:

Tab t = new Tab("My New Tab");
try {
    t.setClosable(true);
    t.setId("test");
    t.setContent(FXMLLoader.
      load(getClass().
        getResource("/package/NewTabView.fxml")));
} catch (Exception e) {

}

tabPane.getTabs().add(t);
selectionModel.selectLast();
希望您能帮助我,因为我遇到以下例外情况:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

正如@DVarga所说,您需要检查
NewTabView.fxml
文件的位置

下面是一个可以帮助您的示例:

  • 这里我们有两个FXML文件及其控制器类
  • firstView
    FXML文件是包含
    TabPane
  • secondView
    FXML文件包含我们将在第一个
    TabPane
  • 在MainApp类中没有什么特别之处,我们只是加载firstView并设置它的控制器
  • 有趣的事情发生在
    createtabdynamicy()
    方法中,我们加载
    secondView
    的FXML文件并设置其控制器,然后实例化一个新选项卡并将secondView设置为其内容,最后将其添加到
    TabPane
FirstView.fxml

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

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

<BorderPane fx:id="container" 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">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="closeMI" mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Action">
            <items>
              <MenuItem fx:id="openTabMI" mnemonicParsing="false" text="Open the new tab" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <center>
      <TabPane fx:id="tabPane" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="ALL_TABS">
        <tabs>
          <Tab fx:id="myTab" closable="false" text="MyTab">
               <content>
                  <VBox>
                     <padding>
                        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                     </padding>
                     <children>
                        <Label text="Hello From the first view" />
                     </children>
                  </VBox>
               </content>
            </Tab>
        </tabs>
      </TabPane>
   </center>
</BorderPane>
SecondViewController.java

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;

public class FirstViewController implements Initializable {

    @FXML private MenuItem openTabMI, closeMI;
    @FXML private TabPane tabPane;
    private Tab myDynamicTab;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        openTabMI.setOnAction((event)->{
            createTabDynamically();
        });

        closeMI.setOnAction((event)->{Platform.exit();});
    }

    private void createTabDynamically() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("secondView.fxml"));
        loader.setController(new SecondViewController());
        try {
            Parent parent = loader.load();
            myDynamicTab = new Tab("A Dynamic Tab");
            myDynamicTab.setClosable(true);
            myDynamicTab.setContent(parent);
            tabPane.getTabs().add(myDynamicTab);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class SecondViewController implements Initializable {

    @FXML private Label secondInfoLbl;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        secondInfoLbl.setText("Hello from the second view");
    }
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

    public static void main(String [] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("FirstView.fxml"));
        FirstViewController firstViewController = new FirstViewController();
        loader.setController(firstViewController);
        Parent parent = loader.load();
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

}
MainApp.java

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;

public class FirstViewController implements Initializable {

    @FXML private MenuItem openTabMI, closeMI;
    @FXML private TabPane tabPane;
    private Tab myDynamicTab;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        openTabMI.setOnAction((event)->{
            createTabDynamically();
        });

        closeMI.setOnAction((event)->{Platform.exit();});
    }

    private void createTabDynamically() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("secondView.fxml"));
        loader.setController(new SecondViewController());
        try {
            Parent parent = loader.load();
            myDynamicTab = new Tab("A Dynamic Tab");
            myDynamicTab.setClosable(true);
            myDynamicTab.setContent(parent);
            tabPane.getTabs().add(myDynamicTab);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class SecondViewController implements Initializable {

    @FXML private Label secondInfoLbl;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        secondInfoLbl.setText("Hello from the second view");
    }
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

    public static void main(String [] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("FirstView.fxml"));
        FirstViewController firstViewController = new FirstViewController();
        loader.setController(firstViewController);
        Parent parent = loader.load();
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

}

实际上哪一行抛出异常?我猜是“加载(getClass().getResource(“/package/NewTabView.fxml”);”。你的档案在哪里?因为现在它表示您正在将其存储在“\package\”中