Insert JFrame into a Tab in Swing(将 JFrame 插入到 Swing 中的选项卡中)
问题描述
我有一个已经给定的 JFrame 框架,我想将它显示(插入)到 JTabbedPane 的 tab 中,但是不可能像那样明确地:
I Have an already given JFrame frame, that I want to show it (insert it) into a tab of JTabbedPane, but that was not possible explicitely like that:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainTabs.addTab("Editor", null, frame, null);
错误是:
java.lang.IllegalArgumentException: adding a window to a container
我尝试了一些解决方案,例如将框架插入到 JPanel 中,但也是徒劳的.我有将其转换为 InternalJFrame 的想法,但我对此一无所知.
I tried some solutions like to insert the frame into a JPanel but also in vain. I have the idea to convert it into an InternalJFrame but I don't have any idea about that.
这是插入该 frame 的任何解决方案吗?
Is that any solution to insert that frame?
更新:
我尝试了那个解决方案:
I tried that soluion:
mainTabs.addTab("Editor", null, frame.getContentPane(), null);
但是我丢失了我添加的 JMenuBar.
But i lost the JMenuBar that I added.
推荐答案
你不能将 JFrame(或另一个顶级组件)添加到另一个组件/容器中,但是你可以使用 getContentPane() 框架的方法,获取框架的主面板并将其添加到 JTabbedPane 选项卡.像下一个:
You can't add JFrame(or another top-level component) to another component/container, but you can use getContentPane() method of frame, to get main panel of your frame and add that to JTabbedPane tab. Like next:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
frame.add(new JButton("button"));
tabs.addTab("1", frame.getContentPane());
您也可以将 JFrame 更改为 JPanel 并使用它.
Also you can change JFrame to JPanel and use that.
阅读 JInternalFrame, 顶级容器.
getContentPane() 不返回任何装饰或 JMenuBar,您需要手动添加此组件,例如下一个带有菜单的示例:
getContentPane() doesn't return any decorations or JMenuBar, this components you need to add manually, like in next example with menu:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
JMenuBar bar = new JMenuBar();
bar.add(new JMenu("menu"));
frame.setJMenuBar(bar);
frame.add(new JButton("button"));
JPanel tab1 = new JPanel(new BorderLayout());
tab1.add(frame.getJMenuBar(),BorderLayout.NORTH);
tab1.add(frame.getContentPane());
tabs.addTab("1", tab1);
这篇关于将 JFrame 插入到 Swing 中的选项卡中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 JFrame 插入到 Swing 中的选项卡中
基础教程推荐
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
