2017-03-24 9 views
1

私はJavaFXで新しく、これは私のコードです。このアプリケーションにはボタンがあります。ボタンをクリックすると、新しいフォームが開きます。誰もが新しいフォームに画像を挿入する方法を知っていますか?新しい形式で画像をインポートする方法

package javafxwindow; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.ActionEvent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

私が使用したパッケージ。 クラスJavaFXWindow。

public class JavaFXWindow extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     Button btn = new Button(); 
     btn.setText("Open a New Window"); 
     btn.setOnAction((ActionEvent event) -> { 
      Label secondLabel = new Label("Hello"); 

      StackPane secondaryLayout = new StackPane(); 
      secondaryLayout.getChildren().add(secondLabel); 

      Scene secondScene = new Scene(secondaryLayout, 200, 100); 

      Stage secondStage = new Stage(); 
      secondStage.setTitle("New Stage"); 
      secondStage.setScene(secondScene); 

      secondStage.show(); 

     }); 

     StackPane root = new StackPane(); 
     root.getChildren().add(btn); 

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

     primaryStage.setTitle("java-buddy.blogspot.com"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     primaryStage.setOnCloseRequest(e -> Platform.exit()); 

    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 

答えて

0

子ImageViewをsecondaryLayoutに追加するだけです。例えば;

ImageView imageViewTest = new ImageView(new Image("com/test/images/test.png")); 
secondaryLayout.getChildren().add(imageViewTest); 

ただし、test.pngの正しいパスを記述する必要があります。 srcパッケージの後に書かれます。

+0

4つのイメージを新しいフォームに追加するにはどうすればよいですか?ありがとう。 –

+0

"path"は誤解を招く恐れがあります。 *** URL ***にする必要があります! @somiludakあなた自身で何かを試してみてはいかがですか?警告:あなたはこれをやっている経験を得るかもしれません... – fabian

関連する問題