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

Java 单击事件处理程序:如何在单击时更改形状类型,java,javafx,event-handling,root,shapes,Java,Javafx,Event Handling,Root,Shapes,我正在尝试创建一些事件处理程序,这些事件处理程序将在单击时更改屏幕上的形状。当我点击run时,屏幕上已经有一个默认的矩形。单击时,正方形将变为圆形。我有另一个事件处理程序,它将执行相反的操作,从圆到方。我遇到的问题是,由于直到事件处理程序发生后才创建圆形,编译器会给出错误:“错误:找不到符号”->cir(我为圆形指定的名称) 对于一个简单的问题,我觉得我走错了路。有没有一种方法可以使一个多形状在单击时可以在形状之间改变,或者类似的东西 这是我目前的代码: public class Multish

我正在尝试创建一些事件处理程序,这些事件处理程序将在单击时更改屏幕上的形状。当我点击run时,屏幕上已经有一个默认的矩形。单击时,正方形将变为圆形。我有另一个事件处理程序,它将执行相反的操作,从圆到方。我遇到的问题是,由于直到事件处理程序发生后才创建圆形,编译器会给出错误:“错误:找不到符号”->cir(我为圆形指定的名称)

对于一个简单的问题,我觉得我走错了路。有没有一种方法可以使一个
多形状
在单击时可以在形状之间改变,或者类似的东西

这是我目前的代码:

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();
    }
}
公共类Multishape扩展应用程序
{
选择私人文本;
公共静态void main(字符串[]args)
{
启动(args):
}
公众假期开始(阶段)
{
矩形rec=新矩形(240240);
组根=新组();
root.getChildren().add(rec);
场景=新场景(根,640480);
rec.setFill(Color.Color(1,1,1));
记录设定行程(颜色颜色(0,0,0));
记录设定行程宽度(1);
EventHandler STC=(MouseEvent e)->
{ 
圆圈cir=新圆圈(50.0f,颜色为红色);
cir.setFill(Color.Color(1,1,1));
循环设定行程(颜色颜色(0,0,0));
循环设定行程宽度(1);
root.getChildren().add(cir);
root.getChildren().remove(rec1);
};
EventHandler CTS=(MouseEvent e)->
{ 
矩形rec1=新矩形(240240);
rec1.setFill(Color.Color(1,1,1));
记录1.设定行程(颜色。颜色(0,0,0));
记录1.设定行程宽度(1);
root.getChildren().add(rec1);
root.getChildren().remove(cir);
};
rec.addEventHandler(MouseEvent.MOUSE_按下,STC);
cir.addEventHandler(按下MouseEvent.MOUSE_,CTS);
舞台场景;
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);
            }
        }
    };
}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
/**
*塞德里克(SedJ601)
* 
*/
公共类应用程序扩展应用程序{
//在这里声明变量给了它们更大的作用域
矩形矩形=新矩形(240,240,Color.BLUE);
圆圈=新圆圈(50,颜色为红色);
StackPane root=新的StackPane();
@凌驾
公共无效开始(阶段primaryStage){
rectangle.setOnMouseClicked(handleMouseClick);//设置鼠标单击处理程序
circle.setOnMouseClicked(handleMouseClick);//设置鼠标单击处理程序
root.getChildren().add(矩形);//设置初始形状。
场景=新场景(根,500500);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射();
}    
EventHandler handleMouseClick=新建EventHandler(){
@凌驾
公共无效句柄(MouseEvent t){
if(root.getChildren().get(0).equals(rectangle))//如果当前显示的形状是矩形,请将其更改为圆形,否则请执行相反的操作
{
root.getChildren().set(0,圆);
}
其他的
{
root.getChildren().set(0,矩形);
}
}
};
}
关键是赋予需要它的变量更大的值

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);
            }
        }
    };
}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
/**
*塞德里克(SedJ601)
* 
*/
公共类应用程序扩展应用程序{
//在这里声明变量给了它们更大的作用域
矩形矩形=新矩形(240,240,Color.BLUE);
圆圈=新圆圈(50,颜色为红色);
StackPane root=新的StackPane();
@凌驾
公共无效开始(阶段primaryStage){
rectangle.setOnMouseClicked(handleMouseClick);//设置鼠标单击处理程序
circle.setOnMouseClicked(handleMouseClick);//设置鼠标单击处理程序
root.getChildren().add(矩形);//设置初始形状。
场景=新场景(根,500500);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射();
}    
EventHandler handleMouseClick=新建EventHandler(){
@凌驾
公共无效句柄(MouseEvent t){
if(root.getChildren().get(0).equals(rectangle))//如果当前显示的形状是矩形,请将其更改为圆形,否则请执行相反的操作
{
root.getChildren().set(0,圆);
}
其他的
{
root.getChildren().set(0,矩形);
}
}
};
}

矩形
圆形
一个更大的范围。给
矩形
圆形
一个更大的范围。