选项卡式窗格错误:将容器的父级添加到自身

时间:2014-03-15 15:50:16

标签: java swing

我遇到了一个无法诊断的问题:(

这是我教授的代码,所以我不知道为什么会出错。我已经将JPanel扩展到我调用的所有类中。

以下是错误代码:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Container.java:472)
at java.awt.Container.addImpl(Container.java:1083)
at java.awt.Container.add(Container.java:998)
at javax.swing.JFrame.addImpl(JFrame.java:562)
at java.awt.Container.add(Container.java:966)
at UserInterface.AirlineReservation$BookMenuAction.actionPerformed(AirlineReservation.java:165)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这是我的代码:

mainPane = new JPanel();

        // Create the main panel for the JTabbedPane to be added to
            mainPane.setLayout(new BorderLayout());
            // Add the main panel to the JFrame
            frame.getContentPane().add(mainPane);
            // Create the JTabbedPane
            JTabbedPane tabs = new JTabbedPane();

            // Create the tabs
            SearchFlight search = new SearchFlight();
            tabs.add("Search Flight", search);

            SelectFlight select = new SelectFlight();
            tabs.add("Select Flight", select);

            FlightPrice price = new FlightPrice();
            tabs.add("Flight Price", price);

            BookTicket book = new BookTicket();
            tabs.add("Book Ticket", book);

            Payment pay = new Payment();
            tabs.add("Payment", pay);

            BookingSummary summary = new BookingSummary();
            tabs.add("Booking Summary", summary);

            // Add the tabbed panel to the main panel
            frame.add(tabs, BorderLayout.CENTER);
            // Add the main panel to the frame
            frame.add(frame, BorderLayout.CENTER);
            frame.setVisible(true);
            frame.validate();

1 个答案:

答案 0 :(得分:2)

这是不正确的:

frame.add(frame, BorderLayout.CENTER);

您似乎正在尝试向自己添加一个不允许的组件。

解决方案:不要这样做。


其他问题:

您正在使用容器(JFrame' contentPane)将多个组件添加到BorderLayout,所有这些都是默认方式,即BorderLayout.CENTER方式。这意味着添加的任何组件将完全覆盖并替换先前添加的任何组件。也不要这样做。