JavaFx显示弹出窗口无法在客户端计算机上运行

时间:2015-03-02 22:26:34

标签: javafx-8 popupwindow

以下是我在JavaFx桌面应用程序中显示弹出窗口的代码。

public boolean popup(Object parent, ViewModelBase viewModel, AsyncCommand cancelCommand) {

        javafx.scene.layout.Pane root = null;
        try
        {               
            if(!IOC.platformInfo.isPlatformThread())
            {

               return PlatformUtil.runAndWait(()->
                {
                    return popup(parent,viewModel,cancelCommand);
                });
            }


            String name = viewModel.getClass().getSimpleName();
            name = Pattern.compile("(viewModel|Controller)$",Pattern.CASE_INSENSITIVE)
                    .matcher(name).replaceAll("View");

            FXMLLoader loader  = new FXMLLoader(com.technoinn.videoprospector.ui.fx.service.WindowService
            .class.getResource(String.format("/fxml/%s.fxml",name))
            , null, new JavaFXBuilderFactory(), new IocControllerFactory());
            if(viewModel instanceof ControllerBase)
            {
                loader.setController(viewModel);
            }
            root = loader.load();            

            if(!(viewModel instanceof ControllerBase))
            {
                Object controller = loader.getController();
                if(controller instanceof ControllerBase)
                {
                    ((ControllerBase)controller).setViewModel(viewModel);
                }
            }




            jfxtras.scene.control.window.Window window = 
                   new jfxtras.scene.control.window.Window(viewModel.getDisplayName());
            window.getContentPane().getChildren().add(root);
            window.setPrefSize(root.getPrefWidth(), root.getPrefHeight());
            window.setMinSize(root.getPrefWidth(), root.getPrefHeight());
            CloseIcon closeIcon = new CloseIcon(window);

            window.getLeftIcons().add(closeIcon); 

            Scene scene = new Scene(window);
           // Scene scene = new Scene(root);
            scene.getStylesheets().add(FxModule.StyleFile);




            Stage stage = new Stage(StageStyle.UNDECORATED);
            stage.setResizable(true);

            stage.setMinWidth(root.getPrefWidth());
            stage.setMinHeight(root.getPrefHeight());

            viewModel.addPropertyChangeListener(ViewModelBase.closeCommand,
            (x)->
            {
                if(x.getNewValue()!=null && x.getNewValue()==Boolean.TRUE)
                {
                    stage.close();
                }
            });

            closeIcon.setCursor(Cursor.HAND);

            closeIcon.setOnAction((x)->
            {
                if(cancelCommand!=null)
                    cancelCommand.beginExecution();
                else
                    stage.close();
            }); 
            /*

            stage.setOnCloseRequest((WindowEvent event) -> {
                if(cancelCommand!=null)
                    cancelCommand.beginExecution();
                else
                    stage.close();
            });*/
            stage.setScene(scene);
            stage.centerOnScreen();
            stage.initModality(Modality.APPLICATION_MODAL);
            if(!parentWindows.isEmpty())
            {
                stage.initOwner(parentWindows.peek());
                stage.initModality(Modality.WINDOW_MODAL);
            }
            parentWindows.push(stage);

            stage.showAndWait();
            parentWindows.pop();
        }catch(Exception exp)
        {
            Logger.getGlobal().log(Level.SEVERE,"Error in popup",exp);
        }

        return true;
    } 

问题是,弹出窗口在我的机器上显示良好且尺寸合适。(开发机器)。但目标客户端计算机上的大小是不可预测的。有时它非常小,有时它甚至不会显示弹出窗口的内容窗格。客户端计算机具有jRE 1.8_31。知道什么可能是错的。客户端机器大小与我的开发机器相同。 感谢

1 个答案:

答案 0 :(得分:0)

很可能你很快就会调用下一个代码:

     window.setPrefSize(root.getPrefWidth(), root.getPrefHeight());
     window.setMinSize(root.getPrefWidth(), root.getPrefHeight());

     stage.setMinWidth(root.getPrefWidth());
     stage.setMinHeight(root.getPrefHeight());

大多数布局值仅在显示场景后计算。尝试在调用

后移动此类代码
stage.showAndWait();