gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:07:59 25 4
gpt4 key购买 nike

我对 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 (这个抽象类还负责组件属性注入(inject)、退出处理以及以适合简单单帧应用程序的方式保存/恢复 session 状态)。

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

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

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

  2. 我使用主Container(即mainFrame对象)调用getContentPane()方法

  3. 然后,我创建一个名为 externalPanelJPanel 对象,在其中放置一个初始化的 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 内的对象......问题出在哪里?我该如何解决?

Tnx

安德里亚

最佳答案

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

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

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

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

关于java - 当我尝试使用 Swing 将 Container 对象添加到主 JFrame 对象时,为什么会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19858221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com