java - JTabbedPane FlowLayout 抛出 ArrayIndexOutOfBoundsException

标签 java swing jtabbedpane indexoutofboundsexception flowlayout

我正在尝试将流布局应用于 JTabbedPane,以便我可以设置 hgap(0) 和 vgap(0)。 JTabbedPane 的默认布局在各处留下了间隙。

当我尝试设置布局时,以下代码抛出:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.lang.reflect.InvocationTargetException;

import javax.swing.*;


public class Main {

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override public void run() {
                    JFrame F = new JFrame("Testing");
                    F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JTabbedPane Pane = new JTabbedPane();
                    Pane.setLayout(new FlowLayout(0, 0, FlowLayout.CENTER));
                    F.setLayout(new BorderLayout());
                    F.add(Pane, BorderLayout.CENTER);

                    Pane.addTab("Hello", new JPanel());
                                    F.pack();
                    F.setVisible(true);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

没有流程布局时我看到的内容: enter image description here

我想删除黑色 JPanel 周围看起来绿松石色的边框。我不想删除标签。我只想删除选项卡和组件之间的空间。我想删除上图中看起来呈蓝色的组件周围的空间。

为什么它会抛出一个异常,显示 ArrayIndexOutOfBounds:

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.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$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.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$1000(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$200(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$1.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)

最佳答案

我认为您不能弄乱 JTabbedPane 的布局。考虑这个根本没有意义。它可能有自己的专业布局。


编辑1
事实上它确实如此:TabbedPaneLayout

解决方案:不要弄乱 JTabbedPane 的布局,当然也不要将其替换为无法与 JTabbedPane 一起使用的其他布局。


编辑2
你说:

How can I remove the gaps around it then? I leaves these gaps all around the component :S I tried other layouts as well.. yeah they throw too.

最好更改 JTabbedPane 所持有的组件的布局,以允许它们重新调整大小并填充间隙,但为了让我们能够充分了解您可能遇到的问题,请显示以下图像正是您所看到的以及您想要展示的内容。


编辑3
您声明:

want to remove the turquoise looking border thing around the Black JPanel. I don't want to remove the tab. I just want to remove the space between the tab and the component. I want to remove the space around the component that looks blue-ish in the above picture.

这可能是外观和感觉问题,而不是布局问题。至于您原来问题的直接答案,答案是:不要弄乱 JTabbedPane 布局,句号。至于您试图解决的问题,请考虑使用不同的 L&F,或者如果这不起作用,请为 JTabbedPane 编写您自己的 ui 委托(delegate)。不过我自己从来没有做过这种事。

关于java - JTabbedPane FlowLayout 抛出 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574302/

相关文章:

java - Java 中的事件和监听器

java - 使用 ComboBox 更改面板的背景颜色

java - 使用 JPanel 和 JLabel 手动创建 JTabbedPane 的选项卡标题

java - jsp相册可以解析为类型

java - 获取所有开头的链接

java - libgdx 上的 touchDragged 问题

java - CursorLoader 通知数据变化

java - 使用键盘移动图像 - Java

java - 使用 insertTab() 插入选项卡时 JTabbedPane.getComponentAt() 似乎返回错误的组件

java - 如何使用 JTextArea 将新选项卡添加到 JTabbedPane