将JFrame插入Swing中的选项卡 [英] Insert JFrame into a Tab in Swing

查看:68
本文介绍了将JFrame插入Swing中的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经给定的JFrame frame,我想将其显示(插入)到JTabbedPanetab中,但这不可能明确地做到这一点:

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中的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