英文:
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 < 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");
}
});
}
}
}
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<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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论