【问题标题】:Can we write a .setOnAction() into another .setOnAction() block in JavaFX?我们可以将 .setOnAction() 写入 JavaFX 中的另一个 .setOnAction() 块吗?
【发布时间】:2017-07-06 04:58:58
【问题描述】:

自从上学期上第一堂编程课以来,我就是 Java 初学者,今年夏天我正努力继续练习。我正在做一些非常基本和简单的事情,但我发现我遇到了问题。

public void start(Stage primaryStage) {
    try {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root,400,400);


scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("My little success story");


        TextInputDialog entername = new TextInputDialog("Your name");
        entername.setTitle("Player Information");
        entername.setHeaderText("What is going to be the name of the `person we'll follow?");`
        entername.setContentText("Please enter your name:");

        Optional<String>result = entername.showAndWait();
        if (result.isPresent()){
            name = result.get();
        }


        Image roleplay1 =new Image("Roleplay - 1.jpg");
        ImageView image1A = new ImageView();

        image1A.setImage(roleplay1);
        image1A.setFitHeight(400);
        image1A.setFitWidth(400);
        image1A.setSmooth(true);
        root.getChildren().add(image1A);

        Label welcome = new Label("Welcome "+name+" it's 2:00 `p.m."+newLine`
                +"You either can go to your bio class or to the gym.");

        HBox HWelcome = new HBox(welcome);
        HWelcome.setAlignment(Pos.BOTTOM_CENTER);
        HWelcome.setStyle("-fx-background-color: #faffff");

        Button goToClass = new Button("Go to class");
        goToClass.setPrefWidth(120.);

        Button goToGym = new Button("Go to the gym");
        goToGym.setPrefWidth(120.);

        HBox choice = new HBox(goToClass, goToGym);
        choice.setAlignment(Pos.BOTTOM_CENTER);
        choice.setSpacing(70.);
        choice.setPadding(new Insets(30));

        root.setBottom(HWelcome);
        root.setCenter(choice);


        goToClass.setOnAction(new EventHandler <ActionEvent>(){

            public void handle1(ActionEvent e){

                BorderPane root = new BorderPane();
                Scene scene = new Scene(root,400,400);


scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.setTitle("Roleplay");

                Image roleplay2A = new Image("Roleplay - 2A.jpg");
                ImageView image2A = new ImageView();

                image2A.setImage(roleplay2A);
                image2A.setSmooth(true);
                image2A.setFitHeight(400.);
                image2A.setFitWidth(400.);
                root.getChildren().add(image2A);

                Label scenario = new Label("You've arrived to class in `time."+newLine+`
                        "You are lucky, it was very important `today."+newLine+`
                        "You can team up with your best friend or with `the nerd.");`

                HBox HinClass = new HBox(scenario);
                HinClass.setAlignment(Pos.BOTTOM_CENTER);
                HinClass.setStyle("-fx-background-color: #faffff");

                Button yourFriend = new Button("Your friend");
                yourFriend.setPrefWidth(120.);

                Button theNerd = new Button("The nerd");
                theNerd.setPrefWidth(120.);

                HBox choice = new HBox(yourFriend, theNerd);
                choice.setAlignment(Pos.BOTTOM_CENTER);
                choice.setSpacing(70.);
                choice.setPadding(new Insets(30));


                root.setBottom(HinClass);
                root.setCenter(choice);


            }

            yourFriend.setOnAction(new EventHandler <ActionEvent>(){

                public void handle(ActionEvent e){

                            BorderPane root = new BorderPane();
                            Scene scene = new Scene(root,400,400);


scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                            primaryStage.setScene(scene);
                            primaryStage.setTitle("Roleplay");

                            Image image = new Image("Roleplay 3 - `A.jpg");`
                            ImageView img = new ImageView(image);
                            root.getChildren().add(img);
                            img.setSmooth(true);

                        }

                    });


        }); 

