首页 > 解决方案 > JavaFX在运行时更改矩形的颜色

问题描述

我正在用 JavaFX 测试一些东西并遇到了问题。我有 3 个具有动作事件的单选按钮。这些单选按钮每个都代表一种颜色(红色、蓝色和黄色),如果按下它,正方形应该会改变颜色。

下面是我的按钮窗格: public class ButtonsPane extends VBox { private final ControlPane controlPane;

    public ButtonsPane(ControlPane controlPane) {
        this.controlPane = controlPane;
        this.setPrefHeight(150);
        this.setPrefWidth(120);

        RadioButton radioButton1 = new RadioButton("Red");
        radioButton1.setSelected(true);
        radioButton1.setOnAction(e -> controlPane.setColor("Red"));

        RadioButton radioButton2 = new RadioButton("Blue");
        radioButton2.setOnAction(e -> controlPane.setColor("Blue"));

        RadioButton radioButton3 = new RadioButton("Yellow");
        radioButton3.setOnAction(e -> controlPane.setColor("Yellow"));

        ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.getToggles().addAll(radioButton1, radioButton2, radioButton3);

        this.getChildren().addAll(radioButton1, radioButton2, radioButton3);
        this.setPadding(new Insets(20, 0, 0, 15));
    }
}

下面是我的控制面板。action 事件调用 DrawPane 中的一个方法,该方法尝试将 String 颜色转换为真实的 Color 对象值。

    public class ControlPane extends VBox {
    private Color color;
    private final DrawPane drawPane;

    public ControlPane(boolean buttons) {
        ButtonsPane buttonsPane = new ButtonsPane(this);
        ListViewPane listViewPane = new ListViewPane();
        drawPane = new DrawPane();

        SliderPane sliderPane = new SliderPane();

        if(buttons) {
            this.getChildren().add(buttonsPane);
        } else {
            this.getChildren().add(listViewPane);
        }

        this.setPrefHeight(350);
        this.setPrefWidth(120);
        this.getChildren().add(sliderPane);
        this.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
    }

    public void setColor(String color) {
        try {
            drawPane.setColor(Color.valueOf(color));
        } catch (Exception e) {
            drawPane.setColor(Color.WHITE);
        }

    }
}

如果颜色失败与否,我的绘图窗格中将调用一个方法来更改颜色。

public class DrawPane extends BorderPane {
    private final Rectangle rectangle;

    public DrawPane() {
        this.rectangle = new Rectangle();
        this.rectangle.setFill(Color.RED);
        this.rectangle.setHeight(180);
        this.rectangle.setWidth(180);
        this.rectangle.setStroke(Color.BLACK);

        this.setCenter(this.rectangle);
        this.setPadding(new Insets(10));
        this.setPrefHeight(350);
        this.setPrefWidth(380);
    }

    public void setColor(Color color) {
        System.out.println("true");
        this.rectangle.setFill(Color.BLACK);
        this.setBackground(new Background(new BackgroundFill(Color.YELLOW, null, null)));
    }
}

场景:

public class MyScene extends Scene {
    public MyScene(HBox root) {
        super(root);

        root.getChildren().addAll(new DrawPane(), new ControlPane(true));
    }
}

该方法被调用,因为我已经使用 System.out 对此进行了测试,但颜色没有更新。我错过了什么或做错了什么?

标签: javajavafx

解决方案


工作样本

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;

    public class RectangleApp extends Application {

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

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

            Rectangle rectangle = new Rectangle(100, 100, Color.BLACK);
            StackPane stackPane = new StackPane(rectangle);

            RadioButton redButton = new RadioButton("Red");
            RadioButton blueButton = new RadioButton("Blue");

            //It's not good style to get the color using valueOf() but I'm staying in context of the sample 
            redButton.setOnAction(event -> rectangle.setFill(Color.valueOf("red")));
            blueButton.setOnAction(event -> rectangle.setFill(Color.valueOf("blue")));

            ToggleGroup toggleGroup = new ToggleGroup();
            toggleGroup.getToggles().setAll(redButton, blueButton);

            HBox hBox = new HBox(redButton, blueButton);

            VBox vBox = new VBox(hBox, stackPane);
            Scene scene = new Scene(vBox, 400, 400);
            stage.setScene(scene);
            stage.show();
        }
    }

推荐阅读