java - 程序运行正常但是显示很多错误

标签 java exception javafx

当我的程序运行时,它会显示这些错误,即使它也按照我的要求运行。仅当您按下简单或困难并获得至少两分时,才会弹出这些错误。我的代码将在错误下方给出。

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = Pane@3175f25b[styleClass=root]
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at TransFo.TransoFo.lambda$null$2(TransoFo.java:251)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

这是我的代码

public class TransoFo extends Application{

    private int total;

        public void start(Stage stage){
                Pane p = new Pane();
        Button b = new Button("easy");
        b.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" +
                "-fx-font-size: 15;" +
                "-fx-text-fill: white;"); 
        b.setLayoutX(307);
        b.setLayoutY(400);

        Button hard = new Button("hard");
        hard.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 120;" +
                "-fx-font-size: 15;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                "-fx-text-fill: white;");
        hard.setLayoutX(307);
        hard.setLayoutY(450);



             Circle bi = new Circle();
            Rectangle biRec = new Rectangle();
            Circle circle = new Circle();
            Rectangle rec = new Rectangle();

            rec.setWidth(20);
            rec.setHeight(30);

            rec.setArcWidth(5);
            rec.setArcHeight(5);
            rec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");


            circle.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            circle.setCenterX(370);
            circle.setCenterY(250);
            circle.setRadius(50);

            biRec.setWidth(30);
            biRec.setHeight(20);
            biRec.setArcWidth(5);
            biRec.setArcHeight(5);
            biRec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            bi.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            bi.setCenterX(370);
            bi.setCenterY(250);
            bi.setRadius(100);
            p.getChildren().addAll(bi, biRec, circle, rec);
            // transition for small circle and rectangle
            PathTransition pt1 = new PathTransition();
            pt1.setDuration(Duration.millis(1200));
            pt1.setPath(bi);
            pt1.setNode(biRec);
            pt1.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt1.setCycleCount(Timeline.INDEFINITE);
            pt1.setAutoReverse(false);

           pt1.play();

             PathTransition pt3 = new PathTransition();
            pt3.setDuration(Duration.millis(800));
            pt3.setPath(circle);
            pt3.setNode(rec);
            pt3.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt3.setCycleCount(Timeline.INDEFINITE);
            pt3.setAutoReverse(false);

           pt3.play();

//            button play event lambda expression                              
        b.setOnAction((ActionEvent event) -> {
            bi.setVisible(false);
            biRec.setVisible(false);
            circle.setVisible(false);
            rec.setVisible(false);
                 b.setVisible(false); 

            Circle big = new Circle();
            // create rectangle for big circle
            Rectangle bigRec = new Rectangle();
            Circle circ = new Circle();
            Rectangle r = new Rectangle();
            //event for small rectangle
            r.setWidth(20);
            r.setHeight(30);


            r.setArcWidth(5);
            r.setArcHeight(5);
            r.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            circ.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            circ.setCenterX(370);
            circ.setCenterY(300);
            circ.setRadius(50);


            bigRec.setWidth(30);
            bigRec.setHeight(20);
            bigRec.setArcWidth(5);
            bigRec.setArcHeight(5);
            bigRec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            big.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            big.setCenterX(370);
            big.setCenterY(300);
            big.setRadius(100);

            // transition for small circle and rectangle
            PathTransition pt2 = new PathTransition();
            pt2.setDuration(Duration.millis(1200));
            pt2.setPath(big);
            pt2.setNode(bigRec);
            pt2.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt2.setCycleCount(Timeline.INDEFINITE);
            pt2.setAutoReverse(true);

            pt2.play();

            PathTransition pt = new PathTransition();
            pt.setDuration(Duration.millis(800));
            pt.setPath(circ);
            pt.setNode(r);
            pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt.setCycleCount(Timeline.INDEFINITE);
            pt.setAutoReverse(false);

            pt.play();     


            Button b2 = new Button(" | | ");
              b2.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 70;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 700;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                "-fx-text-fill: white;");
        b2.setLayoutX(670);
        b2.setLayoutY(10);

        b2.setOnAction((ActionEvent event1) -> {
            pt2.stop();
            pt.stop();
            });                

       Button b3  = new Button(" ▶ ");
        b3.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 70;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 700;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                   "-fx-text-fill: white;");
                   b3.setLayoutX(590);
                   b3.setLayoutY(10);

                   b3.setOnAction((ActionEvent event2) -> {
                                pt.play();
                                pt2.play();

                            });     
                Button match = new Button(" Match ");
                match.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                "-fx-text-fill: white;");
                match.setLayoutX(310);
                match.setLayoutY(450);         

                Label scoreTitle = new Label("Score");  

                scoreTitle.setFont(new Font("Courier New bold", 30));
                scoreTitle.setLayoutX(310);
                scoreTitle.setLayoutY(40);
              // here is the button Match's event (lambda expression)
               Label score = new Label("0");


                 match.setOnAction((ActionEvent evt) -> {

                     Bounds bBounds = bigRec.getBoundsInParent();
                     Bounds bounds = r.getBoundsInParent();

                     if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) {
                      total += 1;
                       String str = Integer.toString(total);
                         score.setText(str);
                         score.setFont(new Font("Courier New bold", 20));
                          score.setLayoutX(350);
                          score.setLayoutY(90);
                          p.getChildren().add(score);

                            pt.play();
                          pt2.play();

                   }
                     else {
                         total = 0;
                         String str = Integer.toString(total);
                         score.setText(str);
                         score.setFont(new Font("Courier New bold", 20));
                          score.setLayoutX(350);
                          score.setLayoutY(90);
                          p.getChildren().add(score);
                     }
                }); 

            p.getChildren().addAll(big, bigRec, circ, r, b2, b3, match, scoreTitle);
                //fade transition for 
            FadeTransition ft = new FadeTransition(Duration.millis(1500), big);
            ft.setFromValue(0.1);
            ft.setToValue(1);
            ft.play();
               //transition for big rec
            FadeTransition ft2 = new FadeTransition(Duration.millis(1500), bigRec);
            ft2.setFromValue(0.1);
            ft2.setToValue(1);
            ft2.play();
               //transition for  circ
            FadeTransition ft3 = new FadeTransition(Duration.millis(1500), circ);
            ft3.setFromValue(0.1);
            ft3.setToValue(1);
            ft3.play();
               //transition for r
            FadeTransition ft4 = new FadeTransition(Duration.millis(1500), r);
            ft4.setFromValue(0.1);
            ft4.setToValue(1);
            ft4.play();
               //transition for match
            FadeTransition ft5 = new FadeTransition(Duration.millis(1500), match);
            ft5.setFromValue(0.1);
            ft5.setToValue(1);
            ft5.play();
               //transition for pause button
            FadeTransition ft6 = new FadeTransition(Duration.millis(3000), b2);
            ft6.setFromValue(0.1);
            ft6.setToValue(1);
            ft6.play();
               //transtion for play button
            FadeTransition ft7 = new FadeTransition(Duration.millis(3000), b3);
            ft7.setFromValue(0.1);
            ft7.setToValue(1);
            ft7.play();
        });



        hard.setOnAction((ActionEvent evter) -> {
                 bi.setVisible(false);
            biRec.setVisible(false);
            circle.setVisible(false);
            rec.setVisible(false);
                 b.setVisible(false);   

            Circle big = new Circle();
            // create rectangle for big circle
            Rectangle bigRec = new Rectangle();
            Circle circ = new Circle();
            Rectangle r = new Rectangle();
            //event for small rectangle
            r.setWidth(20);
            r.setHeight(30);


            r.setArcWidth(5);
            r.setArcHeight(5);
            r.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            circ.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            circ.setCenterX(370);
            circ.setCenterY(300);
            circ.setRadius(50);


            bigRec.setWidth(30);
            bigRec.setHeight(20);
            bigRec.setArcWidth(5);
            bigRec.setArcHeight(5);
            bigRec.setStyle("-fx-fill: #ff9933;" +
                    "-fix-stroke-width: 20;" +
                    "-fix-stroke: #ff4d4d;");

            big.setStyle("-fx-fill: #88ff4d;" +
                    "-fx-stroke-width: 12;" +
                    "-fx-stroke: #3399ff;");
            big.setCenterX(370);
            big.setCenterY(300);
            big.setRadius(100);

            // transition for small circle and rectangle
            PathTransition pt2 = new PathTransition();
            pt2.setDuration(Duration.millis(1100));
            pt2.setPath(big);
            pt2.setNode(bigRec);
            pt2.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt2.setCycleCount(Timeline.INDEFINITE);
            pt2.setAutoReverse(true);

            pt2.play();

            PathTransition pt = new PathTransition();
            pt.setDuration(Duration.millis(700));
            pt.setPath(circ);
            pt.setNode(r);
            pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt.setCycleCount(Timeline.INDEFINITE);
            pt.setAutoReverse(false);

            pt.play();     


            Button b2 = new Button(" | | ");
              b2.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 70;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 700;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                "-fx-text-fill: white;");
        b2.setLayoutX(670);
        b2.setLayoutY(10);

        b2.setOnAction((ActionEvent event1) -> {
            pt2.stop();
            pt.stop();
            });                

       Button b3  = new Button(" ▶ ");
        b3.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 70;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 700;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                   "-fx-text-fill: white;");
                   b3.setLayoutX(590);
                   b3.setLayoutY(10);

                   b3.setOnAction((ActionEvent event2) -> {
                                pt.play();
                                pt2.play();

                            });     
                Button match = new Button(" Match ");
                match.setStyle("-fx-background-radius: 3em;" + 
                "-fx-background-color: #66a3ff;" +
                "-fx-min-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-max-width: 120;" +
                "-fx-min-height: 40;" +
                "-fx-cursor: hand;" + 
                "-fx-text-fill: white;");
                match.setLayoutX(310);
                match.setLayoutY(450);   

                Label scoreTitle = new Label("Score");  

                scoreTitle.setFont(new Font("Courier New bold", 30));
                scoreTitle.setLayoutX(310);
                scoreTitle.setLayoutY(40);

                Label score = new Label("0");
              // here is the button Match's event (lambda expression)
                 match.setOnAction((ActionEvent evt) -> {

                     Bounds bBounds = bigRec.getBoundsInParent();
                     Bounds bounds = r.getBoundsInParent();

                     if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) {
                      total += 1;
                       String str = Integer.toString(total);
                          score.setText(str);
                         score.setFont(new Font("Courier New bold", 20));
                          score.setLayoutX(350);
                          score.setLayoutY(90);
                          p.getChildren().add(score);

                            pt.play();
                          pt2.play();

                   }
                     else{
                         total = 0;
                             String str = Integer.toString(total);
                          score.setText(str);
                         score.setFont(new Font("Courier New bold", 20));
                          score.setLayoutX(350);
                          score.setLayoutY(90);
                          p.getChildren().add(score);
                     }
                });

            p.getChildren().addAll(big, bigRec, circ, r, b2, b3, match, scoreTitle);
                //fade transition for 
            FadeTransition ft = new FadeTransition(Duration.millis(1500), big);
            ft.setFromValue(0.1);
            ft.setToValue(1);
            ft.play();
               //transition for big rec
            FadeTransition ft2 = new FadeTransition(Duration.millis(1500), bigRec);
            ft2.setFromValue(0.1);
            ft2.setToValue(1);
            ft2.play();
               //transition for  circ
            FadeTransition ft3 = new FadeTransition(Duration.millis(1500), circ);
            ft3.setFromValue(0.1);
            ft3.setToValue(1);
            ft3.play();
               //transition for r
            FadeTransition ft4 = new FadeTransition(Duration.millis(1500), r);
            ft4.setFromValue(0.1);
            ft4.setToValue(1);
            ft4.play();
               //transition for match
            FadeTransition ft5 = new FadeTransition(Duration.millis(1500), match);
            ft5.setFromValue(0.1);
            ft5.setToValue(1);
            ft5.play();
               //transition for pause button
            FadeTransition ft6 = new FadeTransition(Duration.millis(3000), b2);
            ft6.setFromValue(0.1);
            ft6.setToValue(1);
            ft6.play();
               //transtion for play button
            FadeTransition ft7 = new FadeTransition(Duration.millis(3000), b3);
            ft7.setFromValue(0.1);
            ft7.setToValue(1);
            ft7.play();
            // transition for Score Label
            FadeTransition ft8 = new FadeTransition(Duration.millis(3000), scoreTitle);
            ft8.setFromValue(0.1);
            ft8.setToValue(1);
            ft8.play();
            // transition for
        });

        p.getChildren().addAll(b, hard);
        p.setStyle("-fx-background-color: #88ff4d;");
        Scene s = new Scene(p, 750, 650);
        stage.setResizable(false);
        stage.setScene(s);
        stage.show();
 }      
    public static void main(String[] args) {
        Application.launch(args);
    } 
}

