目前我正在尝试制作一个多窗口程序。
从其他论坛上看,似乎在Java JFrame中实现这个功能需要通过添加new JComponent(new window/layout/idk)
来更新其内容面板,将当前窗口的可见性设置为false,将新窗口的可见性设置为true,并验证和重新绘制内容面板:
contentPane.add(newWindowPanel);
currentWindowPanel.setVisible(false);
newWindowPanel.setVisible(true);
contentPane.validate();
contentPane.repaint();
现在我正在尝试并已经创建了一个扩展JPanel
的类,这个类将成为我将要创建的许多其他JPanel类的顶级类。
在这个类中,我有这个方法:
public void updateContentPane(Container contentPane, JPanel currentPanel, JPanel nextPanel){
contentPane.add(nextPanel);
currentPanel.setVisible(false);
nextPanel.setVisible(true);
contentPane.validate();
contentPane.repaint();
}
当我在子类中调用此方法时,它不起作用。
updateContentPane(WindowMain.contentPane, this, mainMenuClass);
每个子类都继承了JPanel的特性。
"WindowMain"是一个扩展JFrame的类,"contentPane"是一个静态容器变量,它保存了框架的contentPane。
"this"代表当前类(继承JPanel),但实际上"this"不起作用,
new Object()
起作用。"mainMenuClass"也继承了JPanel,并已在此类中被实例化。
我的目标只是通过调用该方法简单地从一个场景跳转到另一个场景。但是它会通过代码(调试),但什么也不会发生。但是,如果我将方法中的代码放在按钮监听器内部,它就可以正常工作。
(对于所有这些写作,很抱歉可能会带来一些困惑,但我仍然需要解决这个问题,并且我将为当contentPane已经包含一个类时设置条件,因此无需提及它)