簡體   English   中英

嘗試將標簽文本設置為 int 的 id

[英]Trying to set a Label text to a int's id

我試圖讓標簽 poäng1,2,3 和 4 說明每個玩家在 antalRatt1,2,3 和 4 上有多少分。每次玩家獲得一分時,底部的手柄會在antalRatt int 與這一行: antalRatt1++;。 但標簽上只寫着:“球員姓名:0。” 即使玩家的分數超過 0 分。 這是為什么?

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import com.sun.javafx.scene.LayoutFlags;
import java.beans.beancontext.BeanContext;

public class Main extends Application implements EventHandler<ActionEvent> {


    int antalRatt1 = 0;
    int antalRatt2 = 0;
    int antalRatt3 = 0;
    int antalRatt4 = 0;


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


        buttonDoneQ1.setOnAction(this);
        buttonDoneQ2.setOnAction(this);
        buttonDoneQ3.setOnAction(this);
        buttonDoneQ4.setOnAction(this);

        Label poäng1 = new Label("");
        Label poäng2 = new Label("");
        Label poäng3 = new Label("");
        Label poäng4 = new Label("");
        layoutPoäng.getChildren().addAll(poäng1, poäng2, poäng3, poäng4, buttonPoängDone);
        poäng1.textProperty().bind(
                Bindings.concat(
                        namnSpelare1.textProperty(), ": ", antalRatt1, "."
                )
        );
        poäng2.textProperty().bind(
                Bindings.concat(
                        namnSpelare2.textProperty(), ": ", antalRatt2, "."
                )
        );
        poäng3.textProperty().bind(
                Bindings.concat(
                        namnSpelare3.textProperty(), ": ", antalRatt3, "."
                )
        );
        poäng4.textProperty().bind(
                Bindings.concat(
                        namnSpelare4.textProperty(), ": ", antalRatt4, "."
                )
        );

}
    @Override
    public void handle(ActionEvent event) {
        if (event.getSource() == buttonDoneQ1) {
            if (svarFråga1.getText().equals(answers[randomNum1])) {
                window.setScene(sceneRatt);
                antalRatt1++;
                buttonDoneRättSvar.setOnAction(e -> window.setScene(sceneQ2));

            } else {
                window.setScene(sceneFel);
                buttonDoneFelSvar.setOnAction(e -> window.setScene(sceneQ2));
            }
        }

        if (event.getSource() == buttonDoneQ2) {
            window.setScene(sceneRatt);

            if (svarFråga2.getText().equals(answers[randomNum2])) {
                window.setScene(sceneRatt);
                antalRatt2++;
                buttonDoneRättSvar.setOnAction(e -> window.setScene(sceneQ3));

            } else {
                window.setScene(sceneFel);
                buttonDoneFelSvar.setOnAction(e -> window.setScene(sceneQ3));
            }

        }
        if (event.getSource() == buttonDoneQ3) {
            window.setScene(sceneRatt);

            if (svarFråga3.getText().equals(answers[randomNum3])) {
                window.setScene(sceneRatt);
                antalRatt3++;
                buttonDoneRättSvar.setOnAction(e -> window.setScene(sceneQ4));

            } else {
                window.setScene(sceneFel);
                buttonDoneFelSvar.setOnAction(e -> window.setScene(sceneQ4));
            }

        }
        if (event.getSource() == buttonDoneQ4) {
            window.setScene(sceneRatt);

            if (svarFråga4.getText().equals(answers[randomNum4])) {
                window.setScene(sceneRatt);
                antalRatt4++;
                buttonDoneRättSvar.setOnAction(e -> window.setScene(scenePoängTavla));

            } else {
                window.setScene(sceneFel);
                buttonDoneFelSvar.setOnAction(e -> window.setScene(scenePoängTavla));
            }

        }





    }
}

您沒有使用整數的屬性,因此沒有什么可以觸發對綁定的更改。

從這里開始並根據需要更改其余代碼:

SimpleIntegerProperty antalRatt1 = new SimpleIntegerProperty(0);
SimpleIntegerProperty antalRatt2 = new SimpleIntegerProperty(0);
SimpleIntegerProperty antalRatt3 = new SimpleIntegerProperty(0);
SimpleIntegerProperty antalRatt4 = new SimpleIntegerProperty(0);

將 antalRatt1++ 替換為:

antalRatt1.set(antalRatt1.get()+1);

暫無
暫無

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

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