亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在再次打開舞臺之前檢查舞臺是否已經打開 javafx

在再次打開舞臺之前檢查舞臺是否已經打開 javafx

catspeake 2021-11-03 16:22:34
我試圖通過單擊一個按鈕來打開一個舞臺,但在打開它之前,我想檢查舞臺是否已經打開,然后將打開的舞臺彈出到前面而不是打開一個新的舞臺(沒有相同的多重打開)階段)。@FXMLprivate void btn_Validate(ActionEvent event) {    try {        FXMLLoader loader = new FXMLLoader(getClass().getResource("/scontrols/students/StudentManagement.fxml"));        Parent root = (Parent) loader.load();        StudentManagementController sendTo =  loader.getController();        sendTo.receiveFromCamera(txtPictureName.getText());        Stage stage = new Stage();        stage.setScene(new Scene(root));         if(!stage.isShowing())         {             stage.show();}    } catch (IOException ex) {        Logger.getLogger(WebCamController.class.getName()).log(Level.SEVERE, null, ex);    }}
查看完整描述

1 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

您正在檢查!stage.isShowing()在新創建的 Stage。這永遠不會做你想做的。您需要保留對另一個的引用Stage并繼續使用該引用。


public class Controller {


  private Stage otherStage;


  @FXML

  private void btn_Validate(ActionEvent event) {

    if (otherStage == null) {

      Parent root = ...;


      otherStage = new Stage();

      otherStage.setScene(new Scene(root));

      otherStage.show();


    } else if (otherStage.isShowing()) {

      otherStage.toFront();

    } else {

      otherStage.show();

    }

  }

}

如果您不想Stage在關閉時將其保留在內存中,那么您可以稍微更改上述內容。


public class Controller {


  private Stage otherStage;


  @FXML

  private void btn_Validate(ActionEvent event) {

    if (otherStage == null) {

      Parent root = ...;


      otherStage = new Stage();


      otherStage.setOnHiding(we -> otherStage = null);


      otherStage.setScene(new Scene(root));

      otherStage.show();


    } else {

      otherStage.toFront();

    }

  }

}

根據您的需要,您可能還想存儲對已加載控制器的引用。


查看完整回答
反對 回復 2021-11-03
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號