我想在我的Java Swing應用程序中創建一個選項卡窗格,但它不工作。JTabbedPane ArrayIndexOutOfBoundsException:0
當將JTabbedPane
設置爲contentpane
時,一切都很順利。只要我嘗試添加一個標籤,我會得到一個ArrayIndexOutOfBoundsException: 0
。
儘管如此,組件仍然被添加到窗格中,只是拋出了這個錯誤。我的代碼和錯誤在下面。
代碼:
// This all happens in a class which extends JFrame
private JTabbedPane contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
ConfigBuilderWindow frame = new ConfigBuilderWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ConfigBuilderWindow() {
setTitle("Config Builder");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 916, 617);
contentPane = new JTabbedPane();
contentPane.setBorder(new LineBorder(Color.BLUE, 4));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.addTab("Test1", new JButton("Test1"));
contentPane.addTab("Test2", new JButton("Test2"));
setContentPane(contentPane);
}
錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at javax.swing.plaf.basic.BasicTabbedPaneUI.paintTabArea(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI.paint(Unknown Source)
at javax.swing.plaf.metal.MetalTabbedPaneUI.paint(Unknown Source)
at javax.swing.plaf.metal.MetalTabbedPaneUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.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)
我希望任何人知道發生了什麼事。
您是在Swing事件線程上使用'SwingUtilities.invokeLater(Runnable r)'啓動GUI嗎? –
歡迎來到Stack Overflow!請參考[遊覽](http://stackoverflow.com/tour),環顧四周,閱讀[幫助中心](http://stackoverflow.com/help),特別是[我該如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)和[我可以問什麼問題?](http://stackoverflow.com/help/on-topic)。 請提供[最小,完整和可驗證示例(MCVE)](http://stackoverflow.com/help/mcve) –
是的,我是。這是我不應該做的嗎? –