如何在javafx上选择形状以对其进行更改?

如何在javafx上选择形状以对其进行更改?,javafx,Javafx,如何选择形状(圆形、矩形等),以便在单击某些按钮时仅编辑选定的形状 代码如下: package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.geometry.*; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.shape.*; import javafx

如何选择形状(圆形、矩形等),以便在单击某些按钮时仅编辑选定的形状

代码如下:

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.shape.*;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import java.util.Random;

public class CirclesAndSquares extends Application {

    @Override
    public void start(Stage primaryStage) {
        /** set up the pane and boxes*/
        BorderPane pane = new BorderPane();
        HBox top = new HBox();
        top.setAlignment(Pos.CENTER);
        HBox bottom = new HBox();
        bottom.autosize();
        bottom.setAlignment(Pos.CENTER);
        VBox left = new VBox();
        left.autosize();
        left.setAlignment(Pos.CENTER);
        VBox right = new VBox();
        right.autosize();
        right.setAlignment(Pos.CENTER);
        Pane center = new Pane();
        center.autosize();
        center.setStyle("-fx-border-color: black");

        /** set and place the buttons */
        Button btnRed = new Button("Red");
        Button btnGrn = new Button("Green");
        left.getChildren().addAll(btnRed, btnGrn);
        Button btnRot = new Button("Rotate");
        right.getChildren().add(btnRot);
        Button btnSqr = new Button("Square");
        Button btnCrcl = new Button("Circle");
        bottom.getChildren().addAll(btnSqr, btnCrcl);
        pane.setLeft(left);
        pane.setRight(right);
        pane.setTop(top);
        pane.setBottom(bottom);
        pane.setCenter(center);

        /** Draw circle and square */
        Rectangle square = new Rectangle();
        center.getChildren().add(square);
        Circle circle = new Circle();
        center.getChildren().add(circle);
        Shape shape;

        btnCrcl.setOnAction(e -> {
            circle.setRadius(50);
            circle.setStroke(Color.BLACK);
            circle.setFill(null);
            circle.setCenterX(circle.getRadius()+5 + Math.random()*(center.getWidth() - 2*circle.getRadius()-5));
            circle.setCenterY(circle.getRadius()+5 + Math.random()*(center.getHeight() - 2*circle.getRadius()-5));
        } );

        btnSqr.setOnAction(e -> {
            square.setHeight(100);
            square.setWidth(100);
            square.setStroke(Color.BLACK);
            square.setFill(null);
            square.setX(Math.random()*(center.getWidth() - square.getWidth()));
            square.setY(Math.random()*(center.getHeight() - square.getHeight()));
        } );



        Scene scene = new Scene(pane,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setTitle("Draw square and circle");
        primaryStage.setScene(scene);
        primaryStage.show();

        btnRed.setOnAction(e -> {
            if (true) {
                System.out.println("true");
                circle.setFill(Color.RED);}
        } );

    }


    class CustomPane extends StackPane {
          public CustomPane(Button btn) {
            getChildren().addAll(btn);
            setStyle("-fx-border-color: red");
            setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
          }

    }
    public static void main(String[] args) {
      launch(args);
    }
}
这就是它的样子:

当我点击“红色”时,我想用红色填充“选定形状”

并通过点击“圆形”按钮或“方形”按钮来选择形状

或者单击窗格上的形状

如何使形状“选定”


在JavaFX中是否有一些简单的方法可以做到这一点?

实现跟踪上次单击哪个节点的逻辑“在此范围内定义变量不起作用。”=>然后不要定义变量来跟踪该范围内当前选定的形状,在lambda块外定义它(例如,作为封闭类的成员)。可能重复:。请注意,对于非填充形状,单击处理程序可能无法执行此操作。准确地击中边界可能很棘手。选择边界可能会产生不准确的结果;基本上,不是一个边平行于轴的矩形的所有东西都可能产生误报。@Coder88您是否按照我的建议定义了变量,如?如果是这样,那么它应该会起作用。相反,如果您定义了一个局部变量,并试图在lambda中更改为变量值,则。