JavaFX:更改以编程方式创建的按钮的值的方法

问题描述 投票:0回答:1

我正在尝试更改在for循环中创建的按钮的值。按钮的值必须保存在一个哈希图中,该哈希图包含按钮的ID和值。

这是我目前拥有的:

private void createMap(int blocksX, int blocksY) {
        // blocksX and blocksY are the amount of buttons to be placed
        for (int x = 0; x < blocksX; x++) {
            for (int y = 0; y < blocksY; y++) {
                Button btn = new Button();
                btn.setText("0");
                btn.setPrefSize(32, 32);
                btn.setLayoutX(32 * x);
                btn.setLayoutY(32 * y);
                btn.setId(String.valueOf(button_id));
                map_list.put(button_id, 0);
                button_id+=1;
                items.getChildren().addAll(btn);
                // If the user clicks a button, change the value of it...
                btn.setOnAction(click -> {
                    if(btn.getText() == "0"){
                        changeButtonValue(Integer.parseInt(btn.getId()), 1);
                        btn.setText("1");
                    } else if(btn.getText() == "1") {
                        changeButtonValue(Integer.parseInt(btn.getId()), 0);
                        btn.setText("0");
                    }
                });
            }
        }
    }

但是现在HashMap中唯一要更新的项目是最后创建的按钮。如何更改此设置,以便更新所有按钮的值?

java javafx javafx-8 fxml
1个回答
0
投票

我完成了一个可运行示例的代码。而且我看不到你的问题。

import java.util.HashMap;
import java.util.Map;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class FX01 extends Application {
    public int button_id = 0;
    public Map<Integer, Integer> map_list = new HashMap<>();
    public AnchorPane items;

    public static void main(String[] args) {
        launch(args);

    }

    @Override
    public void start(Stage stage) throws Exception {
        items = new AnchorPane();
        createMap(5, 5);

        Scene scene = new Scene(items);
        stage.setScene(scene);

        stage.show();
    }


    private void createMap(int blocksX, int blocksY) {
        // blocksX and blocksY are the amount of buttons to be placed
        for (int x = 0; x < blocksX; x++) {
            for (int y = 0; y < blocksY; y++) {
                Button btn = new Button();
                btn.setText("0");
                btn.setPrefSize(32, 32);
                btn.setLayoutX(32 * x);
                btn.setLayoutY(32 * y);
                btn.setId(String.valueOf(button_id));
                map_list.put(button_id, 0);
                button_id+=1;
                items.getChildren().addAll(btn);
                // If the user clicks a button, change the value of it...
                btn.setOnAction(click -> {
                    if(btn.getText() == "0"){
                        changeButtonValue(Integer.parseInt(btn.getId()), 1);
                        btn.setText("1");
                    } else if(btn.getText() == "1") {
                        changeButtonValue(Integer.parseInt(btn.getId()), 0);
                        btn.setText("0");
                    }
                });
            }
        }
    }

    private void changeButtonValue(int id, int value) {
        map_list.put(id, value);
        System.out.println("map_list: " + map_list);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.