如何在Javafx中获取平铺窗格的背景色?

如何在Javafx中获取平铺窗格的背景色?,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,问题描述-在我的应用程序中,我需要恢复平铺窗格(或任何其他控件)的颜色,但我找不到任何属性/函数 我使用“平铺窗格”显示色样,在其单击事件中,我需要其背景色 我想要的:我想要在点击事件上获得控件的背景色在ActionEvent中尝试一下,你可以这样做 handle(ActionEvent event){//suppose we are in the handle method Object o = event.getSource(); if(o instanceof Region){

问题描述-在我的应用程序中,我需要恢复平铺窗格(或任何其他控件)的颜色,但我找不到任何属性/函数

我使用“平铺窗格”显示色样,在其单击事件中,我需要其背景色


我想要的:我想要在点击事件上获得控件的背景色

ActionEvent
中尝试一下,你可以这样做

handle(ActionEvent event){//suppose we are in the handle method
   Object o = event.getSource();
   if(o instanceof Region){
       Background b = ((Region)o).getBackground();
       Paint p = b.getFills().get(0).getFill();//paint is actually your color :)
       if(p instanceof Color){
          ((Color)p) //now you have a color :)

希望它有帮助

ActionEvent
中尝试一下,您可以做到这一点

handle(ActionEvent event){//suppose we are in the handle method
   Object o = event.getSource();
   if(o instanceof Region){
       Background b = ((Region)o).getBackground();
       Paint p = b.getFills().get(0).getFill();//paint is actually your color :)
       if(p instanceof Color){
          ((Color)p) //now you have a color :)

希望它有帮助

您应该检查这个问题:您应该检查这个问题:谢谢您,先生,但是我通过创建带有背景色属性的自定义控件来解决了这个问题。由于最后一个填充是要渲染的,所以获得最后一个填充可能会更好(根据javafx.scene.layout.Background的文档“每个定义的背景填充都是按顺序渲染的”). 因此它应该是
b.getFills().get(b.getFills().size()-1.getFill()
而不是
b.getFills().get(0.getFill()
谢谢您,先生,但是我通过创建带有back color属性的自定义控件解决了这个问题,因为它是最后一个要渲染的填充,所以获得最后一个填充可能更好(根据javafx.scene.layout.Background的文档“每个定义的BackgroundFill都是按顺序呈现的”)。因此它应该是
b.getFills().get(b.getFills().size()-1).getFill()
而不是
b.getFills().get(0).getFill()