首页 > 解决方案 > 处理圆形类并增加半径时,无法让我的代码更新 javafx 中的 GUI

问题描述

我有这个代码,它应该通过改变圆的半径来增加或减小圆的大小。我的问题是,由于某种原因,半径更新时圆圈没有更新。半径已更新,但圆不会扩大或缩小。谁能帮我吗?下面是我的代码。

我尝试了很多事情,但最终变得越来越复杂,没有成功。

public class Circle_GUI extends Application {

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

        BorderPane root = new BorderPane();
        CirclePane cp = new CirclePane();
        root.setCenter(new CirclePane());
        HBox hbox = new HBox();
        hbox.setPadding(new Insets(10, 20, 10, 20));
        hbox.setAlignment(Pos.CENTER);
        hbox.setSpacing(10);
        Button btnEnlarge = new Button("Enlarge");
        btnEnlarge.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                cp.enlargeShrinkCircle(true);
                System.out.println("You have enlarged");
            }
        });
        Button btnShrink = new Button("Shrink");
        btnShrink.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                cp.enlargeShrinkCircle(false);
                System.out.println("You have shrunk");
            }
        });
        hbox.getChildren().addAll(btnEnlarge, btnShrink);
        root.setBottom(hbox);

        Scene scene = new Scene(root, 400, 300);
        stage.setScene(scene);
        stage.setTitle("JFK=Event=Demo");
        stage.show();
    }
    class CirclePane extends StackPane {
        Circle circle = null;

        CirclePane() {
            circle = new Circle();
            circle.setRadius(100);
            circle.setStroke(Color.BLUE);
            circle.setFill(Color.AQUA);
            this.getChildren().add(circle);

        }

        void enlargeShrinkCircle(boolean enlarge) {
            if (enlarge) {
                circle.setRadius(circle.getRadius() + 10);
            } else {
                double r = circle.getRadius();
                r -= 10;
                if (r < 1.0) {
                    r = 1.0;
                }
                    circle.setRadius(r);
            }

        }
    }

我希望 GUI 上的 Circles 大小在我按下放大按钮时增加,在我按下缩小按钮时减小。

标签: eclipseanimationjavafx

解决方案


推荐阅读