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

时间:2015-04-21 00:26:17

标签: java javafx javafx-2

基本上我想要做的是从ArrayAddingScreen类的addingButton中的InitialScreen类中的nextStageButton引用myHashTable。我在我的代码中包含了注释,试图更好地解释自己。我真的试图通过这项任务超越顶级,所以如果你看到其他任何我做错了或者我可以做得更好,请提供我的见解,我将非常感激。如果有人感兴趣的话,分配就是制作一个哈希表并在其上存储客户端

编辑:为了相关起见修剪了一些东西

UI屏幕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);
    }

}

UI屏幕2

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 ArrayAddingScreen extends Application {
   //Same as before trimmed for relevance
        addingButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent t)
            {
               String nameString = nameInput.getText();
               String cityString = cityInput.getText();               
               Client myClient = new Client(nameString,cityString);
               //I need to access myHashTable here

            }

        });
    }

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

}

客户端类

package hashtableinsert;
/**
 * Write a description of class Client here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Client
{
    private String name;
    private String city;

    public Client(String name, String city)
    {
        this.name = name;
        this.city = city;
    }

    public String toString()
    {
        return name + " " + city;
    }

    public String getName()
    {
        return name;
    }

    public String getCity()
    {
        return city;
    }
}

Hashtable类

package hashtableinsert;     / **      *在这里写一个HashTable类的描述。      *      * @author(你的名字)      * @version(版本号或日期)      * /     公共类Hashtable {

    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;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您需要访问新实例化的类中的某些内容,则将其在构造函​​数中或作为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;
}