我正在尝试将URL图像添加到我的javafx应用程序中

时间:2016-04-27 11:12:06

标签: java user-interface url javafx combobox

我读过一张XML,其中包括来自不同天气(WeerStation)的数据,包括一个URL。现在当你从组合框中选择一个耐候性时,我想在我的应用程序中显示该URL,就像一个小天气应用程序,但我无法完成它...

public class WeatherGUI extends Application {
ComboBox combo;
ArrayList<WeerStation> list = new ArrayList();

@Override
public void start(Stage primaryStage) throws ParserConfigurationException, IOException, SAXException {

    BuienRadarController controller = new BuienRadarController(this);
    list = controller.getStations();
    combo = new ComboBox();
    combo.getItems().addAll(list);
    combo.setPromptText("Het weer in");

    VBox vbox = new VBox(8);
    vbox.setAlignment(Pos.CENTER);
    Label label1 = new Label();
    Label label2 = new Label();
    vbox.getChildren().addAll(label1,label2);

    Button button = new Button("Kies");
    button.setOnAction((ActionEvent e) -> {
        WeerStation station = (WeerStation) combo.getValue();
        label1.setText("Het weer in " + station.toString() + ":");
        label2.setText(station.getBeschrijving());
        // ADD url to vbox
    });

    VBox layout = new VBox(10);
    layout.setPadding(new Insets(6));
    layout.getChildren().addAll(combo,button,vbox);

    Scene scene = new Scene(layout, 300, 250);

    primaryStage.setTitle("BuienRadar");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}}

1 个答案:

答案 0 :(得分:0)

假设URL是一个URL,其内容是标准格式的图像(例如png),您可以声明ImageView并将图像设置为从URL创建的Image

@Override
public void start(Stage primaryStage) throws ParserConfigurationException, IOException, SAXException {

    BuienRadarController controller = new BuienRadarController(this);
    list = controller.getStations();
    combo = new ComboBox();
    combo.getItems().addAll(list);
    combo.setPromptText("Het weer in");

    VBox vbox = new VBox(8);
    vbox.setAlignment(Pos.CENTER);
    Label label1 = new Label();
    Label label2 = new Label();
    ImageView imageView = new ImageView();
    vbox.getChildren().addAll(label1,label2,imageView);

    Button button = new Button("Kies");
    button.setOnAction((ActionEvent e) -> {
        WeerStation station = (WeerStation) combo.getValue();
        label1.setText("Het weer in " + station.toString() + ":");
        label2.setText(station.getBeschrijving());
        // ADD url to vbox
        imageView.setImage(new Image(station.getURL().toExternalForm()));
    });

    VBox layout = new VBox(10);
    layout.setPadding(new Insets(6));
    layout.getChildren().addAll(combo,button,vbox);

    Scene scene = new Scene(layout, 300, 250);

    primaryStage.setTitle("BuienRadar");
    primaryStage.setScene(scene);
    primaryStage.show();
}