我想在 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
创建并在Event Dispatch Thread
上进行操作,JavaFX 组件应该通过Platform.runLater()
除了上述问题之外,您遇到的最大问题是添加/删除组件后不刷新 GUI/容器,因此不会显示任何更改。:
在 JFrame
实例上调用 revalidate()
和 repaint()
以反射(reflect)在可见容器中添加/删除组件后的更改。
关于java - 在 Swing JFrame 上添加/删除 JFXPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13738294/