【问题标题】:Bind CSS Style Property to Node in JavaFX将 CSS 样式属性绑定到 JavaFX 中的节点
【发布时间】:2016-03-22 06:04:17
【问题描述】:

我想为我的项目设置一个模型,以便我的控制器可以相互通信。我希望它有一个 setter 和 getter,以便轻松访问任一类的某些节点的样式。

我的问题:是否可以将样式属性(例如“-fx-background-color: blue”)绑定到节点?

根据我的研究,我发现这对于标签的文本值绝对是可能的(由 James_D 在这里解释:JavaFX - How to use a method in a controller from another controller?),但我很难弄清楚用“setStyle”做类似事情的语法是什么" 会。

我目前拥有的模型:

 public class Model {

    private final StringProperty shadow = new SimpleStringProperty("-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.24), 10,0,0,0)");

    public StringProperty shadowProperty() {
        return shadow;
    }

    public final String getShadow() {
        return shadowProperty().get();
    }

    public final void setShadow(String shadow) {
        shadowProperty().set(shadow);
    }
}

我知道如何从控制器设置“影子”值,但我不明白如何绑定另一个控制器的节点以监听该更改。

假设节点是这样的:

@FXML AnchorPane appBar

我希望“appBar”承担对模型中“shadow”所做的任何更改。那会是什么样子?

【问题讨论】:

  • 你能举一个你试过的例子,并解释为什么它不起作用?目前尚不清楚确切的问题是什么。

标签: css javafx properties styles bind


【解决方案1】:

您需要向 shadowProperty 添加一个监听器来监听它的变化。

something.shadowProperty() .addListener( (observable, oldValue, newValue) -> {
    //do something with appBar 
}) ;

我不完全确定您想要实现什么,但这应该可以回答您关于如何收听属性更改的问题。

PS:我是手机,所以不保证错别字

编辑:您还可以将一个对象的属性绑定到另一个对象的属性。为此使用bind()

编辑:这是一个例子:

import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    Property<Background> backgroundProperty;
    StringProperty styleProperty;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        VBox root = new VBox(10);

        backgroundProperty = new SimpleObjectProperty<>();
        styleProperty = new SimpleStringProperty();

        // Pane that changes background by listener
        Pane pane1 = new Pane();
        pane1.setMinHeight(40);
        backgroundProperty.addListener( (observable, oldValue, newValue) -> {
            pane1.setBackground(backgroundProperty.getValue());
        });

        // Pane that changes background by property binding
        Pane pane2 = new Pane();
        pane2.setMinHeight(40);
        pane2.backgroundProperty().bind(backgroundProperty);

        // Pane that binds the style property
        Pane pane3 = new Pane();
        pane3.setMinHeight(40);
        pane3.styleProperty().bind(styleProperty);

        backgroundProperty.setValue(new Background(new BackgroundFill(Color.RED, null, null)));
        styleProperty.setValue("-fx-background-color: black");

        root.getChildren().add(pane1);
        root.getChildren().add(pane2);
        root.getChildren().add(pane3);

        Scene scene = new Scene(root, 200, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

【讨论】:

  • 感谢您的回复。我的问题是.. 绑定节点的 css 属性会是什么样子?例如“-fx-background-color”。
  • 你当然也可以使用一个字符串属性,监听它并通过在监听器中设置新的css样式来改变样式
  • 更新为绑定到窗格的 styleProperty
  • 这正是我需要了解它是如何工作的。非常感谢。
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 2020-02-11
  • 2014-05-23
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
相关资源
最近更新 更多