簡體   English   中英

JavaFX label 綁定到 integer 值不變

[英]JavaFX label binding to integer value not changing

我正在 javaFX 中構建一個戰艦游戲。對於這個游戲,我想顯示 2 個標簽,描述玩家和計算機擁有的戰艦數量。 但是數量沒有變化。 我的代碼如下:

Main Class可以用以下代碼實現:

  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);
    }


}

和董事會 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;
}

我究竟做錯了什么?

您沒有更改將 label 綁定到的屬性。 使 SimpleIntegerProperty 可訪問並更改它。 像這樣:

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

您可能還想將該屬性放入您的 enemyBoard class 中。您不需要原始整數。

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

和:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM