2016-10-20 54 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',並將'HBox'的圖片放在'center'和'bottom'區域的按鈕中。 –

回答

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()將刪除HBox所有兒童,包括Button

你有3 ImageView s,並希望保持ImageView s的數量不變。這意味着您不應該替換它們,而是替換它們包含的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完全不同。 –