Java 调用我在另一个类的按钮中创建的类的实例化版本

Java 调用我在另一个类的按钮中创建的类的实例化版本,java,javafx,javafx-2,Java,Javafx,Javafx 2,基本上,我想做的是从ArrayAddingScreen类的addingButton中的InitialScreen类的nextStageButton引用myHashTable。我在代码中加入了注释,试图更好地解释自己。我真的在努力完成这项任务,所以如果你看到我做错了什么,或者我可以做得更好,请告诉我,我将不胜感激。任务是制作一个哈希表,如果有人感兴趣,将客户机存储在哈希表上 编辑:为了相关性,删掉了一些内容 用户界面屏幕1 package hashtableinsert; import java

基本上,我想做的是从ArrayAddingScreen类的addingButton中的InitialScreen类的nextStageButton引用myHashTable。我在代码中加入了注释,试图更好地解释自己。我真的在努力完成这项任务,所以如果你看到我做错了什么,或者我可以做得更好,请告诉我,我将不胜感激。任务是制作一个哈希表,如果有人感兴趣,将客户机存储在哈希表上

编辑:为了相关性,删掉了一些内容

用户界面屏幕1

package hashtableinsert;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Administrator
 */
public class InitialScreen extends Application {
//code went here but it wasn't relevant so I took it out
        nextStageButton.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t)
            {
                if(arraySize.getText().equals("")){
                    errorLabel.setVisible(true);
                }else{
                int passingToN = Integer.parseInt(arraySize.getText());
//I need to reference this in the adding button
                Hashtable myHashTable = new Hashtable(passingToN);
                errorLabel.setVisible(false);
                primaryStage.hide();
                arraySize.clear();

                nextStage = new ArrayAddingScreen(firstStage);

                }
            }               
            });         
    }

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

}
哈希表类

包装哈希表插入; /** *在这里写一个HashTable类的描述。 * *@author(你的名字) *@version(版本号或日期) */ 公共类哈希表{

    private int n;
    private Client[] table;

    public Hashtable(int n) {
        this.n = n;
        table = new Client[n];
    }

    public int hashFunction(String key) {
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        return sum;
    }

    public String search(String key) {       
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }

        if (key.equals(table[sum])) {
            return table[sum].toString();
        } else if (table[sum] == null) {
            return null;
        } else {
            while (table[sum] != null) {
                sum++;
            }
        }
           return table[sum].toString();
    }

    public boolean insert(Client myClient) {
        int counter = 0;
        String temp = myClient.getName();
        boolean ret = false;
        int tempSum = 0;
        for (int i = 0; i < temp.length(); i++) {
            tempSum += (int) temp.charAt(i);
        }

        if (table[tempSum] == null) {
            table[tempSum] = myClient;
            ret = true;
        } else {
            while (table[tempSum] != null) {
                if(tempSum == table.length){
                    tempSum = -1;
                }
                tempSum++;
                counter++;
            }
            if(counter != n){
             ret = true;  
             table[tempSum] = myClient;
            }
        } 
        return ret;
    }
}
privateint;
私有客户端[]表;
公共哈希表(int n){
这个,n=n;
表=新客户机[n];
}
公共整数哈希函数(字符串键){
整数和=0;
对于(int i=0;i
如果需要访问新实例化的类中的某些内容,请在其构造函数中或作为setter方法将其传递给该类

Hashtable myHashTable = new Hashtable(passingToN);
.....
// ArrayAddingScreen needs to read/write myHashTable so we will pass it
nextStage = new ArrayAddingScreen(firstStage, myHashTable);

// OR
// create a setter method in ArrayAddingScreen class

public void setMyhashTable (Hashtable myHashTable) {
   this.myHashTable = myHashTable;
}

抱歉,扎克,这里的代码太多了。尽量缩小你想问的内容,只显示相关代码。根据你的要求,我删掉了一堆
    private int n;
    private Client[] table;

    public Hashtable(int n) {
        this.n = n;
        table = new Client[n];
    }

    public int hashFunction(String key) {
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        return sum;
    }

    public String search(String key) {       
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }

        if (key.equals(table[sum])) {
            return table[sum].toString();
        } else if (table[sum] == null) {
            return null;
        } else {
            while (table[sum] != null) {
                sum++;
            }
        }
           return table[sum].toString();
    }

    public boolean insert(Client myClient) {
        int counter = 0;
        String temp = myClient.getName();
        boolean ret = false;
        int tempSum = 0;
        for (int i = 0; i < temp.length(); i++) {
            tempSum += (int) temp.charAt(i);
        }

        if (table[tempSum] == null) {
            table[tempSum] = myClient;
            ret = true;
        } else {
            while (table[tempSum] != null) {
                if(tempSum == table.length){
                    tempSum = -1;
                }
                tempSum++;
                counter++;
            }
            if(counter != n){
             ret = true;  
             table[tempSum] = myClient;
            }
        } 
        return ret;
    }
}
Hashtable myHashTable = new Hashtable(passingToN);
.....
// ArrayAddingScreen needs to read/write myHashTable so we will pass it
nextStage = new ArrayAddingScreen(firstStage, myHashTable);

// OR
// create a setter method in ArrayAddingScreen class

public void setMyhashTable (Hashtable myHashTable) {
   this.myHashTable = myHashTable;
}