单击事件处理程序:如何在单击时更改形状类型

Click Event Handler: How to change shape type on click

我正在尝试创建一些事件处理程序,这些事件处理程序会在单击时更改屏幕上的形状。当我点击 运行 时,屏幕上已经有一个默认的矩形。单击时,正方形变成圆形。我有另一个事件处理程序可以执行相反的操作,Circle to Square。我遇到的问题是因为直到事件处理程序发生之后才创建圆形,编译器给出错误:"Error: cannot find symbol" -> cir(我为圆圈指定的名称。 )

我觉得我为一个简单的问题走错了路。有没有办法让 Multishape 可以在点击时改变形状,或者类似的东西?

这是我目前的代码:

public class Multishape extends Application
{
    private Text selected;

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

    public void start(Stage stage)
    {
        rectangle rec = new Rectangle(240, 240);

        Group root = new Group();
        root.getChildren().add(rec);
        Scene scene = new Scene(root, 640, 480);
        rec.setFill(Color.color(1,1,1));
        rec.setStroke(Color.color(0,0,0));
        rec.setStrokeWidth(1);

        EventHandler<MouseEvent> STC = (MouseEvent e) ->
        { 
            Circle cir = new Circle(50.0f, Color.RED);
            cir.setFill(Color.color(1,1,1));
            cir.setStroke(Color.color(0,0,0));
            cir.setStrokeWidth(1);
            root.getChildren().add(cir);
            root.getChildren().remove(rec1);
        };

        EventHandler<MouseEvent> CTS = (MouseEvent e) ->
        { 
            Rectangle rec1 = new Rectangle(240, 240);
            rec1.setFill(Color.color(1,1,1));
            rec1.setStroke(Color.color(0,0,0));
            rec1.setStrokeWidth(1);
            root.getChildren().add(rec1);
            root.getChildren().remove(cir);
        };

        rec.addEventHandler(MouseEvent.MOUSE_PRESSED, STC);
        cir.addEventHandler(MouseEvent.MOUSE_PRESSED, CTS); 

        stage.setScene(scene);
        stage.show();
    }
}

关键是给需要它的变量更大的

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * Sedrick (SedJ601)
 * 
 */
public class App extends Application {
    //Declaring variables here gives them greater scope
    Rectangle rectangle = new Rectangle(240, 240, Color.BLUE);
    Circle circle = new Circle(50, Color.RED);    
    StackPane root = new StackPane();

    @Override
    public void start(Stage primaryStage) {
        rectangle.setOnMouseClicked(handleMouseClick);//Set mouse click handler
        circle.setOnMouseClicked(handleMouseClick);//Set mouse click handler

        root.getChildren().add(rectangle);//Set initial shape.

        Scene scene = new Scene(root, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    EventHandler<MouseEvent> handleMouseClick = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent t) {
            if(root.getChildren().get(0).equals(rectangle))//if the current shape showing is rectangle change it to circle, else do the opposite
            {
                root.getChildren().set(0, circle);
            }
            else
            {
                root.getChildren().set(0, rectangle);
            }
        }
    };
}