有没有办法在选项卡组件中放置图像

我正在向标签添加图标,但我希望ImageIcon适合所有tabComponent。

在此处输入图像描述

我试过这段代码

ImageIcon icon = new ImageIcon("images/itemtexto-off.png"); Image img = icon.getImage() ; Image newimg = img.getScaledInstance( 50, 25, java.awt.Image.SCALE_DEFAULT ) ; icon = new ImageIcon( newimg ); tabbedPaneProductDetail.setIconAt(0, icon); 

我也试过这个解决方案,但没有奏效。

 JLabel label = new JLabel(icon); label.setBackground(Color.BLUE); tabbedPaneProductDetail.setTabComponentAt(1,label); 

您可以尝试使用UIManager。 在开始创建组件之前,在程序开头添加以下内容:

 UIManager.put("TabbedPane.tabInsets", new Insets(0, 0, 0, 0)); 

当然,并非所有LAF都支持此选项。 有关更多信息,请参阅UIManager默认值 。

我找到了一个解决方案,我不知道它是否合适,感谢@camickr

 tabbedPane.setUI(new SynthTabbedPaneUI(){ Insets insets =new Insets(0, 0, 0, 0); @Override protected Insets getTabInsets(int tabPlacement, int tabIndex){ return insets; } }); 

在此处输入图像描述

UPDATE

我找到另一个设置此属性的解决方案

 UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab.contentMargins", new Insets(0, 0, 0, 0));