2010-06-26 193 views
2

我在我的應用程序中使用了JTabbedPane。我添加了兩個標籤,它們是自定義類「ContentPanel」的實例。這擴展了JPanel並設置了背景,邊框等。基本上,這意味着我不必設置每個我想要應用此顏色方案的JPanel的屬性。我注意到,不僅他們的邊框出現了,而且另一個邊框(我認爲它是藍色的,至少在我的屏幕上)出現在這個邊框周圍,連接到選擇器本身(也就是你點擊的按鈕來獲得適當的看法)。我想改變這個邊框,因爲它對金/棕色配色方案看起來很古怪。有沒有人有任何想法如何做到這一點?我試過JTabbedPane.setBorder(邊框b),但不起作用。這只是設置了整個事物的邊界,包括選項卡選擇器..不是我想要的。JTabbedPane - 在標籤周圍設置默認邊框..?

任何幫助,這將不勝感激。

回答

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 
    //... 
} 

如果你不想去儘可能定義自己的大號&男,你必須設置一個自定義的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; 
    } 
}); 

你當然可以想要更改這些顏色設置。按照設定,你會看到哪些變量在哪裏使用。

+0

是的這看起來像我需要..謝謝。如果我有任何問題,我會再次評論。 Richard – ClarkeyBoy 2010-06-26 17:07:01

+0

嗯似乎工作,但不完全。我已經查看了BasicTabbedPaneUI的所有變量並設置了列出的所有顏色,但仍然存在稍微更薄的淺藍色/綠松石邊框。它與標籤「選擇器」本身的顏色相同。 – ClarkeyBoy 2010-06-26 17:28:01

+3

解決了它 - 我發現有人使用了UIManager.put(「TabbedPane.contentBorderInsets」,new Insets(0,0,0,0))。 ; aswel和你放的一樣。現在完全擺脫了邊界。再次感謝。理查德 – ClarkeyBoy 2010-06-26 17:48:59

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

} 
0

改變外觀和 「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); 
    } 
} 

}