如何使用图像创建模态对话框

时间:2013-06-07 10:31:15

标签: javafx-2 javafx javafx-8

我有一个非常简单的模态对话框:

public class DialogPanels
{

    public void initClosemainAppDialog(final Stage primaryStage)
    {

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent event)
            {
                event.consume(); // Do nothing on close request

                // Dialog Stage init
                final Stage dialog = new Stage();
                // If you want to freeze the background during dialog appearence set Modality.APPLICATION_MODAL
                // or to allow clicking on the mainstage components set Modality.NONE
                // and set dialog.showAndWait();
                dialog.initModality(Modality.APPLICATION_MODAL);
                dialog.initOwner(primaryStage);

                // Frage - Label
                Label label = new Label("Exit from the program");

                // Button "Yes"
                Button okBtn = new Button("Yes");
                okBtn.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent event)
                    {
                        //primaryStage.close();
                        //dialog.close();
                        //Platform.exit();    
                        System.exit(0);
                    }
                });

                // Button "No"
                Button cancelBtn = new Button("No");
                cancelBtn.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent event)
                    {
                        primaryStage.show();
                        dialog.close();
                    }
                });

                // Layout for the Button
                HBox hbox = new HBox();
                hbox.setSpacing(10);
                hbox.setAlignment(Pos.CENTER);
                hbox.getChildren().add(okBtn);
                hbox.getChildren().add(cancelBtn);

                // Layout for the Label and hBox
                VBox vbox = new VBox();
                vbox.setAlignment(Pos.CENTER);
                vbox.setSpacing(10);
                vbox.getChildren().add(label);
                vbox.getChildren().add(hbox);

                // Stage
                Scene scene = new Scene(vbox, 450, 150, Color.WHITESMOKE);
                dialog.setScene(scene);
                dialog.show();
            }
        });

    }
}

我想添加图片并使其看起来像这样:

enter image description here

但是我管理员认为,为了获得适当的结果,我的简短知识太复杂了。你能告诉我如何分割对话框,添加第二个背景并让我的代码看起来像这个例子一样吗?

1 个答案:

答案 0 :(得分:2)

看看ControlsFX project,它们有一些复杂的对话框,它是开源的,所以你可以查看它是如何完成的。例如,您的对话框看起来像ControlsFX的这个确认对话框:

enter image description here

还支持自定义对话框。

€二叔: 启用“show Masthead”选项后,它实际上看起来完全像它:

enter image description here