首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在JavaFX 2.0中创建和显示通用对话框(错误、警告、确认)?

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

Stack Overflow用户
提问于 2011-11-29 19:28:50
回答 6查看 178.6K关注 0票数 77

如何在JavaFX 2.0中创建和显示常见对话框(错误、警告、确认)?我找不到任何像DialogDialogBoxMessage之类的“标准”类。

EN

回答 6

Stack Overflow用户

发布于 2012-10-04 07:04:03

Sergey是正确的,但是如果您需要在调用它的相同代码块中从您的本地对话框中获取响应以进行评估,则应该使用.showAndWait(),而不是.show()。下面是我在Swing的OptionPane中提供的几种对话框类型:

代码语言:javascript
复制
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();
}

}

票数 13
EN

Stack Overflow用户

发布于 2015-06-10 22:07:31

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

javafx.scene.control.Alert

有关如何使用JavaFX对话框的深入描述,请参阅:JavaFX Dialogs (official) by code.makery。它们比Swing对话框更强大、更灵活,并且能够远远超出弹出消息的范围。

代码语言:javascript
复制
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是一个单线程图形用户界面工具包,这意味着应该直接从JavaFX应用程序线程调用此方法。如果你有另一个线程在工作,它需要一个对话框,那么可以看看下面这些:JavaFX2: Can I pause a background Task / Service?Platform.Runlater and Task Javafx

要使用此方法调用:

代码语言:javascript
复制
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");

代码语言:javascript
复制
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");
票数 8
EN

Stack Overflow用户

发布于 2016-04-29 20:34:37

从java 8u40开始:

代码语言:javascript
复制
Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.show();
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8309981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档