在我的应用程序中,当用户按下按钮时,当前窗口将隐藏/关闭并打开一个新窗口。在这个新窗口中是一个"退出"按钮。当用户点击它时,它也会关闭/隐藏当前窗口并重新打开"父窗口。我对" 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
}
}
这段代码可以按照我的意愿运行,但是我希望在一个类中尽可能多地使用相同的代码,这样我只需要调用这个类,而不是一次又一次地重写相同的代码。对于&#34;退出&#34;按钮onAction事件我已经创建了一个类。我还可以在stage.setOnCloseRequest事件上运行哪些修改?
答案 0 :(得分: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);
}
});