简体   繁体   English

添加/删除时,将JFXPanel嵌入到Swing应用程序中失败

[英]Embedding a JFXPanel in a Swing application fails on add/remove

When a JavaFX panel is added and removed from a Swing application it can't be added any more. 将JavaFX面板添加到Swing应用程序中或从其中删除后,就不能再添加了。 Execute the test below and press the "add/remove FXPanel" button multiple times - for some reason it works only once. 执行以下测试,然后多次按“添加/删除FXPanel”按钮-由于某种原因,它只能工作一次。

public class EmbeddedFXPanelTest extends JFrame
{
  private JFXPanel fxPanel;

  public static void main(String[] args)  throws Exception
  {
    EventQueue.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        new EmbeddedFXPanelTest();
      }
    });
  }

  public EmbeddedFXPanelTest()
  {
    fxPanel = new JFXPanel();

    add(new JButton(new AbstractAction("Add FXPanel")
    {      
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        JButton b = (JButton)evt.getSource();        
        if (fxPanel.getParent() == null)
        {  
          add(fxPanel);
          b.setText("Remove FXPanel");
        }  
        else
        {
          remove(fxPanel);
          b.setText("Add FXPanel");          
        }
        revalidate();
        repaint();
      }
    }), BorderLayout.NORTH);

    Platform.runLater(new Runnable()
    {
      public void run()
      {
        createScene();
      }
    });

    setTitle(getClass().getSimpleName());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);
    setVisible(true);    
  }

  private void createScene() 
  {
    FlowPane p = new FlowPane(10, 10);
    p.getChildren().add(new Button("FX Button"));
    p.getChildren().add(new CheckBox("FX CheckBox"));
    p.setStyle("-fx-background-color:yellow");
    fxPanel.setScene(new Scene(p));
  }      
}

Platform#setImplicitExit(false) can be used to avoid the the issue. Platform#setImplicitExit(false)可用于避免该问题。

Platform.setImplicitExit(false);
Platform.runLater(new Runnable()
{
  public void run()
  {
    createScene();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM