Java 添加/删除Swing JFrame上的JFXPanel

Java 添加/删除Swing JFrame上的JFXPanel,java,swing,javafx-2,layout-manager,event-dispatch-thread,Java,Swing,Javafx 2,Layout Manager,Event Dispatch Thread,我想在JFrame容器上添加和删除JFXPanel,但无法这样做。我被困在这里,在单击按钮时没有得到正确的解决方案。我想添加和删除JFXPanel控件 这个代码有什么问题 public class abc extends JFrame { JFXPanel fxpanel; Container cp; public abc() { cp=this.getContentPane(); cp.setLayout(null); JButton b1= new JBu

我想在
JFrame
容器上添加和删除
JFXPanel
,但无法这样做。我被困在这里,在单击按钮时没有得到正确的解决方案。我想添加和删除
JFXPanel
控件

这个代码有什么问题

  public class abc extends JFrame
  {
  JFXPanel fxpanel;
  Container cp;
  public abc()
  {
  cp=this.getContentPane();
  cp.setLayout(null);
  JButton b1= new JButton("Ok");
  JButton b2= new JButton("hide");
  cp.add(b1);
  cp.add(b2);
  b1.setBounds(20,50,50,50);
  b2.setBounds(70,50,50,50);
  b1.addActionListener(this);
  b2.addActionListener(this);
  fxpanel= new JFXPanel();
  cp.add(fxpanel);
  fxpanel.setBounds(600,200,400,500);
 }

  public void actionPerformed(ActionEvent ae)
 { 
 if(ae.getActionCommand().equals("OK"))
  {
    fxpanel= new JFXPanel();
    cp.add(fxpanel);
    fxpanel.setBounds(600,200,400,500);


   }
  if(ae.getActionCommand().equals("hide"))
   { 
    cp.remove(fxpanel);
    }
   }
    public static void main(String args[])
   {

      abc f1= new abc();
      f1.show();
    }
   }
  • 不要不必要地扩展
    JFrame
    调用

  • 不要使用
    null
    /
    Absolute
    LayoutManager

  • 为什么要使用
    show()
    JFrame
    设置为可见?您应该使用
    setVisible(true)

  • 别忘了Swing组件应该通过
    SwingUtilities.invokeXXX
    和JavaFX组件通过
    Platform.runLater()在
    Event Dispatch线程上创建

除上述问题外,您面临的最大问题是在添加/删除组件后不刷新GUI/容器,因此不会显示任何更改:


JFrame
实例上调用
revalidate()
repaint()
,以反映从可见容器中添加/删除组件后的更改。

此代码中的错误是什么
cp.setLayout(null)使用布局。有关“添加和删除内容”的信息,请参阅。