2017-03-31 65 views
1

我想在我的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) 

我希望任何人知道發生了什麼事。

+1

您是在Swing事件線程上使用'SwingUtilities.invokeLater(Runnable r)'啓動GUI嗎? –

+4

歡迎來到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) –

+0

是的,我是。這是我不應該做的嗎? –

回答

4

我不知道爲什麼你設置BorderLayoutcontentPane,我建議你給變量名稱更改爲tabbedPane並刪除這一行:

tabbedPane.setLayout(new BorderLayout(0, 0)); 

此修正錯誤。

如果要編輯選項卡的佈局,請使用該佈局創建一個JPanel,並將其作爲參數傳遞,而不是您的JButton

另外一點需要注意的是不是叫setBoundspack()setLocation()setLocationRelativeTosetLocationByPlatform()方法,而不是

現在,順便說一句,注意,不要延長JFrame,看The use of multiple JFrames, Good/Bad practice?(普遍的共識說,這是一種不好的做法)。另請參閱Extends JFrame vs creating it inside of class

+0

這確實確實解決了它,謝謝!我對Java中的Swing和GUI頗爲陌生,所以我不知道這可能會搞砸了。 –

+0

@FinnBon:實際上,它完全阻止了JTabbedPane正確地佈局標籤,因爲它使用它自己的佈局管理器。但更多的是,從邏輯上思考,你爲什麼要這樣做?將來請發佈一個有效的[mcve]與您的問題,這意味着我們可以複製並粘貼它,並且無需修改即可運行它。由於您沒有顯示有效的課程或導入內容,所以我們無法使用您的代碼,關鍵是儘可能讓我們回答問題。 (1+這個答案) –