JavaFx嵌入Swing JDesktopPane不显示任何内容

时间:2014-12-23 14:56:31

标签: java swing javafx-8

我已经阅读并成功了tutorial

我想使用Swing JDesktopPane将内部框架嵌入到JavaFX中。

代码:

public class FxSwingFx extends Application {

   private static void createSwing( SwingNode swingNode ) {
      final JDesktopPane desktopPane = new JDesktopPane();
      swingNode.setContent( desktopPane );
      final JInternalFrame if1 = new JInternalFrame( "Hello, ", true, true, true, true );
      final JInternalFrame if2 = new JInternalFrame( " World!", true, true, true, true );
      if1        .setVisible( true );
      if2        .setVisible( true );
      desktopPane.setVisible( true );
      desktopPane.add( if1 );
      desktopPane.add( if2 );
   }

   @Override
   public void start( Stage primaryStage ) throws Exception {
      final SwingNode swingNode = new SwingNode();
      final BorderPane root = new BorderPane( swingNode );
      root.setBottom( new Button( "FX Button" ));
      SwingUtilities.invokeLater(() -> createSwing( swingNode ));
      primaryStage.setScene( new Scene( root, 400, 300 ));
      primaryStage.show();
   }

   public static void main( String[] args ) {
      launch( args );
   }
}

结果:

enter image description here

问题:为什么不显示内部框架?

1 个答案:

答案 0 :(得分:2)

查看内部框架的条件是:

  • 必须使用setSize()设置尺寸,setPreferredSize()并非
  • setVisible( true )必须被称为

<强>代码:

public class FxSwingFx extends Application {

   JInternalFrame createInternalFrame( String title, int width, int height ) {
      final JInternalFrame frame = new JInternalFrame( title, true, true, true, true );
      frame.setVisible( true );
      frame.setSize( width, height );
      return frame;
   }

   void createSwing( SwingNode swingNode ) {
      final JDesktopPane desktopPane = new JDesktopPane();
      desktopPane.add( createInternalFrame( "One", 400, 300 ));
      desktopPane.add( createInternalFrame( "Two", 400, 300 ));
      swingNode.setContent( desktopPane );
   }

   @Override
   public void start( Stage primaryStage ) throws Exception {
      final SwingNode swingNode = new SwingNode();
      SwingUtilities.invokeLater(() -> createSwing( swingNode ));
      final BorderPane root = new BorderPane( swingNode );
      final Button jfxBtn = new Button( "FX Button" );
      root.setBottom( jfxBtn );
      primaryStage.setScene( new Scene( root, 600, 500 ));
      primaryStage.show();
   }

   public static void main( String[] args ) {
      launch( args );
   }
}

<强>结果:

JavaFX8 Application with JDesktopPane