[英]JavaFX Alert doesn't open in new window but in a new tab
我正在將 JavaFX 8 與場景構建器一起使用,並且我正在嘗試創建一個警報彈出窗口。 請參閱下面的我的代碼:
更新:包括我擁有的所有代碼(不包括一些個人標識符)。
public class MainViewController {
@FXML
private void handleQuitButtonAction(ActionEvent event) {
System.exit(0);
}
@FXML
private void handleImportButtonAction(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select the file to import");
File file = fileChooser.showOpenDialog(new Stage());
Alert alert;
if (FileAccess.importFile(file)) {
alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Success!");
alert.setHeaderText("Congrats!");
alert.setContentText("Successfully imported data from the chosen file!");
} else {
alert = new Alert(AlertType.ERROR);
alert.setTitle("Error!");
alert.setHeaderText("Sorry...");
alert.setContentText("Something wrong with the import, please try again.");
}
alert.showAndWait();
// Refresh Scroll List
}
@FXML
private void handleExportButtonAction(ActionEvent event) {
}
}
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane mainView;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Main System");
showMainView();
}
private void showMainView() {
try {
// Load Main View from FXML file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/MainView.fxml"));
mainView = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(mainView);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
public class FileAccess {
public static boolean importFile(File file) {
return false;
}
}
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.geometry.Insets?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.ButtonBar?> <?import javafx.scene.control.ScrollPane?> <?import javafx.scene.effect.DropShadow?> <?import javafx.scene.layout.BorderPane?> <BorderPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.MainViewController"> <center> <ScrollPane prefWidth="800.0" BorderPane.alignment="CENTER" /> </center> <top> <ButtonBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER"> <buttons> <Button focusTraversable="false" mnemonicParsing="false" onAction="#handleImportButtonAction" text="Import" ButtonBar.buttonData="LEFT" /> <Button focusTraversable="false" mnemonicParsing="false" onAction="#handleExportButtonAction" text="Export" ButtonBar.buttonData="LEFT" /> <Button focusTraversable="false" mnemonicParsing="false" onAction="#handleQuitButtonAction" text="Quit Program" ButtonBar.buttonData="RIGHT" /> </buttons> <effect> <DropShadow /> </effect> <BorderPane.margin> <Insets left="10.0" right="10.0" /> </BorderPane.margin> </ButtonBar> </top> </BorderPane>
但是,它沒有打開新窗口,而是在新選項卡中打開警報(我沒有任何與選項卡設置相關的內容),請參見下面的屏幕截圖:
它甚至沒有正確大小...
我正在使用 macOS Mojave。
謝謝!
解決了:
這是操作系統設置問題。 在系統偏好設置的 Dock中,我將打開文檔時的首選選項卡設置為始終 ,將其設置為手動后 ,它可以正常工作。
謝謝大家的幫助。
我認為您不應該創建一個新的舞台對象。 這可能是問題所在。
Stage stage = (Stage)((Node)(event).getSource()).getScene().getWindow();
上面的代碼將為您提供當前階段。 這可能會解決您的問題。
祝好運
@Jing 解決方案工作正常,但如果您不想強制您的用戶修改他們的操作系統設置,您需要為 JavaFX 警報或任何 Dialog 組件分配一個父級。 警報擴展對話框。
private void setDefaultOwnerIfNotExisting(Dialog<?> dialog, Window parent) {
if (dialog.getOwner() == null) {
dialog.initOwner(parent);
}
}
這已經在 MacOS Monterey 上進行了測試,現在我的警報不是作為新標簽出現的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.