java.lang.IllegalArgumentException:向容器添加窗口 [英] java.lang.IllegalArgumentException: adding a window to a container

查看:988
本文介绍了java.lang.IllegalArgumentException:向容器添加窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用frame.add(this)时,我得到'java.lang.IllegalArgumentException:向容器添加一个窗口'。我做错了什么,如何修复错误。在此先感谢。

I get 'java.lang.IllegalArgumentException: adding a window to a container' when I call frame.add(this). What am I doing wrong, and how do I fix the error. Thanks in advance.

public class mainclass extends JFrame{

private static final long serialVersionUID = 1L;

private int width = 400;
private int height = 400;

public static JFrame frame;

public static void main(String args[]) {
frame = new JFrame();

    mainclass mainclass = new mainclass();
    mainclass.createFrame();
}

public void createFrame() {
    frame.setSize(new Dimension(width, height));
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(this); // this is where the error occurs
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setIconImage(new ImageIcon("res/icon.png").getImage());
}
}

Stacktrace:

Stacktrace:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at test.mainclass.createFrame(mainclass.java:27)
at test.mainclass.main(mainclass.java:21)


推荐答案

简单的答案是你不能这样做。您的问题将受益于描述为什么您想要这样做 - 您想要实现的最终结果是什么?

The simple answer is that you can't do this. Your question would benefit from describing why you would want to do this - what end result are you trying to achieve?

如果您正在尝试要添加另一个容器,您应该使用 JPanel 秒。如果您正在尝试创建类似MDI的应用程序,那么您应该查看 的JInternalFrame 秒。如果你想要一个弹出框架,你需要 JDialog s。

If you're trying to add another container then you should use JPanels. If you are trying to create an MDI-like app, then you should look at JInternalFrames. If you want a popup frame, you need JDialogs.

如需了解更多信息,请设计 JFrame s成为顶级容器 - 它们包含 JRootPane 作为唯一的子容器。当您想要向框架添加内容时,您实际上会添加到框架的根窗格,称为内容窗格。正确的方法是调用 frame.getContentPane()。add()

For a little more information, JFrames are designed to be top-level containers - they contain a JRootPane as its only child. When you want to add something to a frame you are in effect adding to the frame's root pane, referred to as the content pane. The correct way is to call frame.getContentPane().add().

这是一个恒定的来源沮丧是因为许多开发人员本能地想要调用 frame.add()这几乎是所有其他Swing组件的工作方式。因此,方便 frame.add()已被覆盖以调用 frame.getContentPane()。add()

This was a constant source of frustration because a lot of developers instinctively wanted to call frame.add() which is how practically all the other Swing components work. Therefore as a convenience frame.add() has been overridden to call frame.getContentPane().add().

因此,如果您考虑现在示例中发生的情况,您将尝试将JFrame添加到框架的根内容窗格中。可以理解的是,根窗格不能将其他顶级容器作为子元素,例如JFrame,因为它们拥有自己的根窗格。

So if you think about what's happen in your example now, you are trying to add a JFrame to a frame's root content pane. Understandably root panes cannot have other top-level containers as child elements, eg JFrames as they possess their own root pane.

这篇关于java.lang.IllegalArgumentException:向容器添加窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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