2017-01-31 3 views
0

Когда моя программа запускается, она показывает эту ошибку, даже если она работает так же, как я прошу об этом. Эта ошибка появляется только тогда, когда вы нажимаете кнопку «легко или трудно» и получаете как минимум две точки. Мой код будет указан ниже ошибок.Программа работает нормально, но показывает так много ошибок.

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = [email protected][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); 
    } 
} 
+0

Пуск изолируя ответственную линию самостоятельно. Слишком много кода, чтобы увидеть это легко. Изоляция - это ваша работа. Используйте отладчик или внеклассный материал или вставляйте системные сообщения. – Aziuth

+0

Вы должны сделать оценку метки глобальной и добавить ее только один раз. добавьте метку в начало, но не в обработчик событий. – Sedrick

ответ

1

Вы должны сделать ярлык оценка глобального и добавить его только один раз.

добавьте метку в начало, но не в обработчик событий.

поиск и удалить все

p.getChildren().add(score); 

найти Pane p = new Pane(); и добавить под нее.

Pane p = new Pane(); 
Label score = new Label("0"); 

Теперь измените p.getChildren().addAll(bi, biRec, circle, rec); на:

p.getChildren().addAll(bi, biRec, circle, rec, score); 
2

Ваш вопрос в том, что вы добавляете один и тот же узел в дереве сцены больше, чем один раз.

Например у вас есть это:

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(); 
}.... 
Смежные вопросы