簡體   English   中英

無法使用 HashMap 設置圖像

[英]Can't set image using HashMap

該程序提示用戶輸入一個州,然后顯示它的首都和該州旗的圖片。 除了顯示州旗的圖片外,該程序還可以工作。 我在 eclipse 的程序包下的一個文件夾中擁有所有圖像。 我無法使用 HashMap 顯示圖像。 這是代碼:

編輯:我認為它不起作用,因為它位於文本區域中,當我附加文本時,它必須是一個字符串,因此只有文本中的圖像文件名而不是圖像出現。 有沒有辦法將圖像添加到文本字段?

import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;

public class Maps extends Application {
    //Create text box, pane and submit button for the javafx application.
    TextArea answer_result = new TextArea();
    Button submit_button = new Button ("Submit");
    TextField answer_box = new TextField ();
    Map<String, String> linkedHashMap =  new HashMap<String, String>();
    Map<String, Image> HashMap =  new HashMap<String, Image>();

    //Call the stage.
    @Override
    public void start(Stage primaryStage){      
        ImageView us = new ImageView(new Image("state-flags.gif"));
        ImageView state = new ImageView();
        Label lb1 = new Label("Enter State Name: ", us);
        lb1.setContentDisplay(ContentDisplay.BOTTOM);
        lb1.setTextFill(Color.BLACK);   
        answer_result.setEditable(false);

        VBox v_box = new VBox();
        v_box.setAlignment(Pos.CENTER);
        v_box.getChildren().addAll(answer_result, state);

    //Create an HBox for the results and configure the size.
    HBox h_box = new HBox();
    h_box.setAlignment(Pos.CENTER);
    //Put the text box, button, and pane to the HBox.
    h_box.getChildren().addAll(lb1, answer_box, submit_button);

    BorderPane pane2 = new BorderPane(); //Create pane.
    pane2.setTop(h_box); //Set hbox to top of pane.
    pane2.setCenter(v_box); //Set text area to center.

    //Create scene and configure the size.
    Scene scene = new Scene (pane2, 600, 300);
    primaryStage.setScene(scene);
    primaryStage.show();   

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

       // State Flag Images
       HashMap.put("Alabama", new Image("alabama.png"));
       HashMap.put("Alaska", new Image("alaska.png"));
       HashMap.put("Arizona", new Image("arizona.png"));
       HashMap.put("Arkansas", new Image("arkansas.png"));
       HashMap.put("California", new Image("california.png"));
       HashMap.put("Colorado", new Image("colorado.png"));
       HashMap.put("Connecticut", new Image("connecticut.png"));
       HashMap.put("Delaware", new Image("delaware.png"));
       HashMap.put("Florida", new Image("florida.png"));
       HashMap.put("Georgia", new Image("georgia.png"));
       HashMap.put("Hawaii", new Image("hawaii.png"));
       HashMap.put("Idaho", new Image("idaho.png"));
       HashMap.put("Illinois", new Image("illinois.png"));
       HashMap.put("Indiana", new Image("indiana.png"));
       HashMap.put("Iowa", new Image("iowa.png"));
       HashMap.put("Kansas", new Image("kansas.png"));
       HashMap.put("Kentucky", new Image("kentucky.png"));
       HashMap.put("Louisiana", new Image("louisiana.png"));
       HashMap.put("Maine", new Image("maine.png"));
       HashMap.put("Maryland", new Image("maryland.png"));
       HashMap.put("Massachusetts", new Image("massachusetts.png"));
       HashMap.put("Michigan", new Image("michigan.png"));
       HashMap.put("Minnesota", new Image("minnesota.png"));
       HashMap.put("Mississippi", new Image("mississippi.png"));
       HashMap.put("Missouri", new Image("missouri.png"));
       HashMap.put("Montana", new Image("montana.png"));
       HashMap.put("Nebraska", new Image("nebraska.png"));
       HashMap.put("Nevada", new Image("nevada.png"));
       HashMap.put("New Hampshire", new Image("new-hampshire.png"));
       HashMap.put("New Jersey", new Image("new-jersey.png"));
       HashMap.put("New Mexico", new Image("new-mexico.png"));
       HashMap.put("New York", new Image("new-york.png"));
       HashMap.put("North Carolina", new Image("north-carolina.png"));
       HashMap.put("North Dakota", new Image("north-dakota.png"));
       HashMap.put("Ohio", new Image("ohio.png"));
       HashMap.put("Oklahoma", new Image("oklahoma.png"));
       HashMap.put("Oregon", new Image("oregon.png"));
       HashMap.put("Pennsylvania", new Image("pennsylvania.png"));
       HashMap.put("Rhode Island", new Image("rhode-island.png"));
       HashMap.put("South Carolina", new Image("south-carolina.png"));
       HashMap.put("South Dakota", new Image("south-dakota.png"));
       HashMap.put("Tennessee", new Image("tennessee.png"));
       HashMap.put("Texas", new Image("texas.png"));
       HashMap.put("Utah", new Image("utah.png"));
       HashMap.put("Vermont", new Image("vermont.png"));
       HashMap.put("Virginia", new Image("virginia.png"));
       HashMap.put("Washington", new Image("washington.png"));
       HashMap.put("West Virginia", new Image("west-virginia.png"));
       HashMap.put("Wisconsin", new Image("wisconsin.png"));
       HashMap.put("Wyoming", new Image("wyoming.png"));

    //Create the submit button.

    submit_button.setOnAction(e -> {     
        states_capitals(); 
        });     
    }

    private void states_capitals() {    
         String index = answer_box.getText();

         answer_result.appendText("The capital is " + linkedHashMap.get(index) + "\n");
        } 

//Call the main function.
public static void main(String[] args) {
    launch(args);
}
}

看看你的states_capitals()方法。 要顯示您正在使用的大寫名稱,請使用以下語句:

answer_result.appendText("The capital is " + linkedHashMap.get(index) + "\n");

但是你用什么來展示你的形象? 我使ImageView state = new ImageView(); 全局作為您的answer_result變量。 然后將此語句添加到您的states_capitals()方法中,現在它正在工作。

state.setImage(HashMap.get(index));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM