简体   繁体   中英

I am working on making a magic 8 ball in JGrasp using java for my class. how do I get my code to run? I do not know what my error is or how to fix it?

this is my code

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);
         }
      }

This is the error message I got after trying to run my code after compiling it.

----jGRASP exec: java --module-path C:\\Users\\Isabelle rose\\Downloads\\openjfx-11.0.2_windows-x64_bin-sdk (1)\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls FinalProject8ball Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62 ) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.IllegalArgumentException: StackPane@4c7dc253[styleClass=root]is already set as root of another scene at javafx.graphics/javafx.scene.Scene$8.invalidated(Scene.java:1216) at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112) at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147) at javafx.graphics/javafx.scene. Scene.setRoot(Scene.java:1178) at javafx.graphics/javafx.scene.Scene.(Scene.java:356) at javafx.graphics/javafx.scene.Scene.(Scene.java:236) at FinalProject8ball.start(FinalProject8ball.java:58) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) ... 1 more Exception running application F inalProject8ball

The real error in the stack is the following:

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

Look at this line:

Scene scene2 = new Scene(sPane, 300, 400);

Try this instead:

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

NOTE:

Although your program will run with this single change, I think that you're going to find that it doesn't look the way it should. You're using two stackpanes, with a circle on one and a triangle on the other. Think of a stackpane as a stack of nodes, with each node layered one on top of the other, as if you were physically stacking them. Pretty sure you just need one stackpane with your circle and triangle added to them to represent an 8-Ball.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM