2013-07-15 34 views

回答

4

JTabbedPane允許您提供一個組件作爲標籤「渲染器」(各種)。

查看JTabbedPane#setTabComponentAt瞭解更多詳情,並查看this example瞭解更多詳情。

已更新,例如

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestTabbedPaneIcon { 

    public static void main(String[] args) { 
     new TestTabbedPaneIcon(); 
    } 

    public TestTabbedPaneIcon() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JTabbedPane tp = new JTabbedPane(); 
       tp.addTab("Dates", new JPanel()); 
       tp.addTab("Deliveries", new JPanel()); 
       tp.addTab("Exports", new JPanel()); 

       tp.setTabComponentAt(0, getLabel("Dates", "/Icon03.png")); 
       tp.setTabComponentAt(1, getLabel("Deliveries", "/Icon01.png")); 
       tp.setTabComponentAt(2, getLabel("Exports", "/Icon02.png")); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(tp); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    protected JLabel getLabel(String title, String icon) { 
     JLabel label = new JLabel(title); 
     try { 
      label.setIcon(new ImageIcon(ImageIO.read(getClass().getResource(icon)))); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     return label; 
    } 
} 
+0

什麼..沒有屏幕截圖?你沒有圖像的標誌性動漫女孩..? :( –

+0

對不起,我會更新另一個問題;) – MadProgrammer

+0

同意......... – mKorbel

2

JTabbedPane中具有API設置到標籤的圖標,加入的標籤內容或更高時或者:

// when adding 
tabbedPane.addTab(String, Icon, Component); 
// after having added 
tabbedPane.setIconAt(int, Icon); 
+0

+1但注意一次,我試過我有一個問題與SwingUtilities.layoutLabel()???保護尺寸,甚至圖標有適當H&W以像素爲單位 – mKorbel

相關問題