【问题标题】:JavaFX Refresh button disappears after 1 clickJavaFX Refresh 按钮在单击 1 后消失
【发布时间】:2017-03-03 00:35:08
【问题描述】:

我有一个程序,它在屏幕上显示 3 个随机的象形文字图像。我添加了一个“刷新”按钮以刷新象形文字图像。当我单击按钮时,图像会刷新并正确随机化。但是,在第一次单击该按钮后,它就会消失。我几乎可以肯定它与我的pane.getChildren().clear(); 行有关,但我似乎无法弄清楚。有什么提示或建议吗?

如果我发布不正确或没有使用正确的指南,我深表歉意。这是我的第一篇文章。

这是我的代码:

    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);
 }  
}

【问题讨论】:

  • 你是对的。 You Hbox最初有4个孩子,3个图像和按钮。您也应该在 getRandomHieroglyphic() 方法中添加按钮。
  • 但更好的方法是将按钮与图像分开。最好的方法是在场景中添加一个BorderPane,并将带有图像的HBox 放在center 中,并将按钮放在bottom 区域中。

标签: java user-interface button javafx


【解决方案1】:

clear()HBox 中删除所有子代,包括Button

您有 3 个 ImageViews 并希望保持 ImageViews 的数量不变。这意味着您不应该替换它们,而是替换它们包含的images。此外,您应该避免重新加载图像并在开始时加载所有 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)]);
        }
    }

【讨论】:

  • 给出基本相同的答案而不是编辑现有的答案更好?
  • 这个答案与你@TimothyTruckle 的答案大不相同。
【解决方案2】:

你是对的。 You 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多