英文:
JavaFX: TextFields displayed from array, array updated / increased on button click - how to force the scene to redraw the updated array
问题
我有一个显示动物及其名称的程序:
private int TOTAL_ANIMALS = 5;
private TextField[] animals;
private TextField[] names;
public AnimalsApp() {
animals = new TextField[TOTAL_ANIMALS];
names = new TextField[TOTAL_ANIMALS];
for (int i = 0; i < TOTAL_ANIMALS; i++) {
animals[i] = new TextField();
names[i] = new TextField();
}
}
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
// ...
for (int i = 0; i < TOTAL_ANIMALS; i++) {
grid.add(animals[i], 0, i);
grid.add(names[i], 1, i);
}
与 "添加动物" 按钮关联的方法:
public void addAnimals() {
animals = Arrays.copyOf(animals, TOTAL_ANIMALS + 1);
names = Arrays.copyOf(names, TOTAL_ANIMALS + 1);
animals[TOTAL_ANIMALS] = new TextField();
names[TOTAL_ANIMALS] = new TextField();
TOTAL_ANIMALS++;
System.out.println("动物已更新");
}
addAnimals
内部的代码确实执行,但新的文本字段不会出现。我该如何修复它?
英文:
I have a program that displays animals and their names:
private int TOTAL_ANIMALS = 5;
private TextField[] animals;
private TextField[] names
public AnimalsApp {
animals = new TextField[TOTAL_ANIMALS];
names = new TextFields[TOTAL_ANIMALS];
for (int i = 0; i < TOTAL_ANIMALS; i++) {
animals[i] = new TextField();
names[i] = new TextField();
}
}
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
...
for (int i = 0; i < TOTAL_ANIMALS; i++) {
grid.add(animals[i], 0, i);
grid.add(names[i], 1, i);
}
The method associated with Add animal
button:
public void addAnimals() {
animals = Arrays.copyOf(animals, TOTAL_ANIMALS + 1);
names = Arrays.copyOf(names, TOTAL_ANIMALS + 1);
animals[TOTAL_ANIMALS] = new TextField();
names[TOTAL_ANIMALS] = new TextField();
TOTAL_ANIMALS++;
System.out.println("Animals updated");
}
The code inside addAnimals
does execute, but the new text field doesn't appear. How should I fix it?
答案1
得分: 2
你忘记将文本字段添加到网格中,仅创建一个文本字段是不会显示的,你需要像在 start
函数中那样将它添加到你的网格中。你还需要将 GridPane
声明为类中的一个变量。
public void addAnimals() {
animals = Arrays.copyOf(animals, TOTAL_ANIMALS + 1);
names = Arrays.copyOf(names, TOTAL_ANIMALS + 1);
animals[TOTAL_ANIMALS] = new TextField();
names[TOTAL_ANIMALS] = new TextField();
grid.add(animals[TOTAL_ANIMALS], 0, TOTAL_ANIMALS);
grid.add(names[TOTAL_ANIMALS], 1, TOTAL_ANIMALS);
TOTAL_ANIMALS++;
System.out.println("动物已更新");
}
英文:
You forgot to add the text fields to grid, just creating a textfield doesn't show it you need to add it to your like you did in your start function. You would also need to make the GridPane a variable in the class.
public void addAnimals() {
animals = Arrays.copyOf(animals, TOTAL_ANIMALS + 1);
names = Arrays.copyOf(names, TOTAL_ANIMALS + 1);
animals[TOTAL_ANIMALS] = new TextField();
names[TOTAL_ANIMALS] = new TextField();
grid.add(animals[TOTAL_ANIMALS], 0, TOTAL_ANIMALS);
grid.add(names[TOTAL_ANIMALS], 1, TOTAL_ANIMALS);
TOTAL_ANIMALS++;
System.out.println("Animals updated");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论