当我尝试使用Swing将Container对象添加到主JFrame对象时,为什么会出现此错误?

时间:2013-11-08 11:38:34

标签: java swing miglayout jsr296

我在Java Swing 应用程序中绝对是新的,我有以下问题。

我有这个简单的 LoginFrame2 类:

package com.test.login;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import net.miginfocom.swt.MigLayout;

import org.jdesktop.application.SingleFrameApplication;

public class LoginFrame2 extends SingleFrameApplication {

    public static void main(String[] args) {
        System.out.println("DENTRO: LoginFrame() ---> main()");
        launch(LoginFrame2.class, args);
    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("Inside startup()");

        JFrame mainFrame = this.getMainFrame();         // main JFrame that represents the Windows
        mainFrame.setTitle("XCloud Login");

        Container mainContainer = mainFrame.getContentPane();       // main Container into the main JFrame

        // JPanel creation and settings of the MigLayout on it:
        JPanel externalPanel = new JPanel();
        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"));

        mainContainer.add(externalPanel);
        mainFrame.add(mainContainer);


    }

}

正如您所看到的,我正在使用 JDesktop 框架的 SingleFrameApplication abstrac类,它只向我提供了一个主JFrame(此抽象类还负责组件属性)注入,退出处理以及保存/恢复会话状态的方式适合简单的单帧应用程序。)

执行类时,执行 startUp()方法。

如您所见,我已执行以下操作:

  1. 我使用 getMainFrame()方法获取主 Jframe ,并将其引用放入 mainFrame 变量

  2. 我将主容器(即进入mainFrame对象)调用 getContentPane()方法

  3. 然后我创建了一个名为 externalPanel JPanel 对象,其中我放置了一个初始化的 JLabel ,它将显示一个字符串。

  4. 最后,我将 JPanel 对象添加到容器对象,然后尝试将容器添加到主 > JFrame 对象。

  5. 问题是当我执行这个类时,我得到以下错误信息:

    DENTRO: LoginFrame() ---> main()
    Inside startup()
    8-nov-2013 12.35.23 org.jdesktop.application.Application$1 run
    GRAVE: Application class com.test.login.LoginFrame2 failed to launch
    java.lang.IllegalArgumentException: adding container's parent to itself
        at java.awt.Container.checkAddToSelf(Container.java:418)
        at java.awt.Container.addImpl(Container.java:1036)
        at java.awt.Container.add(Container.java:957)
        at javax.swing.JFrame.addImpl(JFrame.java:540)
        at java.awt.Container.add(Container.java:363)
        at com.test.login.LoginFrame2.startup(LoginFrame2.java:38)
        at org.jdesktop.application.Application$1.run(Application.java:187)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
        at java.awt.EventQueue.access$400(EventQueue.java:81)
        at java.awt.EventQueue$2.run(EventQueue.java:633)
        at java.awt.EventQueue$2.run(EventQueue.java:631)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class com.test.login.LoginFrame2 failed to launch
        at org.jdesktop.application.Application$1.run(Application.java:192)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
        at java.awt.EventQueue.access$400(EventQueue.java:81)
        at java.awt.EventQueue$2.run(EventQueue.java:633)
        at java.awt.EventQueue$2.run(EventQueue.java:631)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Caused by: java.lang.IllegalArgumentException: adding container's parent to itself
        at java.awt.Container.checkAddToSelf(Container.java:418)
        at java.awt.Container.addImpl(Container.java:1036)
        at java.awt.Container.add(Container.java:957)
        at javax.swing.JFrame.addImpl(JFrame.java:540)
        at java.awt.Container.add(Container.java:363)
        at com.test.login.LoginFrame2.startup(LoginFrame2.java:38)
        at org.jdesktop.application.Application$1.run(Application.java:187)
        ... 14 more
    

    我认为问题是在其Jframe对象中添加Container对象。但为什么?根据我的理解,Container对象是包装在JFrame中的对象......问题出在哪里?我怎么解决呢?

    TNX

    安德烈

3 个答案:

答案 0 :(得分:3)

你正在试图在这个容器中添加容器。问题在这里

Container mainContainer = mainFrame.getContentPane();
...
mainFrame.add(mainContainer);

JFrame mainFrame = this.getMainFrame();已有ContentPane,您还需要添加一个mainContainer。每个JFrame都有3个面板 - > glassPane,contentPane和layerdPane。

尝试在mainFrame中添加元素(已包含在{{1}}中)。

答案 1 :(得分:2)

mainContainer已经从JFrame派生。

JFrame mainFrame = this.getMainFrame();         
    // main JFrame that represents the Windows
mainFrame.setTitle("XCloud Login");

Container mainContainer = mainFrame.getContentPane();
 ..
mainFrame.add(mainContainer);

答案 2 :(得分:1)

mainContainer = mainFrame.getContentPane();
//...
mainFrame.add(mainContainer);//wrong

mainContainer 添加回 mainFrame 没有任何意义,并且将子项添加到JFrame通常是完全错误的。

您应该像以前一样将子项添加到 mainContainer