JTabbedPane ArrayIndexOutOfBoundsException:0

时间:2017-03-31 17:17:06

标签: java swing exception jtabbedpane

我正在尝试在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$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 个答案:

答案 0 :(得分:4)

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

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

这可以解决错误。

如果您要修改标签的布局,请创建一个包含该布局的JPanel,并将其作为参数而不是JButton传递。

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

现在,作为旁注,不要延长JFrame,请参阅The use of multiple JFrames, Good / Bad practice?(普遍的共识认为这是一种不好的做法)。另请参阅Extends JFrame vs creating it inside of class