我正在编写一个小程序,其中多个形状将在JavaFX中可见。我正在尝试创建一个按钮,通过它可以将所有形状的颜色更改为用户选择的颜色。
现在,我只能通过单独更改lambda表达式中的每个形状来实现这一点。现在还可以,因为只有三种形状,但下一种会很不方便。有人能想到一种方法将所有形状组合在一起并访问";setFill";一次改变它们的方法?
这是代码:
// Calling Semi-Circle method
Arc semicircle1 = shapes1.getsemicircle();
// create Colors button and set is as invisible until a shape value is passed by user
Button buttonColors = new Button();
buttonColors.setText("Choose a color for the Shape");
buttonColors.setVisible(true);
buttonColors.setOnAction( e ->
{
if (textField1.getText().equalsIgnoreCase("Grey"))
{
label1.setText("Grey");
semicircle1.setFill(Color.GREY);
}
});
您可以创建一些属性并将所有形状绑定到它
final ObjectProperty<Paint> fillProperty = new SimpleObjectProperty(Color.GREY);
...
semicirle1.fillProperty().bind(fillProperty);
pentagon1.fillProperty().bind(fillProperty);
rectangle1.fillProperty().bind(fillProperty);
...
fillProperty.set(Color.RED);