程序入口點:
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);
}
}
MainScreenController
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的AnchorPane:ID paneContent。 然後從程序中的任何位置調用getContentBox()並使用.set()更改屏幕。