我在 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()方法。
如您所见,我执行了以下操作:
我使用getMainFrame ()方法获取主 Jframe,并将其引用放入mainFrame变量
我将主容器(即进入 mainFrame 对象)调用getContentPane()方法
然后我创建了一个名为externalPanel的JPanel对象,我在其中放置了一个初始化的JLabel,它将显示一个字符串。
最后,我将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 内的对象……问题出在哪里?我该如何解决?
肿瘤坏死因子
安德烈亚