2013-08-05 21 views
1

我正在準備一個使用JTabbedPane的一些水平選項卡的窗口,選項卡和窗口都已正確準備。如何處理JTabbedPane中選項卡標題的高度

現在我需要根據我的要求(基於我的邏輯返回整數值,我需要設置級別)在基礎級別設置這些選項卡。

水平樣子:

enter image description here

能否請您指教?

+2

不是真的支持,最大控制可以通過tabbedPane.setTabComponentAt(..)來實現,它允許使用自定義「選項卡」(但不佈局控制) – kleopatra

+1

您可以使用圖標作爲[示例](http://stackoverflow.com/a/3484251/230513)? – trashgod

回答

4

就在眼前截圖:

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class TabHeightTest { 
    public JComponent makeUI() { 
    JTabbedPane tabbedPane = new JTabbedPane(
     JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT); 
    tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() { 
     @Override protected int calculateTabHeight(
     int tabPlacement, int tabIndex, int fontHeight) { 
     return 32; 
     } 
     @Override protected void paintTab(
     Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, 
     Rectangle iconRect, Rectangle textRect) { 
     if(tabIndex==0) { 
      rects[tabIndex].height = 20 + 1; 
      rects[tabIndex].y = 32 - rects[tabIndex].height + 1; 
     } else if(tabIndex==1) { 
      rects[tabIndex].height = 26 + 1; 
      rects[tabIndex].y = 32 - rects[tabIndex].height + 1; 
     } 
     super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect); 
     } 
    }); 
    tabbedPane.addTab("000", new JLabel("aaaaaaaaaaa")); 
    tabbedPane.addTab("111", new JScrollPane(new JTree())); 
    tabbedPane.addTab("222", new JSplitPane()); 

    return tabbedPane; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    frame.getContentPane().add(new TabHeightTest().makeUI()); 
    frame.setSize(320, 240); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 
} 
1

選項卡邊界是外觀和感覺的責任。如果你在控制這個(如果你不是這樣,你不應該嘗試這種不可移植的技巧),你可以在標籤窗格的佈局管理器中修改它。

對於BasicLookAndFeel製表符邊界計算在BasicTabbedPaneUI.TabbedPaneLayout.calculateTabRects()BasicTabbedPaneUI.TabbedPaneScrollLayout.calculateTabRects()中完成。對於基本L & F派生的主題,BasicTabbedPaneUI有一個createLayout()方法,可以覆蓋該方法以返回具有所需行爲的佈局管理器。

相關問題