Java 我在JFrame.setContentPane上有一个索引越界

Java 我在JFrame.setContentPane上有一个索引越界,java,swing,jframe,jpanel,indexoutofboundsexception,Java,Swing,Jframe,Jpanel,Indexoutofboundsexception,这行: this.setContentPane(panelLoading); 告诉我这个错误: Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.remove(Unknown Source) at java.awt

这行:

this.setContentPane(panelLoading);
告诉我这个错误:

Exception in thread "AWT-EventQueue-0"
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.remove(Unknown Source)
at java.awt.Container.remove(Unknown Source)
at javax.swing.JLayeredPane.remove(Unknown Source)
at java.awt.Container.remove(Unknown Source)
at javax.swing.JRootPane.setContentPane(Unknown Source)
at javax.swing.JFrame.setContentPane(Unknown Source)
at IHM.FenPrincipale.chargement(FenPrincipale.java:291)
at IHM.FenPrincipale.setServeur(FenPrincipale.java:175)
at IHM.PanelMenuPrincipal.actionPerformed(PanelMenuPrincipal.java:174)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
仅当从JComboBox(ActionListener或ItemListener)上的事件调用函数时,使用my函数设置数据库后,此错误才会出现在下一个setContentPane上。

boutonJouer是一个JButton,serveur是一个JComboBox。 请帮帮我


PS:对不起,我的英语不好

谢谢你的卡片布局,我终于找到了解决方案: 在调用函数之前使用CardLayout并移除JcomboBox,然后添加她

remove(serveur); //Remove the JComboBox
fen.setServeur(serveur.getSelectedItem().toString()); //Call the function
add(serveur); //Then add the JCombpBox
它起作用了。
感谢您的帮助。

请考虑提供一份说明您的问题的报告。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应。您确实应该避免使用
setContentPane
切换视图,或者只需使用新数据更新现有视图,或者如果需要,使用
CardLayout
在视图之间切换谢谢您的CardLayout,使用此解决方案,错误不会出现,但我的JComboBox会显示在面板上。如何解决此错误?
remove(serveur); //Remove the JComboBox
fen.setServeur(serveur.getSelectedItem().toString()); //Call the function
add(serveur); //Then add the JCombpBox