如何在gridpane Onclick事件中猜测索引

时间:2018-05-14 15:14:44

标签: java javafx gridpane

我有类似的东西:

...
gridPane.getChildren().forEach(item -> {
        item.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 1) {
                    // I NEED TO KNOW WHICH RAW, COLUMN CLICKED!!
                }
            }
        });

    });

我在网格窗格的每个元素中添加了Onclick事件,是否有任何选项可以获得点击的行和列的正确索引?

1 个答案:

答案 0 :(得分:-1)

ANWSER:

import java.util.Scanner;

public class MeanMedian {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    int i;
    int j;
    int k;
    int in_length;
    int mid_loc;
    int sum_arr;
    float median = 0.0f;
    float mean = 0.0f;
    float delta = 0.0f;
    float incremental_delta = 0.0f;
    float MEDIAN_FOR_MAX_DELTA = 0.0f;
    float MEAN_FOR_MAX_DELTA = 0.0f;
    float MAX_DELTA = -1.0f;
    int MAX_SEQ_LENGTH = 0;

    System.out.print("Enter the length of input: ");
    in_length = sc.nextInt(); 

    int in_arr[]= new int [in_length+1];
    int out_arr[] = new int [in_length+1]; //This is the maximum size of the output array.
    int MAX_DELTA_ARR[] = new int [in_length+1];


    // STAGE-1: Accept the input sequence    
    for (i = 1; i <= in_length; i++) {
     System.out.print("Enter the input #" + i + ": ");
     in_arr[i] = sc.nextInt();
    }
    // STAGE-1 completed.


    // STAGE-2: Sort the array (Bubble sort in Ascending order)
    for (j = 1; j < in_length; j++) {
        for (i = in_length; i > j; i--) {
            if (in_arr[i-1] > in_arr[i]) {
                k = in_arr[i];
                in_arr[i] = in_arr[i-1];
                in_arr[i-1] = k;
            }
        }
    }
    // STAGE-2 completed.


    // STAGE-3: Compute Max Delta
    MAX_DELTA = -99999; //Store as large -ve number as float data type can hold.

    for (i = in_length; i > 2; i--) {
        // STAGE-3a: Optional - Clear the out_arr[]
        for (j = 1; j <= in_length; j++) {
            out_arr [j] = 0;
        }
        // STAGE-3a completed.


        // STAGE-3b: Determine the index of the median for the sequence of length i
        if (i % 2 == 1) {
            mid_loc = (i + 1)/2;
        }
        else {
            mid_loc = (i / 2) + 1;
        }
        // STAGE-3b completed.


        // STAGE-3c: Create the selection that gives the min median and max mean.
        // STAGE-3c1: Create left side of mid point.
        for (j = mid_loc; j > 0; j--) {
            out_arr[j] = in_arr[j];
        }
        // STAGE-3c1 completed.


        // STAGE-3c2: Create right side of mid point.
        k = in_length;
        for (j = i; j > mid_loc; j--) {             
            out_arr[j] = in_arr[k];
            k = k - 1;
        }
        // STAGE-3c2 completed.


        // STAGE-3c3: Do the SHIFT TEST.
        //for (; k <=  mid_loc + in_length - i; k++) {
        for (k = mid_loc + 1; k <=  mid_loc + in_length - i; k++) {
            if (i % 2 == 1) {
                incremental_delta = ((float)in_arr[k] - (float)out_arr[1])/i - ((float)in_arr[k] - (float)out_arr[mid_loc]);
            }
            else {
                incremental_delta = ((float)in_arr[k] - (float)out_arr[1])/i - (((float)in_arr[k] - (float)out_arr[mid_loc]/2));
            }
            if (incremental_delta >= 0 ) {
                //Insert this new element
                for(j = 1; j < mid_loc; j++) {
                    out_arr[j] = out_arr[j+1];
                }
                out_arr[mid_loc] = in_arr[k];
            }
        }
        // STAGE-3c3 completed. 


        // STAGE-3d: Find the median of the present sequence.
        if(i % 2 == 1) {
            median = out_arr[mid_loc];
        }
        else {
            median = ((float)out_arr[mid_loc] + (float)out_arr[mid_loc - 1])/2;
        }
        // STAGE-3d completed. 


        // STAGE-3e: Find the mean of the present sequence.
        sum_arr = 0;
        for(j=1; j <= i ; j++) {
            sum_arr = sum_arr + out_arr[j];
        }
        mean = (float)sum_arr / i;
        // STAGE-3e completed. 


        // STAGE-3f: Find the delta for the present sequence and compare with previous MAX_DELTA. Store the result.
        delta = mean - median;

        if(delta > MAX_DELTA) {
            MAX_DELTA = delta;
            MEAN_FOR_MAX_DELTA = mean;
            MEDIAN_FOR_MAX_DELTA = median;
            MAX_SEQ_LENGTH = i;
            for (j = 1; j <= MAX_SEQ_LENGTH; j++) {
                MAX_DELTA_ARR[j] = out_arr[j];
            }
        }
        // STAGE-3f completed.  
    }


    // STAGE-4: Print the result.
    System.out.println("--- RESULT ---");
    System.out.print("The given input sequence is: ");
    System.out.print("{ ");
    for(i=1; i <= in_length; i++) {
        System.out.print(in_arr[i]);
        System.out.print(" ");
    }

    System.out.print("}");
    System.out.println("");
    System.out.print("The sequence with maximum difference between mean and median is: ");
    System.out.print("{ ");
    for(i=1; i <= MAX_SEQ_LENGTH; i++) {
        System.out.print(MAX_DELTA_ARR[i]);
        System.out.print(" ");
    }

    System.out.print("}");
    System.out.println("");
    System.out.println("The mean for this sequence is: " + MEAN_FOR_MAX_DELTA);
    System.out.println("The median for this sequence is: " + MEDIAN_FOR_MAX_DELTA);
    System.out.println("The maximum difference between mean and median for this sequence is: " + MAX_DELTA);

}

}