在另一个菜单中打开 jFrame 的元素和结构
Open elements and struct of jFrame in another like a menu
我的 java 项目中有 5 个 jFrames。我想做一个主菜单。
我的意思是,我希望程序以 jFrame 开始,当我单击按钮而不是打开 jFrame 时,标签、按钮和表格等所有元素都显示在我的主要 jFrame 中。
如果我点击其他按钮,主框架将清理并为其他 jframe 充电。
有可能吗?我正在使用 java jdk 8 和 netbeans 进行编程。
谢谢
编辑:
我认为标记重复的人不明白我的问题。我不想打开或关闭框架,或其他框架,我想在同一个框架中加载几个的结构和组件。在你开始抱怨重复之前请阅读我的问题
i have 5 jFrames in my java project.
这是个问题。
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
是的,这可以通过从要在当前显示的 JFrame 中显示其内容的 JFrame 获取 contentPane(通常是 JPanel),并将其设置为显示的 JFrame 的 contentPane 来解决,例如:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
但是这个额外的体操很笨重,容易出错并且没有必要。让 class 扩展 JFrame 是在逼迫自己创建和显示 JFrame,而这通常需要更大的灵活性。事实上,我敢说我创建的和我见过的大多数 Swing GUI 代码 没有 扩展 JFrame,事实上你很少会想做这个。更常见的是,您的 GUI classes 将适用于创建 JPanel,然后可以将其放入 JFrames 或 JDialogs,或 JTabbedPanes,或通过 CardLayouts 在任何需要的地方进行交换。这将大大增加您的 GUI 编码的灵活性。
对于这种情况,我建议您这样做,您的 GUI classes 创建 JPanel,然后将要交换的 JPanel 添加到使用 CardLayout 的 JPanel。然后,每当您想显示不同的 "card" 时,在 CardLayout 对象上调用 show(...)
,传入使用它的 JPanel,以及添加 [=35= 时使用的 String 键] JPanel 到CardLayout-using JPanel。这在 CardLayout Tutorials.
中得到了很好的解释
其他有用的链接:
- 有关避免手动交换的原因,请参阅:What's so special about CardLayout vs manual adding/removal of JPanels?
- 要使用 CardLayout 帮助控制具有多个 class 的 "multi-page" 应用程序,请参阅:How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes
我的 java 项目中有 5 个 jFrames。我想做一个主菜单。
我的意思是,我希望程序以 jFrame 开始,当我单击按钮而不是打开 jFrame 时,标签、按钮和表格等所有元素都显示在我的主要 jFrame 中。 如果我点击其他按钮,主框架将清理并为其他 jframe 充电。
有可能吗?我正在使用 java jdk 8 和 netbeans 进行编程。 谢谢
编辑: 我认为标记重复的人不明白我的问题。我不想打开或关闭框架,或其他框架,我想在同一个框架中加载几个的结构和组件。在你开始抱怨重复之前请阅读我的问题
i have 5 jFrames in my java project.
这是个问题。
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
是的,这可以通过从要在当前显示的 JFrame 中显示其内容的 JFrame 获取 contentPane(通常是 JPanel),并将其设置为显示的 JFrame 的 contentPane 来解决,例如:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
但是这个额外的体操很笨重,容易出错并且没有必要。让 class 扩展 JFrame 是在逼迫自己创建和显示 JFrame,而这通常需要更大的灵活性。事实上,我敢说我创建的和我见过的大多数 Swing GUI 代码 没有 扩展 JFrame,事实上你很少会想做这个。更常见的是,您的 GUI classes 将适用于创建 JPanel,然后可以将其放入 JFrames 或 JDialogs,或 JTabbedPanes,或通过 CardLayouts 在任何需要的地方进行交换。这将大大增加您的 GUI 编码的灵活性。
对于这种情况,我建议您这样做,您的 GUI classes 创建 JPanel,然后将要交换的 JPanel 添加到使用 CardLayout 的 JPanel。然后,每当您想显示不同的 "card" 时,在 CardLayout 对象上调用 show(...)
,传入使用它的 JPanel,以及添加 [=35= 时使用的 String 键] JPanel 到CardLayout-using JPanel。这在 CardLayout Tutorials.
其他有用的链接:
- 有关避免手动交换的原因,请参阅:What's so special about CardLayout vs manual adding/removal of JPanels?
- 要使用 CardLayout 帮助控制具有多个 class 的 "multi-page" 应用程序,请参阅:How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes