複数の FXML ファイルを含むアプリケーションを実装しています (現時点では 2 つの異なる FXML ファイルしかありません)。実装が安全かどうか、またはそれを改善するために追加のスタッフが必要かどうかを尋ねたいと思います。 . まず、メイン シーンとそのコントローラー クラスがあります。
アプリケーション クラス
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent rootPane = FXMLLoader.load(getClass().getResource("MainScene.fxml"));
// Main scene
Scene scene = new Scene(rootPane);
primaryStage.setScene(scene);
primaryStage.show();
MyControllerClass.startGame();
}
public static void main(String[] args) {
launch(args);
}
}
コントローラ クラス
public class MyControllerClass implements Initializable {
// Second Stage will be called later.
public static Stage SecondStage;
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
//TODO
SecondStage = new Stage();
// Init Modality to use showAndWait() method
SecondStage .initModality(Modality.APPLICATION_MODAL);
try {
Parent childPane= FXMLLoader.load(getClass().getResource("ChildScene.fxml"));
SecondStage.setScene(new Scene(childPane));
} catch (IOException ex) {
Logger.getLogger(MyControllerClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void startGame() {
// Start the game
...
AttackSimulator.simulateAttack(0,0);
}
..
}
したがって、これまでのところすべて問題ないように見えますが、アプリケーションのいくつかのステップの後、メインシーンを破棄せずに 2 番目の FXML ファイルを使用する必要があります (実際にはその前に)。ここで、いくつかの静的メソッドを含むクラスがあるとします。メイン シーンの前に、いくつかの更新されたコンポーネントを含む 2 番目の FXML ファイルを表示します。お気に入り;
AttackSimulator クラス
public class AttackSimulator {
public static void simulateAttack(int attackFrom, int attackTo){
label1.setText("foobar1");
label2.setText("foobar2");
label3.setText("foobar3");
label4.setText("foobar4");
MyControllerClass.SecondStage.showAndWait();
}
@FXML
private void ButtonActionHandler(ActionEvent event){
// Do some calculations
...
MyControllerClass.SecondStage.hide();
}
@FXML private static Label label1;
@FXML private static Label label2;
@FXML private static Label label3;
@FXML private static Label label4;
}
また、アクション ハンドラ メソッドとインポートされたコンポーネントが含まれているため、クラスは のコントローラ クラスAttackSimulator
として定義されます。ChildScene.fxml
長い質問だと思いますが、メイン シーンのコントローラー クラスに別の fxml ファイルをロードすることに懸念があるため、この実装が安全なものに見えるかどうかを知りたいです。私はすべての応答に感謝します。とにかくありがとう。