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