【问题标题】:Listen to changes in array监听数组的变化
【发布时间】:2018-12-15 08:20:20
【问题描述】:

如果您能抽出几分钟时间,我需要一些帮助。 当我尝试完成这项工作时,我有点担心。

我有一个这样的 javaFX 类

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FoorballTeam extends Application {
int i1=0;  
int i3=0;
String[] PlayerNames = new String[12];
int[] goals = new int[12];

@Override
public void start(Stage primaryStage) {

    player[] playerData = new player[12];

    Button btn = new Button();
    btn.setText("add Player");

    GridPane root = new GridPane();
    root.add(btn,0,0);

    int i2;
    for (i2=0;i2<=11;i2++)
    {playerData[i2]=new player();}        

    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {

           playerData[i3].player(root, i3);
           i3++; 

        }
    });

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public String[] getPlayerNames() {
    return PlayerNames;
}

public void setPlayerNames(String[] PlayerNames) {
    this.PlayerNames = PlayerNames;
}

public int[] getGoals() {
    return goals;
}

public void setGoals(int[] goals) {
    this.goals = goals;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

还有一个像这样命名为 player 的第二类

import javafx.event.EventType;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;

public class player {

String NameOfPlayer = new String();    
int goalsOfPlayer;

public void player (GridPane root,int numberOfPlayer) 
{

TextField name = new TextField();
TextField goals = new TextField();

GridPane grid = new GridPane();
grid.add(name,0,0);
grid.add(goals,1,0);
root.add(grid,0,numberOfPlayer+1);

System.out.println("player " + numberOfPlayer + " added");

name.textProperty().addListener((observable, oldValue, newValue) -> {
NameOfPlayer=newValue;

}); 

goals.textProperty().addListener((observable, oldValue, newValue) -> {
goalsOfPlayer=Integer.parseInt(newValue);
});    


}
}

我希望每次更改球员姓名或目标时都将更改传递到主类的两个数组 PlayerNames[] 和目标 [] 上。
例如,如果 player1 将目标从 1 更改为 2,我希望目标 [1]=2。 也可以在这两个数组中放置一个监听器,以便当玩家更改名称或目标时触发监听器。
任何帮助将不胜感激。

【问题讨论】:

  • 为使您的代码易于阅读,请将其缩进并关注Java Naming Conventions
  • 我认为,与其保持两个数组的更新,不如使用一个简单的函数来在需要时检索所有更新的值,例如 int[] getGoals()
  • 我找到了第一个问题的解决方案,但我相信没有办法向变量添加更改侦听器。
  • goals.textProperty().addListener... 不是给变量添加监听器吗?
  • 谢谢朋友的回答,我希望在FooballTeam类中变量int[] targets = new int[12] 有一个监听器,是否可以;

标签: java arrays javafx observable


【解决方案1】:

一个简单的解决方案是使用可观察列表扭曲 int[] 数组,并监听该列表中的变化:

import java.util.Arrays;
import java.util.Random;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class ListenToArrayCahnges extends Application {

    private final int SIZE = 12;
    private final Integer[] goals = new Integer[SIZE];
    private final Random rand = new Random();
    private int counter = 0;

    @Override
    public void start(Stage primaryStage) {

        Arrays.fill(goals, 0); //initial values
        //Warp array with an observable list. list back by array so it is of fixed length
        ObservableList<Integer> goalsList = FXCollections.observableArrayList(Arrays.asList(goals));
        //add listener to list
        goalsList.addListener((ListChangeListener<Integer>) c ->{
            //respond to list changes
            System.out.println("Goals changed to : "+ goalsList);
        });
        //button to change the list
        Button btn = new Button();
        btn.setText("Add goal");    
        btn.setOnAction(event -> {
            goalsList.set(counter,  rand.nextInt(100));
            counter = ++counter % SIZE ; //increment counter 0,1,2....11 and back to 0
        });

        GridPane root = new GridPane();
        root.add(btn,0,0);

        Scene scene = new Scene(root, 150, 50);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

请注意,张贴的mcve 代表需要解决的问题(或本例中的解决方案),不是具体的应用或用途案子。

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多