JavaFX: TextFields displayed from array, array updated / increased on button click – how to force the scene to redraw the updated array

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

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 &lt; 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 &lt; 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(&quot;Animals updated&quot;);
}

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(&quot;Animals updated&quot;);
}

huangapple
  • 本文由 发表于 2020年5月4日 18:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61590305.html
匿名

发表评论

匿名网友

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

确定