简体   繁体   中英

JavaFX label binding to integer value not changing

I am building a battleship game in javaFX. For the game I want to display 2 labels, which depict the number of ships the player and the computer have. However the number does not change. My code is the following:

The Main Class can be implemented with the following code:

  package sample;

import java.util.Random;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

    private Board enemyBoard;

    private Random random = new Random();

    private Parent createContent() {
        BorderPane root = new BorderPane();
        root.setPrefSize(600, 800);

        EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                enemyBoard.ships--;
            }
        };

        enemyBoard = new Board();

        Label label4 = new Label("Computer Ships Available: ");
        IntegerProperty eships = new SimpleIntegerProperty(enemyBoard.ships);
        label4.textProperty().bind(eships.asString());
        Button newb = new Button("Change");
        newb.setOnAction(event1);
        root.setCenter(label4);
        root.setBottom(newb);

        return (root);
    }


    @Override
    public void start(Stage primaryStage) throws Exception {

        Scene scene = new Scene(createContent());
        primaryStage.setTitle("Battleship");
        primaryStage.setResizable(false);


        primaryStage.setScene(scene);

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        //set Stage boundaries to visible bounds of the main screen

        primaryStage.setX(primaryScreenBounds.getMinX());
        primaryStage.setY(primaryScreenBounds.getMinY());
        primaryStage.setWidth(primaryScreenBounds.getWidth()-400);
        primaryStage.setHeight(primaryScreenBounds.getHeight()-100);

        primaryStage.show();
    }

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


}

And the Board Class:

package sample;

import java.util.ArrayList;
import java.util.List;

import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Parent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Board extends Parent{
    private VBox rows = new VBox();
    private boolean enemy  = false;
    public int ships = 5;
}

What am I doing wrong?

You aren't changing the property you've bound the label to. Make the SimpleIntegerProperty accessible and change it. Something like this:

    EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e)
        {
            eships.set(eships.get()-1);
        }
    };

You might also want to put the property in your enemyBoard class. You don't need the raw int.

    EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e)
        {
            enemyBoard.ships.set(enemyBoard.ships.get()-1);
        }
    };

With:

public class Board extends Parent{
    private VBox rows = new VBox();
    private boolean enemy  = false;
    public SimpleintegerProperty ships = new SimpleIntegerProperty(5);
}

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