2014-04-17 13 views
0

我重寫功能getForeGroundAt和getBackgroundAt的選項卡面板爲這樣:getBackgroundAt方法在我的程序不採取行動,通常

jtp = new JTabbedPane(SwingConstants.LEFT) { 
    public Color getForegroundAt(int index) {    
     if (getSelectedIndex() == index) { 
      System.out.println("ueue1"); 
      return new Color(69, 69, 69); 
     } 
     return new Color(188, 188, 188); 
    } 
    public Color getBackgroundAt(int index) { 
     if (getSelectedIndex() == index) { 
      System.out.println("ueue2"); 
      return new Color(247, 248, 243); 
     } 
     return new Color(255, 255, 255); 
    } 
}; 

我現在面臨的問題是,在getBackgrountAt方法print語句是永遠甚至當我選擇不同的選項卡時setForegroundAt中的選項卡正常工作。結果是我無法設置所選標籤的背景顏色,但我可以設置前景顏色。我該如何解決這個問題?謝謝。

回答

0

這是一個LAF功能。 BasicTabbedPaneUI的代碼如下:

Color selectedColor = UIManager.getColor("TabbedPane.selected"); 
... 
... 
g.setColor(!isSelected || selectedColor == null ? 
    tabPane.getBackgroundAt(tabIndex) : selectedColor); 

所以UIManager中的屬性具有優先權。你可以嘗試用你想要的顏色更新UIManager的:

UIManager.put("TabbedPane.selected", ...); 

更多的信息和UIManager的屬性的列表,請參閱UIManager Defaults。前景沒有這個問題,因爲沒有選定前景的默認屬性。