2015-03-02 54 views
0

以下是我的代码,用于在我的JavaFx桌面应用程序中显示弹出窗口。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。任何想法可能是错的。客户端机器大小与我的开发机器相同。 感谢

回答

0

最有可能你是太早调用下面的代码:

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

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

大多数布局值被计算显示场景之后。尝试移动此类代码后,致电

stage.showAndWait();