从列表中更改标签的值。

时间:2017-11-27 21:45:25

标签: list javafx binding

我正在尝试制作一个猜谜游戏,程序将一次显示一个序列的序列。我有从列表中随机生成的序列,但我无法一次显示列表中的值。我想用bind更改标签,但我似乎无法使其工作。

public class Tutorial extends Application {

    Stage window; 
    Scene scene1, scene2; 
    Label label; 
    public static void main(String[] args) {
        launch(args);

    }

    public void start(Stage primaryStage) throws Exception {
        window = primaryStage; 

        //Scene 1
        Label welcome = new Label("Welcome to the Guessing Game. Press Start to Enter");
        Button start =  new Button("Start"); 
        VBox firstlayout = new VBox(10); 
        firstlayout.getChildren().addAll(welcome, start); 
        firstlayout.setAlignment(Pos.CENTER);

        start.setOnAction(e -> window.setScene(scene2));

        scene1 = new Scene(firstlayout, 450, 300); 
        window.setScene(scene1);


            ArrayList<Integer> guessList = numbergen(); 




         Label label = new Label(Integer.toString(getValues(guessList)));


            label.setStyle("-fx-text-fill: red; -fx-padding: 10px; -fx-font-size: 50px");

            FadeTransition fader = createFader(label);

            SequentialTransition Fader = new SequentialTransition(
                    label,
                    fader
            );


            StackPane layout = new StackPane(); 
            layout.getChildren().addAll(label); 


           scene2 = new Scene(layout, 250, 300); 

            Fader.play();



    window.setTitle("Program Close");
    window.setScene(scene1);

    window.show(); 

    }

private FadeTransition createFader(Node node) {
    FadeTransition fade = new FadeTransition(Duration.seconds(5), node);
    fade.setFromValue(1);
    fade.setToValue(0);

    return fade;
}

     public ArrayList<Integer> numbergen(){ 
            ArrayList<Integer> numb = new ArrayList<Integer>(); 
            Random rand = new Random();

            for(int i = 0; i<4; i++){

            int n = rand.nextInt(5) + 1; 
                numb.add(n); 
            }   
            return numb; 
        }

        public boolean result(ArrayList<Integer> solnList, ArrayList<Integer> guessList){ 

            if(solnList.size() != guessList.size()){ 
                return false; 
            }else{ 
                for(int i = 0; i<solnList.size(); i++){ 
                    if(solnList.get(i) != guessList.get(i)){ 
                        return false; 
                    }
                }
                return true; 
            }



        }

        public int getValues(ArrayList<Integer> list){ 
            for(int i = 0; i<list.size(); i++){ 

                return list.get(i); 
            }
            return 0; 
        }





} 

1 个答案:

答案 0 :(得分:2)

如果要绑定到整数列表元素,可以使用:

Bindings.integerValueAt(observableList, observableIndex)

例如:

label.textProperty().bind(
    Bindings.integerValueAt(observableList, observableIndex).asString()
);

其中IntegerProperty中的observableIndex引用列表中您希望绑定其值的元素的当前索引。

当您致电observableIndex.add(1)时,绑定值将会相应更改。