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

如何获得选定元素的背景色?

  •  2
  • Sandro  · 技术社区  · 15 年前

    在Swing中,当使用JTree/JList/JTable选择项目时,会更改其背景色。当组件未被选中时,是否有方法检索当前外观的颜色?非常感谢。

    3 回复  |  直到 15 年前
        1
  •  4
  •   Savvas Dalkitsis    15 年前

    您可以使用:

    UIManager.getColor("Tree.selectionBackground")
    

    其他有用的值包括:

    UIManager.getColor("Tree.selectionForeground")
    UIManager.getColor("Tree.textBackground")
    UIManager.getColor("Tree.textForeground")
    

    编辑: 提出的其他解决方案当然可以,但缺点是您需要一个组件,必须先选择它,然后才能调用该方法,否则您将获得未选择的背景。即使您没有树开始,也可以调用此解决方案:)

        2
  •  2
  •   camickr    15 年前
        3
  •  0
  •   BCunningham    15 年前