JTabbedPane ArrayIndexOutOfBoundsException: 0

JTabbedPane ArrayIndexOutOfBoundsException: 0

我正在尝试在我的 Java Swing 应用程序中创建一个选项卡式窗格,但它不起作用。

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.run(Unknown Source)
at javax.swing.RepaintManager.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.access00(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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.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)

我希望任何人都知道发生了什么事。

我不确定为什么要将 BorderLayout 设置为 contentPane,我建议您将变量名称更改为 tabbedPane 并删除此行:

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

这修复了错误。

如果您想编辑选项卡的布局,请使用该布局创建一个 JPanel 并将其作为参数而不是您的 JButton 传递。

另一件需要注意的事情不是调用 setBounds 而是调用 pack()setLocation()setLocationRelativeTosetLocationByPlatform() 方法

现在,作为旁注,不要扩展 JFrame,参见 The use of multiple JFrames, Good / Bad practice? (The general consensus says it's a bad practice). Also see Extends JFrame vs creating it inside of class