2010-12-19 139 views
3

我有一个选项卡式窗格,我想调整选项卡的大小......不是整个选项卡式窗格,也不是面板添加到它。但标签本身....更清楚我想要的是... alt text
改变显示tab1和tab2的大小........我该怎么做...... ??调整选项卡式窗格中的选项卡的大小

回答

5

覆盖calculateTabWidthBasicTabbedPaneUI这样的:

public static void main(String[] args) throws Exception { 

    JTabbedPane tabs = new JTabbedPane(JTabbedPane.LEFT); 
    tabs.setUI(new BasicTabbedPaneUI() { 
     @Override 
     protected int calculateTabWidth(
       int tabPlacement, int tabIndex, FontMetrics metrics) { 
      return 200; // the width of the tab 
     } 
    }); 

    tabs.add("Tab 1", new JButton()); 
    tabs.add("Tab 2", new JButton()); 

    JFrame frame = new JFrame("Test"); 
    frame.add(tabs); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 300); 
    frame.setVisible(true); 
} 
1

本身是由UI委托的嵌套类,TabbedPaneLayout,在其中您可以覆盖preferredTabAreaWidth()确定标签的最佳尺寸。