JavaFX如何通过标题栏上的“x”按钮以及窗口中的按钮关闭当前窗口并打开另一个窗口?

4
在我的应用程序中,当用户按下按钮时,当前窗口将隐藏/关闭并打开一个新窗口。在这个新窗口中有一个“退出”按钮。当用户点击它时,它也会关闭/隐藏当前窗口并“重新打开”父窗口。标题栏中的“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
    }

}

这段代码已经符合我的需求,但我希望有尽可能多的相同代码出现在一个类中,这样我只需要调用这个类而不是一遍又一遍地重写相同的代码。对于"Exit"按钮的onAction事件,我已经创建了一个类。需要做哪些修改才能使它在stage.setOnCloseRequest事件中运行呢?

1个回答

1

我找到了一个解决方案。

首先,我修改了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);
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接