Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaFX8中评估select绑定时发生异常_Java_Javafx 8 - Fatal编程技术网

在JavaFX8中评估select绑定时发生异常

在JavaFX8中评估select绑定时发生异常,java,javafx-8,Java,Javafx 8,以下是Pro JavaFx 8中的一个示例: package projavafx.reversi.examples; import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.geometry.Pos; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.effe

以下是Pro JavaFx 8中的一个示例:

    package projavafx.reversi.examples;

    import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Ellipse;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import projavafx.reversi.model.Owner;
import projavafx.reversi.model.ReversiModel;
    /**
     * @author Stephen Chin <steveonjava@gmail.com>
     */
    public class BorderLayoutExample extends Application {

        TilePane scoreTiles;
        TilePane titleTiles;

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

        @Override
        public void start(Stage primaryStage) {
            BorderPane borderPane = new BorderPane();
            borderPane.setTop(createTitle());
            borderPane.setCenter(createBackground());
            borderPane.setBottom(createScoreBoxes());
            Scene scene = new Scene(borderPane, 600, 400);
            primaryStage.setScene(scene);
            primaryStage.show();
           // scoreTiles.prefTileWidthProperty().bind(Bindings.selectDouble(scoreTiles.parentProperty(), "width").divide(2));
           // titleTiles.prefTileWidthProperty().bind(Bindings.selectDouble(titleTiles.parentProperty(), "width").divide(2));

        }

        private Node createTitle() {
            StackPane left = new StackPane();
            left.setStyle("-fx-background-color: black");
            Text text = new Text("JavaFX");
            text.setFont(Font.font(null, FontWeight.BOLD, 18));
            text.setFill(Color.WHITE);
            StackPane.setAlignment(text, Pos.CENTER_RIGHT);
            left.getChildren().add(text);
            Text right = new Text("Reversi");
            right.setFont(Font.font(null, FontWeight.BOLD, 18));
            titleTiles = new TilePane();
            titleTiles.setSnapToPixel(false);
            TilePane.setAlignment(right, Pos.CENTER_LEFT);
            titleTiles.getChildren().addAll(left, right);
            titleTiles.setPrefTileHeight(40);
            titleTiles.prefTileWidthProperty().bind(Bindings.selectDouble(titleTiles.parentProperty(), "width").divide(2));
            return titleTiles;
        }

        private Node createBackground() {
            Region answer = new Region();
            RadialGradient rg = new RadialGradient(225, 0, 0, 0, 1, true, CycleMethod.NO_CYCLE,
                    new Stop(0.0, Color.WHITE),
                    new Stop(1.0, Color.GRAY)
            );
            answer.setBackground(new Background(new BackgroundFill(rg, null, null)));
            //       answer.setStyle("-fx-background-color: radial-gradient(radius 100%, white, gray)");
            return answer;
        }

        private Node createScoreBoxes() {
            scoreTiles = new TilePane(createScore(Owner.BLACK), createScore(Owner.WHITE));
            scoreTiles.setSnapToPixel(false);
            scoreTiles.setPrefColumns(2);
            scoreTiles.prefTileWidthProperty().bind(Bindings.selectDouble(scoreTiles.parentProperty(), "width").divide(2));

            return scoreTiles;
        }

        private Node createScore(Owner owner) {
            Region background;
            Ellipse piece = new Ellipse(32, 20);
            piece.setFill(owner.getColor());
            DropShadow pieceEffect = new DropShadow();
            pieceEffect.setColor(Color.DODGERBLUE);
            pieceEffect.setSpread(.2);
            piece.setEffect(pieceEffect);

            Text score = new Text();
            score.setFont(Font.font(null, FontWeight.BOLD, 100));
            score.setFill(owner.getColor());
            Text remaining = new Text();
            remaining.setFont(Font.font(null, FontWeight.BOLD, 12));
            remaining.setFill(owner.getColor());
            VBox remainingBox = new VBox(10, piece, remaining);
            remainingBox.setAlignment(Pos.CENTER);
            FlowPane flowPane = new FlowPane(20, 10, score, remainingBox);
            flowPane.setAlignment(Pos.CENTER);
            background = new Region();
            background.setStyle("-fx-background-color: " + owner.opposite().getColorStyle());
            ReversiModel model = ReversiModel.getInstance();
            StackPane stack = new StackPane(background, flowPane);
            InnerShadow innerShadow = new InnerShadow();
            innerShadow.setColor(Color.DODGERBLUE);
            innerShadow.setChoke(.5);
            background.effectProperty().bind(Bindings.when(model.turn.isEqualTo(owner))
                    .then(innerShadow)
                    .otherwise((InnerShadow) null));
            DropShadow dropShadow = new DropShadow();
            dropShadow.setColor(Color.DODGERBLUE);
            dropShadow.setSpread(.2);

            piece.effectProperty().bind(Bindings.when(model.turn.isEqualTo(owner))
                    .then(dropShadow)
                    .otherwise((DropShadow) null));
            score.textProperty().bind(model.getScore(owner).asString());
            remaining.textProperty().bind(model.getTurnsRemaining(owner).asString().concat(" turns remaining"));
            return stack;
        }
    }

这里出了什么问题?

问题在于方法createTitle()中的以下代码位:

此时,标题尚未添加到borderPane,因此parentProperty的值为null,因此无法在其上找到width属性

CreateScoreBoxs()中的相同

不过,下一次,如果您减少一点示例代码,特别是从项目中删除对类的引用(import projavafx.reversi.model.ReversiModel;),就可以将其粘贴到IDE中并立即运行

      sept. 20, 2015 11:07:03 AM com.sun.javafx.binding.SelectBinding$SelectBindingHelper getObservableValue
      WARNING: Exception while evaluating select-binding [width]
      sept. 20, 2015 11:07:03 AM com.sun.javafx.binding.SelectBinding$SelectBindingHelper getObservableValue
      WARNING: Exception while evaluating select-binding [width]
titleTiles.prefTileWidthProperty().bind(
    Bindings.selectDouble(
        titleTiles.parentProperty(), "width").divide(2));