2016-04-27 62 views
1

我讀過一個XML,其中包含來自不同weatherstation(WeerStation)的數據,其中包括一個URL。現在,當你選擇從ComboBox一個氣象站我想表明我的應用程序像一個小的天氣應用程序,URL,但我不能把它做...我正在嘗試向我的javafx應用程序添加一個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); 
}} 
+0

你是什麼意思「我想表明,URL在我的應用程序」 ?從字面上解釋,這意味着您只需執行'someLabel.setText(station.getURL()。toString());'(或類似的)。但是,也許你的意思是你想顯示你從該URL獲得的實際HTML?在這種情況下,您可以使用['WebView'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/web/WebView.html)。 –

+0

或者如果它只是一個圖像的URL,您可以直接將它傳遞給['ImageView'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView .html#ImageView-java.lang.String - )... –

+0

好的,謝謝,但我如何將URL轉換爲圖像? – user4126670

回答

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(); 
} 
+0

它工作!謝謝 :) – user4126670

相關問題