2

コントローラーでビューメソッドを呼び出したいのですが、方法がわかりません:)例のように探しましたが、見つかりませんでした。このコードでそれを行うことはできますか? 私はそれを新たに構築する必要がありますか?私は javafx と fxml テクノロジを使用しています (ユーザー インターフェイスを構築するため)。

私のビューファイル( gotoRegister() と gotoLogin() メソッドがあります(それらを呼び出したい))

public class FXMLExampleMVC extends Application{

    protected Parent root;
    @Override
    public void start(Stage stage) throws Exception {
        gotoLogin();

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("JavaFX Welcome!");
        scene.getStylesheets().add(FXMLExampleMVC.class.getResource("cssforapp.css").toExternalForm());

        stage.show();
    }

    public void gotoRegister() throws IOException{
        root = FXMLLoader.load(getClass().getResource("RegisterFXML.fxml"));  
    }
    public void gotoLogin() throws IOException{
        root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
    }

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

私のコントローラー(ここでは gotoRegister() メソッドを呼び出したい)

public class SampleController {

    public SampleModel model = new SampleModel();
    @FXML
    protected Text actiontarget;
    @FXML
    protected PasswordField passwordField;
    @FXML
    protected TextField loginField;

    @FXML protected void handleSubmitButtonAction(){
        if((loginField.getText().equals(model.returnLogin()))&&(passwordField.getText().equals(model.returnPass())) ){
            actiontarget.setText("You have access !");
        } else {
           actiontarget.setText("Wrong data !"); 
        }

    }
    @FXML protected void handleSubmitButtonRegister() throws IOException{
        // 
       //Here I want to invoke gotoRegister
      //
    }
}

私の質問: gotoRegister を呼び出すことはできますか? または、fxmlファイルを(コントローラーから)変更する他の方法はありますか?

4

1 に答える 1

5

このコードを FXMLExampleMVC.java に入れます

private static FXMLExampleMVC instance;
public FXMLExampleMVC() {
           instance = this;
}
// static method to get instance of view
public static FXMLExampleMVC getInstance() {
        return instance;
}

これで、このようにコントローラーでビューメソッドを呼び出すことができます

  @FXML protected void handleSubmitButtonRegister() throws IOException{
        // 
       //Here I want to invoke gotoRegister
        FXMLExampleMVC.getInstance().gotoRegister();
    }
于 2012-12-28T02:40:58.777 に答える