2016-10-20 6 views
0

私は3つのランダムな象形文字画像を画面に表示するプログラムを持っています。私は象形文字のイメージをリフレッシュするために "リフレッシュ"ボタンを追加しました。ボタンをクリックすると、画像がリフレッシュされ、適切にランダム化されます。ボタンが最初にクリックされた後、それは消えます。私はそれが私のpane.getChildren().clear();行と何か関係があることはほとんど確信していますが、私はそれを把握しているようです。任意のヒントやアドバイス?クリック1回後にJavaFXリフレッシュボタンが消える

私が間違って投稿した場合、または適切なガイドラインを使用しないと、私はお詫び申し上げます。これは私の最初の投稿の一つです。

import javafx.application.Application; 
    import javafx.stage.Stage; 
    import javafx.scene.Scene; 
    import javafx.scene.image.ImageView; 
    import javafx.scene.control.Button; 
    import javafx.geometry.Pos; 
    import javafx.scene.layout.HBox; 

    public class Lab6a extends Application { 

@Override 
public void start(Stage myStage) { 

    //Create an HBox layout. 
    HBox hBox1 = new HBox(); 

    //Set alignment. 
    hBox1.setAlignment(Pos.CENTER); 
    getRandomHieroglyphic(hBox1); 

    //Create a Refresh button. 
    Button refresh = new Button("Refresh"); 
    refresh.setOnAction(e -> getRandomHieroglyphic(hBox1)); 
    hBox1.getChildren().add(refresh); 

    //Set the title for the second window. 
    myStage.setTitle("Random Hieroglyphics with Refresh"); 

    //Create a scene for the window. 
    Scene myScene = new Scene(hBox1, 400, 400); 

    //Place the scene in the second window. 
    myStage.setScene(myScene); 

    //Show the stage. 
    myStage.show(); 
} 

public void getRandomHieroglyphic(HBox pane) { 
    pane.getChildren().clear(); 

    //Create random generators to get a random image 
    int randomInt1 = (int) (Math.random() * 9) + 1; 
    int randomInt2 = (int) (Math.random() * 9) + 1; 
    int randomInt3 = (int) (Math.random() * 9) + 1; 

    //Create paths for the images to be called 
    String path1 = "Image/Hieroglyphics/h" + randomInt1 + ".png"; 
    String path2 = "Image/Hieroglyphics/h" + randomInt2 + ".png"; 
    String path3 = "Image/Hieroglyphics/h" + randomInt3 + ".png"; 

    //Add the images into the pane 
    pane.getChildren().add(new ImageView (path1)); 
    pane.getChildren().add(new ImageView (path2)); 
    pane.getChildren().add(new ImageView (path3));   
} 

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

そうです。 Hboxには最初に4人の子供がいて、3人の画像とボタンがあります。ボタンを 'getRandomHieroglyphic()'メソッドに追加するべきです。 –

+0

しかし、より良い方法は、ボタンを画像から分離することでした。これは、シーンに 'BorderPane'を追加し、' center'にのみ画像を、 'bottom'にボタンを置いて' HBox'を置くと良いでしょう。 –

答えて

0

あなたがおっしゃるとおり

は、ここに私のコードです。 Hboxには最初に4人の子供がいて、3人の画像とボタンがあります。 getRandomHieroglyphic()メソッドでボタンも追加する必要があります。

しかし、より良い方法は、ボタンを画像から分離することでした。これは、シーンにBorderPaneを追加して、HBoxを画像の中央に、下部のボタンのみに配置することで最適です。

@Override 
public void start(Stage myStage) { 
    HBox hBox1 = new HBox(); 
    hBox1.setAlignment(Pos.CENTER); 
    getRandomHieroglyphic(hBox1); 

    Button refresh = new Button("Refresh"); 
    refresh.setOnAction(e -> getRandomHieroglyphic(hBox1)); 
    BorderPane borderPane = new BorderPane(); 
    borderPane.getBottom().add(refresh); 

    myStage.setTitle("Random Hieroglyphics with Refresh"); 
    Scene myScene = new Scene(borderPane, 400, 400); 
1

clear()Button含むHBoxからすべての子を削除します。

あなたは3 ImageViewを得て、ImageViewの数を一定に保ちたいと思います。これは、それらを置き換えるべきではなく、その代わりにそれらが含まれているimageを置き換えることを意味します。さらに、画像を読み込んで9枚の画像をすべて読み込むのは避けてください。

public class Lab6a extends Application { 

    private Image[] images; 
    private final Random random = new Random(); 

    @Override 
    public void start(Stage myStage) { 
     // load all hieroglyphs 
     images = new Image[9]; 
     for (int i = 0; i < images.length; i++) { 
      images[i] = new Image("Image/Hieroglyphics/h" + (i+1) + ".png"); 
     } 

     // store all imageviews in array 
     final ImageView[] imageViews = Stream.generate(ImageView::new).limit(3).toArray(ImageView[]::new); 

     // set initial images 
     getRandomHieroglyphic(imageViews); 

     ... 

     hBox1.getChildren().add(refresh); 
     for (ImageView iv : imageViews) { 
      hBox1.getChildren().add(iv); 
     } 
     ... 
     refresh.setOnAction(e -> getRandomHieroglyphic(imageViews)); 
     ... 
    } 

    public void getRandomHieroglyphic(ImageView[] imageViews) { 
     for (ImageView iv : imageViews) { 
      iv.setImage(images[random.nextInt(images.length)]); 
     } 
    } 
+0

既存のものを編集するのではなく、基本的に同じ答えを与える方が良いアイデアですか? –

+1

この回答はあなたのものとは大きく異なります@TimothyTruckle。 –

関連する問題