2015-04-17 31 views
5

在JavaFX中關閉窗口時遇到問題。如何解鎖內部關閉請求?

我按我的意願定義我的setOnCloseRequest,當它在窗口中點擊x時它就起作用。不過,我還需要一個按鈕來關閉窗口,並且此onCloseRequest必須工作,問題是它不。事件根本不會發生。

我使用JavaFX 2.2(Java 7中),我注意到,爲setOnCloseRequest參考稱關閉該窗口上外部請求

回答

10

解決方案

火從內部關閉請求事件(在按鈕上按下),以便應用程序認爲它收到了外部關閉請求。那麼無論請求來自外部事件還是內部事件,您的關閉請求邏輯都可以是相同的。

private EventHandler<WindowEvent> confirmCloseEventHandler = event -> { 
     // close event handling logic. 
     // consume the event if you wish to cancel the close operation. 
} 

... 

stage.setOnCloseRequest(confirmCloseEventHandler); 

Button closeButton = new Button("Close Application"); 
closeButton.setOnAction(event -> 
     stage.fireEvent(
       new WindowEvent(
         stage, 
         WindowEvent.WINDOW_CLOSE_REQUEST 
       ) 
     ) 
); 

這是一個Java 8+的解決方案,爲JavaFX 2,您需要將拉姆達職能轉換中的匿名內部類,將無法使用該警報對話框反而會需要提供您自己的警報對話系統,因爲JavaFX 2沒有內置功能。我強烈建議升級到Java 8+,而不是使用JavaFX住2

樣品UI

close app

close confirm

示例代碼

的示例代碼會顯示如果用戶沒有確認t,則用戶關閉確認提醒並取消關閉請求他閉上。

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.StackPane; 
import javafx.stage.*; 
import javafx.stage.WindowEvent; 

import java.util.Optional; 

public class CloseConfirm extends Application { 

    private Stage mainStage; 

    @Override 
    public void start(Stage stage) throws Exception { 
     this.mainStage = stage; 
     stage.setOnCloseRequest(confirmCloseEventHandler); 

     Button closeButton = new Button("Close Application"); 
     closeButton.setOnAction(event -> 
       stage.fireEvent(
         new WindowEvent(
           stage, 
           WindowEvent.WINDOW_CLOSE_REQUEST 
         ) 
       ) 
     ); 

     StackPane layout = new StackPane(closeButton); 
     layout.setPadding(new Insets(10)); 

     stage.setScene(new Scene(layout)); 
     stage.show(); 
    } 

    private EventHandler<WindowEvent> confirmCloseEventHandler = event -> { 
     Alert closeConfirmation = new Alert(
       Alert.AlertType.CONFIRMATION, 
       "Are you sure you want to exit?" 
     ); 
     Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
       ButtonType.OK 
     ); 
     exitButton.setText("Exit"); 
     closeConfirmation.setHeaderText("Confirm Exit"); 
     closeConfirmation.initModality(Modality.APPLICATION_MODAL); 
     closeConfirmation.initOwner(mainStage); 

     // normally, you would just use the default alert positioning, 
     // but for this simple sample the main stage is small, 
     // so explicitly position the alert so that the main window can still be seen. 
     closeConfirmation.setX(mainStage.getX()); 
     closeConfirmation.setY(mainStage.getY() + mainStage.getHeight()); 

     Optional<ButtonType> closeResponse = closeConfirmation.showAndWait(); 
     if (!ButtonType.OK.equals(closeResponse.get())) { 
      event.consume(); 
     } 
    }; 

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

} 
+0

Little note,OP is using JavaFX 2. –

+0

謝謝Uluk,我錯過了關於javafx-2標籤的問題。我更新了答案,以討論留在JavaFX 2中的潛在影響。 – jewelsea