JTabbedPane – 在标签周围设置默认边框..?

我在我的应用程序中使用JTabbedPane。 我添加了两个选项卡,它们是自定义类“ContentPanel”的实例。 这扩展了JPanel并设置了背景,边框等等。基本上它意味着我不必设置我想要应用此颜色方案的每个JPanel的属性。 我注意到它们的边框不仅出现了,而且还有另一个边框(我认为是蓝色 – 至少在我的屏幕上)出现在这个边框周围,连接到标签“选择器”本身(即你点击的按钮以获得适当的观点)。 我想改变这个边界,因为它看起来很奇怪对金色/棕色配色方案。 有谁知道如何做到这一点? 我尝试过JTabbedPane.setBorder(边框b),但这不起作用。 这只是围绕整个事物设置边框,包括选项卡选择器..不是我想要的。

任何有关这方面的帮助将不胜感激。

这些颜色在外观中定义。 如果查看BasicTabbedPaneUI的代码,您会注意到installDefaults()设置了一堆protected Color实例变量。 它们在L&F中定义的键也可在此处获得。

 protected void installDefaults() { LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background", "TabbedPane.foreground", "TabbedPane.font"); highlight = UIManager.getColor("TabbedPane.light"); lightHighlight = UIManager.getColor("TabbedPane.highlight"); shadow = UIManager.getColor("TabbedPane.shadow"); darkShadow = UIManager.getColor("TabbedPane.darkShadow"); //... // a lot more stuff //... } 

如果您不想定义自己的L&F,则可以在选项卡式窗格上设置自定义UI委托:

 myTabbedPane.setUI(new BasicTabbedPaneUI() { @Override protected void installDefaults() { super.installDefaults(); highlight = Color.pink; lightHighlight = Color.green; shadow = Color.red; darkShadow = Color.cyan; focus = Color.yellow; } }); 

您当然可以想要更改这些颜色设置。 如上所述,您将看到在哪里使用哪些变量。

不影响L&F和JVM运行时系统范围的设置代码解决方案。

创建自己的选项卡式窗格类和嵌套的tabbed-pane-UI类,以处理“特定”选项卡窗格类的问题。 下面的代码是原始的:(最后的答案是2010年,但这也可能有用。)

 public class DisplayTabbedPane extends JTabbedPane implements MouseListener, ChangeListener { public DisplayTabbedPane() { setTabPlacement(SwingConstants.BOTTOM); // UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0)); // works but is a JVM system wide change rather than a specific change NoInsetTabbedPaneUI ui = new NoInsetTabbedPaneUI(); // this will build the L&F settings for various tabbed UI components. setUI( ui ); // override the content border insets to remove the tabbed-pane // blue border around the pane ui.overrideContentBorderInsetsOfUI(); } /** * Class to modify the UI layout of tabbed-pane which we wish to override * in some way. This modification only applies to objects of this class. * Doing UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0)); * would affect all tabbed-panes in the JVM run-time. * * This is free to use, no copyright but is "AS IS". */ class NoInsetTabbedPaneUI extends MetalTabbedPaneUI { /** * Create tabbed-pane-UI object to allow fine control of the * L&F of this specific object. */ NoInsetTabbedPaneUI(){ super(); } /** * Override the content border insets of the UI which represent * the L&F of the border around the pane. In this case only care * about having a bottom inset. */ public void overrideContentBorderInsetsOfUI(){ this.contentBorderInsets.top = 0; this.contentBorderInsets.left = 0; this.contentBorderInsets.right = 0; this.contentBorderInsets.bottom = 2; } } ........ } 

用“UIManager”改变外观和感觉

  UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new BackgroundPainter(Color.GRAY)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white)); UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new BackgroundPainter(Color.white)); 

BackgroundPainter类

 public class BackgroundPainter implements Painter { private Color color = null; BackgroundPainter(Color c) { color = c; } @Override public void paint(Graphics2D g, JComponent object, int width, int height) { if (color != null) { g.setColor(color); g.fillRect(0, 0, width - 1, height - 1); } } 

}