所以当我使用我的第一个按钮时一切都很好

    goToClass.setOnAction(new EventHandler <ActionEvent>(){

但是,当我尝试继续使用相同的技术时,每次用户点击带有此代码的按钮时,使用两个按钮创建一个新窗口

   yourFriend.setOnAction(new EventHandler <ActionEvent>(){

它说存在某种语法错误,我不能再进一步了。

我真的不知道我是否清楚,因为这是我在这里发布的第一个问题,但我希望你们能够帮助我!

非常感谢,

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    我没有测试过你的代码,但我只是想帮你消除编译错误,就像你说你是 Java 初学者一样

    package com.test;
    
    import java.util.Optional;
    
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextInputDialog;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class Random
    {
        public void start(Stage primaryStage)
        {
            try
            {
                BorderPane root = new BorderPane();
                Scene scene = new Scene(root, 400, 400);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.setTitle("My little success story");
    
                TextInputDialog entername = new TextInputDialog("Your name");
                entername.setTitle("Player Information");
                entername.setHeaderText("What is going to be the name of the `person we'll follow?");
                entername.setContentText("Please enter your name:");
    
                Optional<String> result = entername.showAndWait();
                String name = "";
                if (result.isPresent())
                {
                    name = result.get();
                }
    
                Image roleplay1 = new Image("Roleplay - 1.jpg");
                ImageView image1A = new ImageView();
    
                image1A.setImage(roleplay1);
                image1A.setFitHeight(400);
                image1A.setFitWidth(400);
                image1A.setSmooth(true);
                root.getChildren().add(image1A);
    
                Label welcome = new Label("Welcome " + name + " it's 2:00 `p.m." + System.lineSeparator() +
                    "You either can go to your bio class or to the gym.");
    
                HBox HWelcome = new HBox(welcome);
                HWelcome.setAlignment(Pos.BOTTOM_CENTER);
                HWelcome.setStyle("-fx-background-color: #faffff");
    
                Button goToClass = new Button("Go to class");
                goToClass.setPrefWidth(120.);
    
                Button goToGym = new Button("Go to the gym");
                goToGym.setPrefWidth(120.);
    
                HBox choice = new HBox(goToClass, goToGym);
                choice.setAlignment(Pos.BOTTOM_CENTER);
                choice.setSpacing(70.);
                choice.setPadding(new Insets(30));
    
                root.setBottom(HWelcome);
                root.setCenter(choice);
    
                goToClass.setOnAction(new EventHandler<ActionEvent>()
                {
    
                    Button yourFriend = new Button("Your friend");
    
                    public void handle(ActionEvent e)
                    {
    
                        BorderPane root = new BorderPane();
                        Scene scene = new Scene(root, 400, 400);
                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                        primaryStage.setScene(scene);
                        primaryStage.setTitle("Roleplay");
    
                        Image roleplay2A = new Image("Roleplay - 2A.jpg");
                        ImageView image2A = new ImageView();
    
                        image2A.setImage(roleplay2A);
                        image2A.setSmooth(true);
                        image2A.setFitHeight(400.);
                        image2A.setFitWidth(400.);
                        root.getChildren().add(image2A);
    
                        Label scenario = new Label("You've arrived to class in `time." + System.lineSeparator() +
                            "You are lucky, it was very important `today." + System.lineSeparator() +
                            "You can team up with your best friend or with `the nerd.");
    
                        HBox HinClass = new HBox(scenario);
                        HinClass.setAlignment(Pos.BOTTOM_CENTER);
                        HinClass.setStyle("-fx-background-color: #faffff");
    
                        yourFriend.setPrefWidth(120.);
    
                        Button theNerd = new Button("The nerd");
                        theNerd.setPrefWidth(120.);
    
                        HBox choice = new HBox(yourFriend, theNerd);
                        choice.setAlignment(Pos.BOTTOM_CENTER);
                        choice.setSpacing(70.);
                        choice.setPadding(new Insets(30));
    
                        root.setBottom(HinClass);
                        root.setCenter(choice);
    
                        yourFriend.setOnAction(new EventHandler<ActionEvent>()
                        {
                            public void handle(ActionEvent e)
                            {
    
                                BorderPane root = new BorderPane();
                                Scene scene = new Scene(root, 400, 400);
                                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                                primaryStage.setScene(scene);
                                primaryStage.setTitle("Roleplay");
    
                                Image image = new Image("Roleplay 3 - `A.jpg");
                                ImageView img = new ImageView(image);
                                root.getChildren().add(img);
                                img.setSmooth(true);
                            }
                        });
                    }
                });
            }
            finally
            {
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2014-11-03
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多