2017-06-23 5 views
0

でjavafxのステージを閉じることができません。私はJavaFxの初心者です。ステージを閉じるだけで問題が発生しています。基本的には、確認ボックスコントローラであるクラスを作成しようとしています。 「はい」ボタンとし、「NO」ボタンが、ステージがアプリケーションを終了し、継続すべきいずれかの方法があります:アクションイベント関数

import javafx.event.ActionEvent; 
import javafx.fxml.FXMLLoader; 
// more imports... 

public class ConfirmBoxController implements IView { 

public javafx.scene.control.Button BTN_save; 
public javafx.scene.control.Label label_Message; 

private Stage stage; 
private boolean answer; 
private String title; 
private String message; 

public ConfirmBoxController(){ 
    this("Confirmation","Are you sure?"); 
} 

public ConfirmBoxController(String title, String message){ 

    this.title = title; 
    this.message = message; 
    stage = new Stage(); 
} 

public boolean confirm(){ 
    try{ 
     stage = new Stage(); 
     FXMLLoader fxmlLoader = new FXMLLoader(); 
     Parent root = fxmlLoader.load(getClass().getResource("ConfirmBox.fxml").openStream()); 
     Scene scene = new Scene(root, 250, 140); 
     stage.setTitle(title); 
     stage.setScene(scene); 
     stage.showAndWait(); 

     return answer; 
    } 
    catch(Exception E){ 
     E.printStackTrace(); 
     return true; 
    } 
} 

public void yes(ActionEvent actionEvent) { 
    answer = true; 
    stage.close(); 
} 

public void no(ActionEvent actionEvent) { 
    answer = false; 
    stage.close(); 
} 

注:iViewが空のインターフェースです。 基本的にステージが現れ、ボタンをクリックすることができ、ボタンをクリックすると「はい」と「いいえ」という機能が登録されているので、これが登録されていることがわかります。しかし、何も起こりません。

私は何か基本的なことを監督していると確信していますが、これを正しく行う方法が見つかりませんでした。ありがとうございました。

答えて

0

コントローラクラスの名前をボタンに指定します。 @FXML public Button closeButton;

と、この方法のように追加します。あなたのFXMLで

@FXML 
public void handleCloseButtonAction(ActionEvent event) { 
    Stage stage = (Stage) closeButton.getScene().getWindow(); 
    stage.close(); 
} 

を使用すると、ボタンの名前とonActionを呼び出すメソッドへの参照が必要になります。

<Button fx:id="closeButton" cancelButton="true" layoutX="350.0" layoutY="767.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefWidth="100.0" text="Close" /> 
+0

は素晴らしい仕事こと、ありがとうございます。私は本当に理由を理解していない。 また、関数 "yes"と "no"が "answer"の値を変更しない理由も知っていますか?つまり、confirmは常にデフォルト値の "answer"を返します。 –

関連する問題