gpt4 book ai didi

java - 带有动态 JPanel 的 JFrame

转载 作者:行者123 更新时间:2023-12-01 13:18:32 24 4
gpt4 key购买 nike

我正在编写一个程序,我需要 JFrame 根据主 JFrame 中的一个选择来更改其组件,我尝试这样做:

public void agregarPanelSegunPrueba(FrmBoleto frm)
{
//this panel is a JPanel make with the graphical editor in netbeans
PnlPruebaDCExesoVelocidad pnl = new PnlPruebaDCExesoVelocidad();
pnl.repaint();
pnl.revalidate();
frm.getContentPane().remove(frm.getPnlPruebasDistanciaTiempo());
frm.getContentPane().add(pnl);
frm.pack();
frm.setVisible(true);

}

有人知道这里出了什么问题吗?提前致谢。

最佳答案

您需要revalidate();repaint();而不是您添加的JPanel,而只是您添加面板的容器。

您需要调用:

frm.getContentPane().revalidate();
frm.getContentPane().repaint();

而不是:

pnl.repaint();
pnl.revalidate();

该行之后:frm.getContentPane().add(pnl);

关于java - 带有动态 JPanel 的 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com