gpt4 book ai didi

java - 选项卡式 Pane 错误 : adding container's parent to itself

转载 作者:行者123 更新时间:2023-12-02 06:04:11 28 4
gpt4 key购买 nike

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

这是直接来 self 的教授的代码,所以我不知道为什么它会是错误的。我已将 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();

最佳答案

这是不正确的:

frame.add(frame, BorderLayout.CENTER);

您似乎试图向自身添加一个组件,这是不允许的。

解决方案:不要这样做。

<小时/>

其他问题:

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

关于java - 选项卡式 Pane 错误 : adding container's parent to itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426025/

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