如何使用输入和处理HashMap来获取JavaFX的输出

时间:2016-04-26 22:19:52

标签: javafx

package application;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;


public class Main extends Application {

    TextField stateName = new TextField();

    @Override
    public void start(Stage primaryStage) { 

        //set up pane and stage
        GridPane pane = new GridPane();
        primaryStage.setTitle("State Capitals and Flags");
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(10);
        pane.setVgap(10);
        pane.setPadding(new Insets(25, 25, 25, 25));
        Scene scene = new Scene(pane,400,400);

        Text sceneTitle = new Text("State Capital and their Flags");
        pane.add(sceneTitle, 0, 0, 2, 1);
        Label stateLabel = new Label ("State:");
        pane.add(stateLabel, 0, 1);
        TextField stateName = new TextField();
        pane.add(stateName, 0, 2);

        Button submit = new Button("Submit");
        HBox input = new HBox(10);
        input.setAlignment(Pos.BOTTOM_RIGHT);
        input.getChildren().add(submit);
        pane.add(input, 1, 4);

        final Text stateCapital = new Text();
        pane.add(stateCapital, 0, 6);

        //set action for submit button
        submit.setOnAction(e -> getCapitals()
                );

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public String getCapitals() {
        //String getState = "State Name is: ";
        String getState = stateName.getText();
        Map<String, String> mapStateCapitals = new HashMap<>(50);
        for (String[] stateCapital : stateCapitals) {
            mapStateCapitals.put(stateCapital[0], stateCapital[1]);
        }
        mapStateCapitals.forEach((state, city) -> {
            if(getState.equalsIgnoreCase(state)){
                System.out.println(city);
            }
        });
        return getState;
    }

    private static String[][] stateCapitals = {
            {"Alabama", "Montgomery"},
            {"Alaska", "Juneau"},
            {"Arizona", "Phoenix"},
            {"Arkansas", "Little Rock"},
            {"California", "Sacramento"},
            {"Colorado", "Denver"},
            {"Connecticut", "Hartford"},
            {"Delaware", "Dover"},
            {"Florida", "Tallahassee"},
            {"Georgia", "Atlanta"},
            {"Hawaii", "Honolulu"},
            {"Idaho", "Boise"},
            {"Illinois", "Springfield"},
            {"Maryland", "Annapolis"},
            {"Minnesota", "Saint Paul"},
            {"Iowa", "Des Moines"},
            {"Maine", "Augusta"},
            {"Kentucky", "Frankfort"},
            {"Indiana", "Indianapolis"},
            {"Kansas", "Topeka"},
            {"Louisiana", "Baton Rouge"},
            {"Oregon", "Salem"},
            {"Oklahoma", "Oklahoma City"},
            {"Ohio", "Columbus"},
            {"North Dakota", "Bismark"},
            {"New York", "Albany"},
            {"New Mexico", "Santa Fe"},
            {"New Jersey", "Trenton"},
            {"New Hampshire", "Concord"},
            {"Nevada", "Carson City"},
            {"Nebraska", "Lincoln"},
            {"Montana", "Helena"},
            {"North Carolina", "Raleigh"},
            {"Missouri", "Jefferson City"},
            {"Mississippi", "Jackson"},
            {"Massachusetts", "Boston"},
            {"Michigan", "Lansing"},
            {"Pennsylvania", "Harrisburg"},
            {"Rhode Island", "Providence"},
            {"South Carolina", "Columbia"},
            {"South Dakota", "Pierre"},
            {"Tennessee", "Nashville"},
            {"Texas", "Austin"},
            {"Utah", "Salt Lake City"},
            {"Vermont", "Montpelier"},
            {"Virginia", "Richmond"},
            {"Washington", "Olympia"},
            {"West Virginia", "Charleston"},
            {"Wisconsin", "Madison"},
            {"Wyoming", "Cheyenne"}
    };  

    public static void main(String[] args) {
        launch(args);
    }
}

我正在尝试使用州名的输入来显示州首府。我还必须显示状态标志,但一次只能显示一个问题......

我尝试了许多不同的方法来编码信息无济于事。任何帮助或推动正确的方向将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

在检索State Capitals之前,初始化一个Map的全局变量。在你的代码中,你正确地说,你只需要一个方法来获取值。有一个函数getCapital,它将State的名称作为参数,并使用函数get检索值。

static Map <String, String> map = new HashMap<String, String>();

public static void loadHashMap(){
    for(String [] str : stateCapitals){
        map.put(str[0], str[1]);
    }
}

public static String getCapital(String state){
    return map.get(state);
}

当然,您的state变量来自您的TextField,因此您需要致电getCapital (stateName.getText())或类似。