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 中的选项卡中
- 如何指定 CORS 的响应标头? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
