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

huangapple go评论74阅读模式
英文:

JavaFX: Way to change value of programmatically created buttons

问题

我试图改变在一个for循环中创建的按钮的值。按钮的值必须保存在一个包含按钮ID和值的哈希映射中。

这是我目前拥有的代码:

private void createMap(int blocksX, int blocksY) {
    // blocksX和blocksY是要放置的按钮数量
    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);
            // 如果用户点击按钮,更改其值...
            btn.setOnAction(click -> {
                if (btn.getText().equals("0")) {
                    changeButtonValue(Integer.parseInt(btn.getId()), 1);
                    btn.setText("1");
                } else if (btn.getText().equals("1")) {
                    changeButtonValue(Integer.parseInt(btn.getId()), 0);
                    btn.setText("0");
                }
            });
        }
    }
}

但现在只有哈希映射中的最后创建的按钮会被更新。如何更改以便更新所有按钮的值?

英文:

I'm trying to change the value of buttons which are created in a for loop. The value of the buttons must be saved in a hashmap which contains the id of the button and the value.

This is what I currently have:

private void createMap(int blocksX, int blocksY) {
        // blocksX and blocksY are the amount of buttons to be placed
        for (int x = 0; x &lt; blocksX; x++) {
            for (int y = 0; y &lt; blocksY; y++) {
                Button btn = new Button();
                btn.setText(&quot;0&quot;);
                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 -&gt; {
                    if(btn.getText() == &quot;0&quot;){
                        changeButtonValue(Integer.parseInt(btn.getId()), 1);
                        btn.setText(&quot;1&quot;);
                    } else if(btn.getText() == &quot;1&quot;) {
                        changeButtonValue(Integer.parseInt(btn.getId()), 0);
                        btn.setText(&quot;0&quot;);
                    }
                });
            }
        }
    }

But now the only item in the HashMap to be updated is the last created button. How can I change this so it will update all button values?

答案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 和 blocksY 是要放置的按钮数量
        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);
                // 如果用户点击了按钮,改变其值...
                btn.setOnAction(click -> {
                    if (btn.getText().equals("0")) {
                        changeButtonValue(Integer.parseInt(btn.getId()), 1);
                        btn.setText("1");
                    } else if (btn.getText().equals("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);
    }
}
英文:

I completed the code to a runnable example. And I can't see your problem.

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&lt;Integer, Integer&gt; map_list = new HashMap&lt;&gt;();
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 &lt; blocksX; x++) {
for (int y = 0; y &lt; blocksY; y++) {
Button btn = new Button();
btn.setText(&quot;0&quot;);
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 -&gt; {
if(btn.getText() == &quot;0&quot;){
changeButtonValue(Integer.parseInt(btn.getId()), 1);
btn.setText(&quot;1&quot;);
} else if(btn.getText() == &quot;1&quot;) {
changeButtonValue(Integer.parseInt(btn.getId()), 0);
btn.setText(&quot;0&quot;);
}
});
}
}
}
private void changeButtonValue(int id, int value) {
map_list.put(id, value);
System.out.println(&quot;map_list: &quot; + map_list);
}
}

huangapple
  • 本文由 发表于 2020年4月6日 23:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61063665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定