我正在为我的课程使用java在JGrasp中制作一个魔法8球。如何让代码运行?我不知道我的错误是什么,也不知道如何修复它?

我正在为我的课程使用java在JGrasp中制作一个魔法8球。如何让代码运行?我不知道我的错误是什么,也不知道如何修复它?,java,jgrasp,Java,Jgrasp,这是我的密码 import java.util.*; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.scene.shape.Circle; import javafx.scene.shape

这是我的密码

import java.util.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Polygon;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.geometry.Pos;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import java.security.SecureRandom;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

   public class FinalProject8ball extends Application
   {
      @Override
      public void start(Stage primaryStage)
      {
         Circle circle = new Circle(80);
         circle.setStroke(Color.BLACK);
         circle.setFill(Color.BLACK);

         StackPane sPane = new StackPane();
         sPane.getChildren().add(circle);

         Scene scene = new Scene(sPane, 300, 400);
         primaryStage.setScene(scene);
         primaryStage.show();       
         Polygon triangle = new Polygon();
         triangle.getPoints().setAll(
            50.0, 50.0,
            60.0, 60.0,
            20.0, 40.0
            );

         StackPane sPane1 = new StackPane();
         sPane1.getChildren().add(triangle);

         Scene scene2 = new Scene(sPane, 300, 400);
         primaryStage.setScene(scene2);
         primaryStage.show();

         String answers[] = {"It is certain", "It is decidedly so", "Without a doubt",
            "Yes - definitely", "You may rely on it", "As I see it, yes",
            "Most likely", "Outlook good", "Signs point to yes",
            "Yes", "Reply hazy, try again", "Ask again later",
            "Better not tell you now", "Cannot predict now", "Concentrate and ask again",
            "Don't count on it", "My reply is no", "My sources say no",
            "Outlook not so good", "Very doubtful"};

        //constructs a random number  
         SecureRandom randomNumber = new SecureRandom();
         ImageIcon image = new ImageIcon("magic8ball.jpg");
         int counter = 0;

        //the loop keeps asking the person to plat till no button is clicked
         while(!(counter == 1)) {
            //prompts the user to ask a yes or no question 
            String answer = JOptionPane.showInputDialog(null, 
                    "PLease enter a yes or no question:", 
                    "WELCOME: What will your answer be?!", JOptionPane.INFORMATION_MESSAGE);

            //displays the answer
            if(answer != null)
               JOptionPane.showMessageDialog(null, answer+ "\n" + answers[randomNumber.nextInt(answers.length)], 
                  "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);

            //gives the user the option to click yes or no to continue or end the program
            counter = JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", 
                    JOptionPane.YES_NO_OPTION, 0, image);                           
         }

        //displays my name after user enters no to give credit
         JOptionPane.showMessageDialog(null, "Created by Isabelle", "Have a MAGICAL day! Your answers have been answerd.", 
                JOptionPane.PLAIN_MESSAGE, image);
      }
      public static void main(String[] args)
      {
         Application.launch(args);
         }
      }
这是我在编译代码后尝试运行代码时收到的错误消息

----jGRASP exec:java--模块路径C:\Users\Isabelle rose\Downloads\openjfx-11.0.2\U windows-x64\U bin-sdk(1)\javafx-sdk-11.0.2\lib--添加模块=javafx.controls FinalProject8ball 应用程序启动方法中的异常 java.lang.reflect.InvocationTargetException 位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 位于java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.base/java.lang.reflect.Method.invoke(Method.java:566) 位于javafx.graphics/com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:464) 位于javafx.graphics/com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:363) 位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 位于java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.base/java.lang.reflect.Method.invoke(Method.java:566) 位于java.base/sun.launcher.launchelper$FXHelper.main(launchelper.java:1051) 原因:java.lang.RuntimeException:应用程序启动方法中的异常 位于javafx.graphics/com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:900) 在javafx.graphics/com.sun.javafx.application.LaunchImpl.lambda$launchApplication$2(LaunchImpl.java:195) 位于java.base/java.lang.Thread.run(Thread.java:834) 原因:java.lang.IllegalArgumentException:StackPane@4c7dc253[styleClass=root]已设置为另一场景的根 位于javafx.graphics/javafx.scene.scene$8.invalidated(scene.java:1216) 位于javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112) 位于javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147) 位于javafx.graphics/javafx.scene.scene.setRoot(scene.java:1178) 在javafx.graphics/javafx.scene.scene.(scene.java:356) 在javafx.graphics/javafx.scene.scene.(scene.java:236) 在FinalProject8ball.start(FinalProject8ball.java:58) 在javafx.graphics/com.sun.javafx.application.launchempl.lambda$launchApplication1$9(launchempl.java:846) 在javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) 在javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) 位于java.base/java.security.AccessController.doPrivileged(本机方法) 位于javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) 位于javafx.graphics/com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:96) 在javafx.graphics/com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) 位于javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) ... 还有一个
运行应用程序FinalProject8ball时出现异常堆栈中的实际错误如下:

Caused by: java.lang.IllegalArgumentException: StackPane@70ea261c[styleClass=root]is already set as root of another scene
请看这一行:

Scene scene2 = new Scene(sPane, 300, 400);
请尝试以下方法:

// You already used 'sPane' as the root of a previous scene called 'scene'
Scene scene2 = new Scene(sPane1, 300, 400);
注:


虽然您的程序将运行这个单一的更改,但我认为您会发现它看起来不像它应该的样子。您使用的是两个堆叠窗格,一个是圆形,另一个是三角形。将stackpane视为一个节点堆栈,每个节点一层一层地叠在另一层上,就好像您在物理上堆叠它们一样。可以肯定的是,您只需要一个stackpane,其中添加了圆和三角形来表示一个8球。

欢迎!这将有助于描述您的代码应该做什么,因为当前的标题和文本没有提供有关这方面的详细信息。请看,您可以相应地编辑您的问题。我正在尝试使用。我用它来提问,然后我挣扎着用代码来实际创建形状。但这就是我得到的答案。更新答案。