2017-03-02 40 views
0

SceneBuilderと3つのボタンと2つのImageViewで小さなFXMLファイルを作成しました。SceneBuilderで画像を表示

は、私は何をしたいです:NEXTボタン、ディスプレイ2枚の他の画像を押すと

  1. が開始
  2. でアプリとディスプレイ2枚の画像を実行します。

私の問題は、イメージをスウィージングするのではなく、シーンビルダによって作成されたImageViewとして表示することです。

ここに私のコントローラクラスです:

public class Controller { 
    private Button Next; //1st button 
    private Button J2inc; //2nd button 
    private Button J1inc; /3rd button 
    private ImageView Img1; 
    private ImageView Img2; 

    void Inc2(ActionEvent event) { 
     //nothing for the moment 
    } 
    void Inc1(ActionEvent event) { 
     //nothing for the moment 
    } 
    void Nextimg(ActionEvent event) { 
     //nothing for the moment 
    } 
} 

そして、私のstart方法:

public void start(Stage primaryStage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("Css.fxml")); 
    Scene scene = new Scene(root); 
    primaryStage.setScene(scene); 
    primaryStage.setTitle("P "); 
    primaryStage.show(); 
} 

私はImageView img1を初期化する方法がわからないので、それが何かをロードします。

は私だけImageViewの行を追加しますので、ここではFXMLを追加できませんでした:

<ImageView fx:id="Img1" fitHeight="750.0" fitWidth="450.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="450.0" AnchorPane.topAnchor="25.0" /> 
+0

「FXMLを追加できませんでした」とは何ですか?コードの書式設定方法を知りたい場合は、[this](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)を読んでください。また、[適切な命名規則](https://en.wikipedia.org/wiki/Naming_convention_(programming)#Java)を使用すると、人々があなたのJavaコードを読むのに役立つことに注意してください(正しく強調表示されます)。 –

+0

はい、それは私が意味していた、私は再びそれを読むでしょう。 – Bouji

答えて

1

、コントローラでそれを初期化し、それを@FXMLに注釈を付けることで、変数にアクセスできるよう、そしてコントローラのinitialize()でそれを初期化するには方法:

public class Controller { 
    private Button Next; //1st button 
    private Button J2inc; //2nd button 
    private Button J1inc; /3rd button 

    @FXML 
    private ImageView Img1; 

    private ImageView Img2; 

    public void initialize() { 
     Img1.setImage(new Image(...)); 
    } 

    void Inc2(ActionEvent event) { 
     //nothing for the moment 
    } 

    void Inc1(ActionEvent event) { 
     //nothing for the moment 
    } 

    void Nextimg(ActionEvent event) { 
     //nothing for the moment 
    } 
} 

あなたはFXMLで直接それを初期化する場合は、image属性の値を提供します。シーンビルダではImageViewを選択し、右上の([プロパティ]の下にある)[画像]フィールドに画像のURLを入力します。 URLの文字列表現の解釈方法については、Image documentationを必ずお読みください。

+0

それは動作します、ありがとう。 問題は解決されました。 – Bouji

+0

@Bouji問題を解決するには、正しい答えを確認する必要があります。 – Sedrick