该程序提示用户输入状态,然后显示它的首都和该状态标志的图片。该程序除了显示状态标志的图片外,还可以工作。我把所有的图像放在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);
}
}
答案 0 :(得分:1)
查看您的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));