77

如何在 JavaFX 2.0 中创建和显示常见对话框(错误、警告、确认)?我找不到任何“标准”类Dialog,例如DialogBox,Message或其他东西。

4

10 回答 10

109

最近发布的 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
}

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

于 2015-03-05T20:38:54.873 回答
51

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


2011 年没有通用对话框支持。您必须自己编写new 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();
于 2011-11-29T12:09:13.483 回答
40

更新

作为RT-12643实现的一部分,官方标准对话框将在 8u40 版中加入 JavaFX 。这些应该在 2015 年 3 月左右以最终版本形式提供,现在在 JavaFX 开发存储库中以源代码形式提供。

同时,您可以使用下面的 ControlsFX 解决方案...


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

如其他答案中所指出的,还有许多其他 3rd 方库可提供通用对话框支持,您可以使用 Sergey 答案中的示例代码轻松创建自己的对话框。

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

标准对话框

命令链接

于 2013-07-17T07:34:26.017 回答
13

Sergey 是正确的,但是如果你需要从你的 home-spun 对话框中获得响应以在调用它的同一代码块中进行评估,你应该使用 .showAndWait(),而不是 .show()。这是我对 Swing 的 OptionPane 中提供的几种对话框类型的演绎:

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();
}

}

于 2012-10-03T23:04:03.257 回答
8

改编自这里的答案:https ://stackoverflow.com/a/7505528/921224

javafx.scene.control.Alert

有关如何使用 JavaFX 对话框的深入描述,请参阅: code.makery 的JavaFX 对话框(官方)。它们比 Swing 对话框更强大、更灵活,而且功能远不止弹出消息。

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 应用程序线程调用此方法。如果您有另一个线程在工作,需要一个对话框,请查看这些 SO Q&As:JavaFX2:我可以暂停后台任务/服务吗?Platform.Runlater 和 Task Javafx

要使用此方法调用:

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

或者

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");
于 2015-06-10T14:07:30.977 回答
6

这从 java 8u40 开始有效:

Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.show();
于 2016-04-29T12:34:37.130 回答
4

更新: JavaFX 8u40 包括简单的对话框和警报!查看这篇博客文章,它解释了如何使用官方 JavaFX 对话框!


于 2013-05-16T08:45:11.747 回答
3

您可以给出由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");
于 2013-07-17T06:53:23.813 回答
2
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();

}



}
于 2014-05-30T21:55:55.533 回答
0

要使Clairton Luz的示例 正常工作,您需要在代码片段中运行FXApplicationThreadand insert into方法:Platform.runLater

  Platform.runLater(() -> {
                        Alert alert = new Alert(Alert.AlertType.ERROR);
                        alert.setTitle("Error Dialog");
                        alert.setHeaderText("No information.");

                        alert.showAndWait();
                    }
            );

否则,你会得到: java.lang.IllegalStateException: Not on FX application thread

于 2020-02-22T02:39:20.013 回答