3

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

对此的任何帮助将不胜感激。

4

3 回答 3

12

这些颜色在外观中定义。如果您查看 的代码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;
   }
});

您当然可能想要更改这些颜色设置。设置后,您将看到在哪里使用了哪些变量。

于 2010-06-26T16:17:50.727 回答
1

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

创建您自己的选项卡式窗格类和嵌套的选项卡式窗格 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;        
        }
    }
    ........

}
于 2011-12-25T20:37:40.123 回答
1

使用“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<JComponent> {

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);
    }
}

}

于 2014-06-03T02:29:55.923 回答