このプログラムは、状態を入力するようユーザーに求め、その状態とその状態フラグの画像を表示します。このプログラムは、状態フラグの画像を表示することを除いて動作します。私は、Eclipseのプログラムパッケージの下のフォルダにすべての画像を持っています。私はHashMapを使って画像を表示できません。ここにコードがあります: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);
}
}
ここで、状態フラグを更新しますか? – jns
私はこのコードではありませんが、これはエラーのない作業バージョンです。私はそれをHashMapに大文字で追加する方法を見つけようとしています。 – goblin
私はそれを考え出したかもしれません、分を教えてください。 – goblin