如何将 JavaFX 变量传递给 CSS 样式?

How to pass JavaFX variable into CSS style?

我想采用选定的颜色并更改按钮的背景颜色。

private void  handleItemBackAction(ActionEvent eve)
{
          System.out.println("You clicked Set Background Color of Item!");     

        java.awt.Color color=JColorChooser.showDialog(null,"Select a color",java.awt.Color.CYAN);

        String hex = Integer.toHexString(color.getRGB() & 0xffffff);

        hex="#"+hex;
        Text.setText(hex);
        ShortcutButton.setStyle("-fx-background-color: hex;");
    }

尝试用变量内容值代替其名称:

ShortcutButton.setStyle("-fx-background-color: " + hex + ";");

您的十六进制变量中有您想要的颜色。

if (!buttonColor.getStyle().trim().contains("-fx-background-color:" +hex)){   
    buttonColor.setStyle("-fx-background-color: " + hex + ";");
}