2014-01-21 79 views

回答

0

試試這個..

AnchorPane pane; // pane where fxml was showing 
    menu1.setOnAction(new EventHandler<ActionEvent>() { 
    @Override 
    public void handle(ActionEvent t) { 
     openNewWindow("m1.fxml"); 

    } 
    }); 
    menu2.setOnAction(new EventHandler<ActionEvent>() { 
    @Override 
    public void handle(ActionEvent t) {    
     openNewWindow("m2.fxml"); 

    }}); 
    menu3.setOnAction(new EventHandler<ActionEvent>() { 
    @Override 
    public void handle(ActionEvent t) {    
     openNewWindow("m3.fxml"); 

    } 
    }); 

功能openNewWindow

public void openNewWindow(String FXMLFile) 
{ 
    //ChildNode child; 
    try {      
     URL url = getClass().getResource(FXMLFile); 
     FXMLLoader fxmlLoader = new FXMLLoader(); 
     fxmlLoader.setLocation(url); 
     fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); 
     AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream()); 

     Pane.getChildren().clear();///name of pane where you want to put the fxml 
     Pane.getChildren().add(page); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
}