How to change the color of pane in javafx?

2020-08-10 07:54发布

问题:

I want to change the color of a Pane which I get as a String from user. How can I set this String as a background color in my pane?

Code:

colorField.setOnKeyTyped(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent t) {
        color = colorField.getText();
    }
});

回答1:

If you really just want to know how to accomplish that particular thing, I'd suggest the following:

Set the Nodes' CSS like this, using the hexacolor that was entered by the user:

String enteredByUser = "abcdef";
yournode.setStyle("-fx-background-color: #" + enteredByUser);

If you want to know more please be more specific with you questions and provide some code samples.

Since you tagged this question with 'javafx-8' i'll provide that code example as well(only works in javafx 8):

yournode.setBackground(new Background(new BackgroundFill(Color.web("#" + enteredByUser), CornerRadii.EMPTY, Insets.EMPTY)));

Hope it helps, Laurenz