0

我在 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抽象类,它只是为我提供了一个主要的 JFrame(这个抽象类还负责组件属性注入、退出处理以及以适合的方式保存/恢复会话状态简单的单帧应用程序)。

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

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

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

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

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

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

问题是,当我执行此类时,我收到以下错误消息:

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 内的对象……问题出在哪里?我该如何解决?

肿瘤坏死因子

安德烈亚

4

3 回答 3

3

您正在尝试在此容器中添加容器。问题就在这里

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

这个JFrame mainFrame = this.getMainFrame();已经有了ContentPane,你又加了一个。每个 JFrame 有 3 个面板 -> glassPane、contentPane 和 layerdPane。

尝试添加元素mainContainer(已包含在 中mainFrame)。

于 2013-11-08T11:44:32.963 回答
2

mainContainer 已经从 JFrame 派生。

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

Container mainContainer = mainFrame.getContentPane();
 ..
mainFrame.add(mainContainer);
于 2013-11-08T11:44:55.730 回答
1
mainContainer = mainFrame.getContentPane();
//...
mainFrame.add(mainContainer);//wrong

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

您应该像已经做的那样简单地将孩子添加到mainContainer 。

于 2013-11-08T11:47:14.070 回答