2016-04-27 7 views
1

URLを含む別の気象ステーション(WeerStation)からのデータを含むXMLを読みました。あなたはコンボボックスからウェザーステーションを選択したとき、今、私は少し天気アプリのように自分のアプリケーションでそのURLを表示したいが、私はそれを成し遂げることができない... 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

どういう意味ですか?文字通り解釈すると、 '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です標準フォーマット(例えば、PNG)での画像、あなたはImageViewを宣言し、URLから作成Imageに画像を設定することができます:あなたは、「私は自分のアプリケーションでそのURLを見せたい」によって

@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

関連する問題