繁体   English   中英

JavaFX关闭窗口并在窗口中的标题栏AND按钮中用“x”按钮打开另一个窗口

[英]JavaFX close window and open another one by “x” Button in title bar AND button in window

在我的应用程序中,当用户按下按钮时,当前窗口将隐藏/关闭并打开一个新窗口。 在这个新窗口中是一个“退出”按钮。 当用户单击它时,它还将关闭/隐藏当前窗口并“重新打开”父窗口。 我在标题栏中的“x”按钮上的行为相同。 目前我通过为两个按钮/事件设置不同的代码块来解决它。 由于动作的代码大致相同,我的目标是只有一个代码块来处理“退出”按钮和标题栏中的“x”按钮。

这是我到目前为止的代码:

import com.sun.deploy.association.Action;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
import javafx.scene.control.DialogEvent;
import javafx.scene.control.DialogPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

/**
 *
 * @author Mike
 */
public class FXMLBlahBLahUIController implements Initializable {

    @FXML
    private MenuItem FileMenuCloseItem;

    @FXML
    private MenuItem HelpMenuAboutItem;

    @FXML
    private javafx.scene.layout.BorderPane BlahBLahUIMainWindow;

    @FXML
    private javafx.scene.control.Button BackupTaskExitButton;

    @FXML
    private void handleButtonActionMenuFileClose(ActionEvent event) {
        Platform.exit();
    }

    @FXML
    private void handleButtonActionMenuHelpAbout(ActionEvent event) throws Exception {

        // Decalaration of Variables
        DialogPane pane;
        Dialog<ButtonType> dia;

        // Execution Block
        pane = FXMLLoader.load(getClass().getResource("FXMLBlahBLahUIHelpAbout.fxml"));
        dia = new Dialog();
        dia.setDialogPane(pane);
        dia.setContentText(pane.getContentText());
        dia.setResizable(false);
        dia.initStyle(StageStyle.UNDECORATED);
        dia.showAndWait();

    }

    @FXML
    private void handleButtonActionTaskBackup(ActionEvent event) throws Exception {

        // Decalaration of Variables
        FXMLLoader pane;
        Parent backup;
        Stage stage, stage1;

        // Execution Block
        pane = new FXMLLoader(getClass().getResource("FXMLBlahBLahUIBackup.fxml"));
        backup = (Parent) pane.load();
        stage = new Stage();
        stage.setScene(new Scene(backup, Color.TRANSPARENT));
        stage.setTitle("BlahBLahui Backuptasks");
        stage.initStyle(StageStyle.UTILITY);
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {

                // Decalaration of Variables
                final Stage stage, stage1;
                FXMLLoader pane;
                Parent taskselectwindow = null;

                // Execution Block
                event.consume();
                stage = (Stage) event.getSource();
                stage.close();
                pane = new FXMLLoader(getClass().getResource("FXMLBlahBLahUI.fxml"));
                try {
                    taskselectwindow = (Parent) pane.load();
                } catch (IOException ex) {
                    Logger.getLogger(FXMLBlahBLahUIController.class.getName()).log(Level.SEVERE, null, ex);
                }
                stage1 = new Stage();
                stage1.setScene(new Scene(taskselectwindow));
                stage1.setTitle("BlahBLahUI");
                stage1.show();
            }

        });
        stage1 = (Stage) BlahBLahUIMainWindow.getScene().getWindow();
        stage1.hide();
        stage.show();

    }

    @FXML
    private void handleButtonActionTaskBackupExit(ActionEvent event) throws Exception {

        closebackuptaskandshowmaintask();
    }

    private void closebackuptaskandshowmaintask() throws Exception {
        // Decalaration of Variables
        final Stage stage, stage1;
        FXMLLoader pane;
        Parent taskselectwindow;

        // Execution Block
        stage = (Stage) BackupTaskExitButton.getScene().getWindow();
        stage.close();
        pane = new FXMLLoader(getClass().getResource("FXMLBlahBLahUI.fxml"));
        taskselectwindow = (Parent) pane.load();
        stage1 = new Stage();
        stage1.setScene(new Scene(taskselectwindow));
        stage1.setTitle("BlahBLahUI");
        stage1.show();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

}

这段代码按我的意思工作,但我希望在一个类中尽可能多地使用相同的代码,这样我就不得不只调用这个类而不是一次又一次地重写相同的代码。 对于“退出”按钮onAction事件,我已经创建了一个类。 我需要在stage.setOnCloseRequest事件上运行哪些修改?

我设法找到了解决方案。

首先,我改变了closebackuptaskandshowmaintask方法。

它现在看起来如此:

    private void closebackuptaskandshowmaintask(Event event) throws Exception {
    // Decalaration of Variables
    final Stage stage, stage1;
    FXMLLoader pane;
    Parent taskselectwindow;
    String eventstring;

    // Execution Block
    eventstring = event.getEventType().toString();
    if ("ACTION".equals(eventstring)) {
        stage = (Stage) BackupTaskExitButton.getScene().getWindow();
        stage.close();
    } else if ("WINDOW_CLOSE_REQUEST".equals(eventstring)) {
        event.consume();
        stage = (Stage) event.getSource();
        stage.close();            
    }
    pane = new FXMLLoader(getClass().getResource("FXMLBlahBLahUI.fxml"));
    taskselectwindow = (Parent) pane.load();
    stage1 = new Stage();
    stage1.setScene(new Scene(taskselectwindow));
    stage1.setTitle("BlahBLahUI");
    stage1.show();
}

然后我用以下代码替换了stage.setOnCloseRequest的代码:

    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event1) {
        try {
            closebackuptaskandshowmaintask(event1);
        }catch (Exception ex) {
            Logger.getLogger(FXMLReflectUIController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});

或者作为lambda表达式:

    stage.setOnCloseRequest((WindowEvent event1) -> {
    try {
        closebackuptaskandshowmaintask(event1);
    }catch (Exception ex) {
        Logger.getLogger(FXMLReflectUIController.class.getName()).log(Level.SEVERE, null, ex);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM