如何在JavaFX 2.0中创build和显示通用对话框(错误,警告,确认)?

如何在JavaFX 2.0中创build和显示常见对话框(错误,警告,确认)? 我无法find像“ Dialog ,“ Dialog ”,“ Message ”等任何“标准”类。

最近发布的JDK 1.8.0_40添加了对JavaFX对话框 ,警报等的支持 。例如,要显示确认对话框,可以使用Alert类:

 Alert alert = new Alert(AlertType.CONFIRMATION, "Delete " + selection + " ?", ButtonType.YES, ButtonType.NO, ButtonType.CANCEL); alert.showAndWait(); if (alert.getResult() == ButtonType.YES) { //do stuff } 

以下是此版本中添加的类的列表:

  • javafx.scene.control.Dialog
  • javafx.scene.control.Alert
  • javafx.scene.control.TextInputDialog
  • javafx.scene.control.ChoiceDialog

编辑 :对话框支持被添加到JavaFX,请参阅https://stackoverflow.com/a/28887273/1054140


在2011年没有共同的对话支持。你必须通过创build新的Stage()来自己写:

  Stage dialogStage = new Stage(); dialogStage.initModality(Modality.WINDOW_MODAL); VBox vbox = new VBox(new Text("Hi"), new Button("Ok.")); vbox.setAlignment(Pos.CENTER); vbox.setPadding(new Insets(15)); dialogStage.setScene(new Scene(vbox)); dialogStage.show(); 

更新

作为RT-12643实施的一部分,官方标准对话框将在Java 8×40中发布。 这些应该在2015年3月左右在最终版本中提供,现在在JavaFX开发库中以源代码forms提供。

同时,您可以使用下面的ControlsFX解决scheme…


ControlsFX是事实上标准的第三方库,用于JavaFX中的常见对话支持(错误,警告,确认等)。

还有很多其他的第三方库可以提供常见的对话支持,如其他一些答案所指出的那样,您可以使用Sergey的答案中的示例代码轻松地创build自己的对话框。

不过,我相信ControlsFX很容易提供目前可用的最佳质量的标准JavaFX对话框。 以下是ControlsFX文档中的一些示例。

标准对话框

命令链接

谢尔盖是正确的,但是如果你需要在你自己创build的对话框中获得一个响应,以便在调用它的代码块中进行评估,你应该使用.showAndWait()而不是.show()。 这是我在Swing的OptionPane中提供的几个对话框types的再现:

 public class FXOptionPane { public enum Response { NO, YES, CANCEL }; private static Response buttonSelected = Response.CANCEL; private static ImageView icon = new ImageView(); static class Dialog extends Stage { public Dialog( String title, Stage owner, Scene scene, String iconFile ) { setTitle( title ); initStyle( StageStyle.UTILITY ); initModality( Modality.APPLICATION_MODAL ); initOwner( owner ); setResizable( false ); setScene( scene ); icon.setImage( new Image( getClass().getResourceAsStream( iconFile ) ) ); } public void showDialog() { sizeToScene(); centerOnScreen(); showAndWait(); } } static class Message extends Text { public Message( String msg ) { super( msg ); setWrappingWidth( 250 ); } } public static Response showConfirmDialog( Stage owner, String message, String title ) { VBox vb = new VBox(); Scene scene = new Scene( vb ); final Dialog dial = new Dialog( title, owner, scene, "res/Confirm.png" ); vb.setPadding( new Inset(10,10,10,10) ); vb.setSpacing( 10 ); Button yesButton = new Button( "Yes" ); yesButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle( ActionEvent e ) { dial.close(); buttonSelected = Response.YES; } } ); Button noButton = new Button( "No" ); noButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle( ActionEvent e ) { dial.close(); buttonSelected = Response.NO; } } ); BorderPane bp = new BorderPane(); HBox buttons = new HBox(); buttons.setAlignment( Pos.CENTER ); buttons.setSpacing( 10 ); buttons.getChildren().addAll( yesButton, noButton ); bp.setCenter( buttons ); HBox msg = new HBox(); msg.setSpacing( 5 ); msg.getChildren().addAll( icon, new Message( message ) ); vb.getChildren().addAll( msg, bp ); dial.showDialog(); return buttonSelected; } public static void showMessageDialog( Stage owner, String message, String title ) { showMessageDialog( owner, new Message( message ), title ); } public static void showMessageDialog( Stage owner, Node message, String title ) { VBox vb = new VBox(); Scene scene = new Scene( vb ); final Dialog dial = new Dialog( title, owner, scene, "res/Info.png" ); vb.setPadding( new Inset(10,10,10,10) ); vb.setSpacing( 10 ); Button okButton = new Button( "OK" ); okButton.setAlignment( Pos.CENTER ); okButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle( ActionEvent e ) { dial.close(); } } ); BorderPane bp = new BorderPane(); bp.setCenter( okButton ); HBox msg = new HBox(); msg.setSpacing( 5 ); msg.getChildren().addAll( icon, message ); vb.getChildren().addAll( msg, bp ); dial.showDialog(); } 

}

从这里的答案适应: https : //stackoverflow.com/a/7505528/921224

javafx.scene.control.Alert

有关如何使用JavaFX对话框的深入说明,请参阅:code.makery的JavaFX对话框(官方) 。 它们比Swing对话更强大,更灵活,并且不仅仅是popup消息。

 import javafx.scene.control.Alert import javafx.scene.control.Alert.AlertType; import javafx.application.Platform; public class ClassNameHere { public static void infoBox(String infoMessage, String titleBar) { /* By specifying a null headerMessage String, we cause the dialog to not have a header */ infoBox(infoMessage, titleBar, null); } public static void infoBox(String infoMessage, String titleBar, String headerMessage) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle(titleBar); alert.setHeaderText(headerMessage); alert.setContentText(infoMessage); alert.showAndWait(); } } 

有一点需要记住的是,JavaFX是一个单线程的GUI工具包,这意味着这个方法应该直接从JavaFX应用程序线程中调用。 如果你有另一个线程正在工作,需要一个对话框然后看到这些问题: JavaFX2:我可以暂停一个后台任务/服务? 和Platform.Runlater和Task Javafx 。

要使用此方法调用:

 ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE"); 

要么

 ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE"); 
  • 你可以看看伟大的工具JavaFX对话框是简单的对话框,从Swing的JOptionPane风格

您可以给出由JavaFX UI Controls Project给出的对话框。 我认为这会帮助你

 Dialogs.showErrorDialog(Stage object, errorMessage, "Main line", "Name of Dialog box"); Dialogs.showWarningDialog(Stage object, errorMessage, "Main line", "Name of Dialog box"); 
 public myClass{ private Stage dialogStage; public void msgBox(String title){ dialogStage = new Stage(); GridPane grd_pan = new GridPane(); grd_pan.setAlignment(Pos.CENTER); grd_pan.setHgap(10); grd_pan.setVgap(10);//pading Scene scene =new Scene(grd_pan,300,150); dialogStage.setScene(scene); dialogStage.setTitle("alert"); dialogStage.initModality(Modality.WINDOW_MODAL); Label lab_alert= new Label(title); grd_pan.add(lab_alert, 0, 1); Button btn_ok = new Button("fermer"); btn_ok.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub dialogStage.hide(); } }); grd_pan.add(btn_ok, 0, 2); dialogStage.show(); } } 

这工作自java 8u40:

 Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK) alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE) alert.show()