2016-04-26 88 views
0
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); 
    } 
} 

我想獲得狀態資本顯示使用狀態名稱的輸入。我也必須顯示狀態標誌,但一次只有一個問題...如何使用輸入和處理HashMap獲得輸出與JavaFX

我已經嘗試了許多不同的方式來編碼信息無濟於事。任何幫助或在正確的方向微調將不勝感激。謝謝

回答

0

有一個全局變量的地圖,你檢索州首府之前初始化。在你的代碼中,你正確地使用它,你只需要一個方法來獲取值。有一個函數getCapital,它以狀態的名稱作爲參數,並使用函數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變量來自於你的文本字段,因此需要調用getCapital (stateName.getText())或相似。