2

我想將流佈局應用到JTabbedPane,以便我可以設置hgap(0)和vgap(0)。 JTabbedPane的默認佈局會留下空隙。JTabbedPane FlowLayout拋出ArrayIndexOutOfBoundsException

當我嘗試設置佈局下面的代碼拋出:

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(); 
     } 
    } 
} 

我看到了什麼,而不流佈局:我想刪除綠松石環視黑色的JPanel邊框的事情 enter image description here

。我不想刪除標籤。我只想刪除標籤和組件之間的空間。我想刪除上圖中看起來是藍色的組件周圍的空間。

爲什麼它拋出的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) 
+0

我不認爲你可以搞砸JTabbedPane的佈局。即使考慮到這一點也沒有意義。它可能有自己的專業佈局。 –

+0

我怎樣才能消除它周圍的空白呢?我在組件周圍留下了這些空白:S我也嘗試過其他佈局,也是。 – Brandon

回答

3

我不認爲你可以亂用JTabbedPane的佈局。即使考慮到這一點也沒有意義。它可能有自己的專業佈局。


編輯1
而事實上,它的確:TabbedPaneLayout

解決方案:不要亂用JTabbedPane的佈局,當然不換它的另一個佈局,不會與JTabbedPane一起使用。


編輯2
你狀態:

我怎樣才能去除空白圍繞它呢?我在組件周圍留下了這些空白:S我也嘗試過其他佈局,也是。

這很可能是最好的改變正在舉行的JTabbedPane中組件的佈局,讓他們重新大小和填補空白,但讓我們完全可以理解,你是有什麼問題,請顯示您正在看到的內容以及您想要展示的內容。


編輯3
幽州:

要刪除的綠松石環視黑色的JPanel邊框的事情。我不想刪除標籤。我只想刪除標籤和組件之間的空間。我想刪除上圖中看起來是藍色的組件周圍的空間。

這可能是一看那&感覺問題,而不是一個佈局的問題。至於直接回答你原來的問題,答案是:不要搞亂JTabbedPane佈局,期間。至於你正在嘗試解決的問題,考慮使用不同的L & F,或者如果這不起作用,請爲JTabbedPane編寫自己的ui委託。儘管我自己從來沒有做過這樣的事情。

+0

嗨,先生。我添加了一張照片。以及與它的描述指出了我想要刪除的內容。 – Brandon

+0

@CantChooseUsernames:請參閱編輯3 –

+0

我會將此標記爲已解決。這足夠符合我的想法..我嘗試了不同的外觀和感覺。我試過:'UIManager.getDefaults()。put(「TabbedPane.contentBorderInsets」,new Insets(0,0,0,0)); \t \t UIManager.getDefaults()。put(「TabbedPane.tabsOverlapBorder」,false);' 適用於右側,底部,左側邊界。現在只需要刪除頂部。 Thanx無論如何。 – Brandon

相關問題