最佳答案

您的问题是您将同一节点多次添加到场景图中。

例如你有这个:

Label score = new Label("0");
match.setOnAction((ActionEvent evt) -> {
    Bounds bBounds = bigRec.getBoundsInParent();
    Bounds bounds = r.getBoundsInParent();
    if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) {
        total += 1;
        String str = Integer.toString(total);
        score.setText(str);
        score.setFont(new Font("Courier New bold", 20));
        score.setLayoutX(350);
        score.setLayoutY(90);
        p.getChildren().add(score);
        pt.play();
        pt2.play();
}....

这条线发生了什么

p.getChildren().add(score);

导致了您的问题。您应该设置它,然后引用它:

例如:

Label score = new Label("0");
score.setText(str);
score.setFont(new Font("Courier New bold", 20));
score.setLayoutX(350);
score.setLayoutY(90);
p.getChildren().add(score);

match.setOnAction((ActionEvent evt) -> {
    Bounds bBounds = bigRec.getBoundsInParent();
    Bounds bounds = r.getBoundsInParent();
    if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) {
        total += 1;
        String str = Integer.toString(total);
        score.setText(str);
        pt.play();
        pt2.play();
}....

关于java - 程序运行正常但是显示很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41959986/

相关文章:

java - 关系数据库(H2、Java): How do I constrain a foreign key to NOT match another foreign key in the same table?

c++ - 在 C++ 中使用模板时返回 NULL

c++ - 在编译时检测异常是否被禁用

iphone - iPhone 应用程序中出现奇怪的间歇性 "unrecognized selector"异常

java - 我怎样才能处理一个按钮上的两个 Action ?

java - JavaFX 中的 SubScene 不显示从 FXML 文件加载的元素

java - Joda MutableDateTime 的 setDate(DateTime) 方法如何工作?

Java线程队列

java - 使用 ControlsFx CheckComboBox 过滤 TableView

java - 初学者 - 从另一个类打开方法