Jin*_*ing 2 java user-interface alert javafx
我正在使用带有场景构建器的 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) {
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
public class FileAccess {
public static boolean importFile(File file) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它没有打开新窗口,而是在新选项卡中打开警报(我没有与选项卡设置相关的任何内容),请参见下面的屏幕截图:
它甚至没有正确大小...
我正在使用 macOS Mojave。
谢谢!
解决了:
这是操作系统设置问题。在Dock of System Preferences 中,我在打开文档时将Prefer 选项卡设置为Always,将其设置为Manually 后,它可以正常工作。
感谢大家的帮助。
| 归档时间: |
|
| 查看次数: |
1057 次 |
| 最近记录: |