JavaFX 2.2(8)网格中的可单击组变得不可点击

时间:2014-08-16 14:54:26

标签: java 3d javafx-8

javafx.scene.layout.GridPane包含两个从javafx.scene.Group继承的典当。典当包含javafx.scene.shape.Cylinder

两次点击任何Pawn后,不再调用鼠标单击事件处理程序。当Group只有一个3D孩子时,它似乎是可点击的,为什么?

Pawn类的代码(最小化):

class Pawn extends Group {

   private static final double DEPTH  = 14.0;
   /*   */ static final double RADIUS = 30.0;
   /*   */ static final double MARGIN = 10.0;

   private final Cylinder shape;

   Pawn() {
      shape = new Cylinder( RADIUS, DEPTH );
      shape.setRotationAxis( Rotate.X_AXIS );
      shape.setRotate( 90.0 );
      shape.setTranslateZ( -DEPTH - 4.0 );
      getChildren().setAll( shape );
      setTranslateX( MARGIN );
   }

   void setQueen() {
      shape.setHeight( 2.0 * DEPTH );
      final Sphere crown = new Sphere( RADIUS / 4.0 );
      crown.setTranslateX( shape.getTranslateX());
      crown.setTranslateY( shape.getTranslateY());
      crown.setTranslateZ( -2.2 * DEPTH );
      crown.setMaterial( new PhongMaterial( Color.CYAN ));
      getChildren().add( crown );
   }
}

应用程序的代码(最小化):

public final class NotClickable extends Application {

   private int clickCount;

   private void onPawnClicked( Pawn pawn ) {
      System.err.println( "onPawnClicked" );
      if( ++clickCount > 1 ) {
         pawn.setQueen(); // pawns becomes no longer clickable
      }
   }

   private Pawn addCell( GridPane pane, int row, int col ) {
      final double size  = 2.0 * ( Pawn.MARGIN + Pawn.RADIUS );
      final double depth = 14.0;
      final Box    cell  = new Box( size, size, depth );
      final Pawn   pawn  = new Pawn();
      pane.add( cell, col, row );
      pane.add( pawn, col, row );
      pawn.setOnMouseClicked( e -> onPawnClicked( pawn ));
      return pawn;
   }

   @Override
   public void start( Stage stage ) throws Exception {
      final GridPane          pane   = new GridPane();
      final PerspectiveCamera camera = new PerspectiveCamera( true );
      camera.setNearClip  ( 0.1 );
      camera.setFarClip   ( 4000.0 );
      camera.setTranslateZ( -1000 );
      camera.setTranslateX( 100 );
      camera.setTranslateY( 1500 );
      camera.setRotationAxis( Rotate.X_AXIS );
      camera.setRotate( 45 );
      final Scene scene = new Scene( pane, 600, 1000, true );
      scene.setCamera( camera );
      scene.setFill  ( Color.LIGHTBLUE );
      stage.setScene ( scene );
      addCell( pane, 0, 0 );
      addCell( pane, 0, 1 );
      stage.show();
   }

   public static void main( String[] args ) {
      launch( args );
   }
}

应用的可见状态:

  • 在第二次点击之前:Before second click

  • 第二次点击后: After second click

输出:

onPawnClicked
onPawnClicked

第二次点击后点击任何一个pawn都没有任何效果,即使在非转换的pawn上也是如此。

问题:如何维护鼠标单击事件处理程序?

编辑 I've filled a bug into JavaFX-JIRA database

1 个答案:

答案 0 :(得分:2)

Martin Sladecek更新了RT-38336:

    Status: Resolved  (was: New)
Resolution: Not an Issue

这是因为默认的pickOnBounds设置。 GridPane(与任何地区一样)将其设置为true,但Group (Pawn)没有。 因此,一旦添加了球体,就会进入GridPane获胜的状态,因为它被边界(包括球体)拾取并且比棋子内的圆柱更近。 pawn本身不是由边界挑选的,所以它的距离是到圆柱的距离。

无论如何,在JavaFX中进行3D时,pickOnBounds是你的朋友(或敌人?)。您通常希望所有内容都为pickOnBounds == false,否则任何对象基本上都会占据围绕它的整个多维数据集(边界)。默认情况下,这通常是假的,但所有Regions / Panes / Controls除外。

在您的情况下,设置pane.setPickOnBounds( false );将解决问题。