2009-11-23 81 views
8

你好我正在使用靈氣外觀和有圖標和文本的tabbedpane。 現在圖標出現在文本的右側,而我想將其放在左側。JTabbedPane:標籤左側的圖標

我也想在圖標和文字之間加一些間距。

謝謝!

回答

15

您需要自己設置選項卡組件;它管理標籤標題的呈現方式。

// Create tabbed pane and add tabs. 
JTabbedPane tabbedPane = ... 

// Create bespoke component for rendering the tab. 
JLabel lbl = new JLabel("Hello, World"); 
Icon icon = new ImageIcon(getClass().getResource("/foo/bar/hello.jpg")); 
lbl.setIcon(icon); 

// Add some spacing between text and icon, and position text to the RHS. 
lbl.setIconTextGap(5); 
lbl.setHorizontalTextPosition(SwingConstants.RIGHT); 

// Assign bespoke tab component for first tab. 
tabbedPane.setTabComponentAt(0, lbl); 

很明顯,你可以在一個實用方法封裝這樣的:

private void addTab(JTabbedPane tabbedPane, Component tab, String title, Icon icon) { 
    tabbedPane.add(tab); 

    JLabel lbl = ... // Create bespoke label for rendering tab title. 

    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, lbl); 
} 
+0

我使用此代碼(組件,字符串,圖標和工具提示)添加到標籤 pane.addTab(「名稱Tab「,新的ImageIcon(」resources \\ 1.png「),mainPanel,」工具提示文本「); 現在我想要的是相同的(文本出現在右邊,圖標在左邊) 我試過了你在上面評論中指定的相同的ü並且我的代碼中有異常...請幫助 – Gagan93 2013-11-05 08:10:28

+0

@ Gagan93:請你能把這個作爲一個單獨的問題發佈嗎?沒有看到異常,我不知道我能幫忙。 – Adamski 2013-11-05 08:35:05

+0

好吧,我不是因擔心陷入低估而發帖,我在2分鐘內發佈了一個與我的問題不同的問題......請提供解決方案 – Gagan93 2013-11-05 10:45:06