【问题标题】:Set the content of a pane with the pane of another FXML file使用另一个 FXML 文件的窗格设置窗格的内容
【发布时间】:2016-11-19 14:54:04
【问题描述】:

我的问题和这两个差不多:

-JavaFX how to inject new FXML content to current Scene

-set the content of a anchorPane with fxml file

我无法使用这些答案,因为我不理解它们。 我有两个(以及更多)FXML 文件,其中一个用作“主”文件,它有一个窗格,应将另一个 FXML 文件的内容添加到其中。

我应该如何实现这个?

【问题讨论】:

    标签: fxml


    【解决方案1】:

    程序入口点:

    public class App extends Application {
    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent startScreen
     = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
        Scene scene = new Scene(startScreen);
        primaryStage.setScene(scene);
        primaryStage.show();
    
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    }
    

    主屏幕控制器

    public class MainScreenController implements Initializable{
    
    private static AnchorPane contentBox;
    
    @FXML
    private AnchorPane paneContent;
    
    public MainScreenController() throws IOException {
        this.paneContent = FXMLLoader.load(getClass().getResource("Home.fxml"));
    
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
         MainScreenController.contentBox = this.paneContent;
    }  
    
    public static AnchorPane getContentBox(){
        return MainScreenController.contentBox;
    }
    
    }
    

    然后 MainScreen.fxml 需要将 MainScreenController 作为控制器,还需要包含一个带有 fx:id paneContent 的 AnchorPane。 然后,您可以从程序中的任何位置调用 getContentBox() 并使用 .set() 来更改屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多