我试图在我的 Java Swing 应用程序中创建一个选项卡式 Pane ,但它不起作用。
将 JTabbedPane
设置为 contentpane
时,一切正常。一旦我尝试添加一个选项卡,我就会收到一个 ArrayIndexOutOfBoundsException: 0
。
尽管如此,组件还是被添加到了 Pane 中,只是抛出了这个错误。我的代码和错误在下面。
代码:
// 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)
我希望任何人都知道发生了什么。
最佳答案
我不确定为什么要为 contentPane
设置 BorderLayout
,我建议您将变量名称更改为 tabbedPane
并删除这一行:
tabbedPane.setLayout(new BorderLayout(0, 0));
这修复了错误。
如果您想编辑选项卡的布局,请使用该布局创建一个 JPanel
并将其作为参数而不是您的 JButton
传递。
要注意的另一件事不是调用 setBounds
而是调用 pack()
和 setLocation()
或 setLocationRelativeTo
或setLocationByPlatform()
方法代替
现在,作为旁注,不要扩展 JFrame
,参见 The use of multiple JFrames, Good / Bad practice? (普遍认为这是一种不好的做法)。另见 Extends JFrame vs creating it inside of class
关于java - JTabbedPane ArrayIndexOutOfBoundsException : 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146316/