如何增加记分牌,Java FX

时间:2018-11-26 04:40:25

标签: javafx

在我确定哪个球员拥有最高价值卡之后,我尝试添加到记分板上。标签不会更新。在玩家4玩完之后,我假设剩下的所有游戏都是在幕后进行的,即谁拥有最高的牌并更新了分数。

Button p4 = new Button("Player 4");
    p4.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            int n = rand.nextInt(56)+1;
            //(Double.parseDouble(card.get(n).getValue()));
            deckView4.setImage(cards.get((n)));

            p4.setDisable(true);


            array[3] = (Double.parseDouble(card.get(n).getValue()));
            Suits[3] = card.get(n).getSuit();


            if(array[0] > array[1] || array[0] > array[2] || array[0] > array[3]){

            incrementIndex1();
            }
                else if(array[1] > array[2] || array[1] > array[3]) {
                    incrementIndex2();  
                }
                else if(array[2] > array[3]) {
                    incrementIndex3();
                }
                else {
                    incrementIndex4();
                }event.consume();
            }

    });

按钮代码上方是这个

//creates Player1 ScoresNames
    Label score1 = new Label();
    score1.setText("P1 -> ");
    score1.setPadding(new Insets(100, 0, 0, 0));

    Label score1_0 = new Label();
    score1_0.setText("" + index1 + "");
    score1_0.setPadding(new Insets(100, 0, 0, 50));

    //creates Player2 ScoreNames
    Label score2 = new Label();
    score2.setText("P2 -> ");
    score2.setPadding(new Insets(99, 0, 0, 0));

    Label score2_0 = new Label();
    score2_0.setText("" + index2 + "");
    score2_0.setPadding(new Insets(177, 0, 0, 50));

    //creates Player3 ScoreName
    Label score3 = new Label();
    score3.setText("P3 -> ");
    score3.setPadding(new Insets(98, 0, 0, 0));

    Label score3_0 = new Label();
    score3_0.setText("" + index3 + "");
    score3_0.setPadding(new Insets(252, 0, 0, 50));


    //creates Player4 ScoreName
    Label score4 = new Label();
    score4.setText("P4 -> ");
    score4.setPadding(new Insets(97, 0, 0, 0));

    Label score4_0 = new Label();
    score4_0.setText("" + index4 + "");
    score4_0.setPadding(new Insets(325, 0, 0, 50));

    //vBox for scoresNames
    VBox score = new VBox(-40);
    score.getChildren().addAll(score1,score2,score3,score4);
    score.setAlignment(Pos.CENTER_LEFT);


    //groups 4 players together
    g.getChildren().addAll(sp,sp1,score,h1,score1_0,score2_0, score3_0, score4_0, deckView, deckView2, deckView3, deckView4);
    //bp.setTop(t);
    bp.setCenter(g);
    //bp.setLeft(tL);
    bp.getChildren().addAll(createButtons(), vb1);
    //bp.getChildren().add(g);

最后,这是初始化上述变量以增加分数:

int index1 = 00;
int index2 = 00;
int index3 = 00;
int index4 = 00;

private static ArrayList<Suit_Value> card = new ArrayList<Suit_Value>();
public static void main(String[] args) {



    cg cgg = new cg();

    cgg.readFromFile("cards");
    launch();

}
private void incrementIndex1() {
    index1++;
}
private void incrementIndex2() {
    index2++;
}
private void incrementIndex3() {
    index3++;
}
private void incrementIndex4() {
    index4++;
}

1 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试按下do..while按钮时更新标签score1_0, score2_0等。

如果我的理解是正确的,那么每次索引更新时都需要调用标签文本设置器,就像更新p4时,每次index4更新时都调用score4_0.setText("" + index4 + "");一样,我认为您不需要按钮处理程序中的index4