当从Swing应用程序添加和删除JavaFX面板时,无法再添加它。执行下面的测试并多次按“添加/删除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));
}
}
答案 0 :(得分:3)
Platform#setImplicitExit(false)可用于避免此问题。
Platform.setImplicitExit(false);
Platform.runLater(new Runnable()
{
public void run()
{
createScene();
}
});