有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图将标签文本设置为int的id

我试着用poäng1,2,3和4来表示每个球员在antalRatt1,2,3和4上的得分。每当一个玩家得到一个点,底部的手柄就会在antalRatt int上加上一个点,这条线是:antalrat1++;。但标签上只写着:“玩家名称: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));
            }

        }





    }
}


共 (1) 个答案

  1. # 1 楼答案

    您没有对整数使用属性,因此没有任何东西可以触发对绑定的更改

    从这里开始,根据需要更改其余代码:

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