代码之家  ›  专栏  ›  技术社区  ›  ClarkeyBoy

JTabbedPane-设置选项卡周围的默认边框。。?

  •  2
  • ClarkeyBoy  · 技术社区  · 16 年前

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

    任何帮助都将不胜感激。

    3 回复  |  直到 11 年前
        1
  •  12
  •   akf    16 年前

    这些颜色在外观中定义。如果你看一下 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;
       }
    });
    

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

        2
  •  1
  •   David    14 年前

    不影响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;        
            }
        }
        ........
    
    }
    
        3
  •  1
  •   Harsha Basnayake    12 年前

    使用“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));
    

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

    }