【问题标题】:setOnAction is not triggered未触发 setOnAction
【发布时间】:2014-11-03 16:18:28
【问题描述】:

我需要在单击按钮时显示一些文本。这里按钮数组列表已添加到 vbox,vbox 到 gridpane 和 setOnAction 在 Platform.runLater() 方法中被调用,如图所示。 代码:

  private ArrayList<Button> btnar;
  private VBox vb;
  private Button downloadbtn;
@FXML
  private ScrollPane displayscroll;
   private GridPane gridpane;

 public HomeUI_2Controller() {
    Platform.runLater(new Runnable() {

        @Override
        public void run() {
         gridpane = new GridPane();
         displayscroll.setContent(gridpane);

         btnar = new ArrayList<>();
            for (int i = 0; i < filelist2.size(); i++) {
                downloadbtn = new Button("Download");
                btnar.add(downloadbtn);
            }
            int imageCol = 0;
            int imageRow = 0;

            for (int i = 0; i < filelist2.size(); i++) {
                System.out.println(filelist2.get(i).getName());

                image = new Image(filelist2.get(i).toURI().toString());

                pic = new ImageView();
                pic.setFitWidth(130);
                pic.setFitHeight(130);

                pic.setImage(image);
                vb = new VBox();
                vb.getChildren().addAll(pic, (Button) btnar.get(i));

                gridpane.add(vb, imageCol, imageRow);
                GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
                imageCol++;

                // To check if all the 3 images of a row are completed
                if (imageCol > 2) {
                    // Reset Column
                    imageCol = 0;
                    // Next Row
                    imageRow++;
                }

            }
             downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    System.out.println("sssss");
                }
            });

    }
    });

}

【问题讨论】:

  • 你为什么在Platform.runLater里面有setOnAction()
  • @ItachiUchiha 那么你有什么建议呢?
  • 你的方法缺陷太多。我无法了解您要达到的目标。您的 scrollPane 是从 FXML 访问的,而其他布局和控件则不是。在您的构造函数中,您有一个Platform.runLater。尝试创建MCVE 或编辑您的代码以及问题以避免混淆

标签: events button javafx


【解决方案1】:
  • 只有最后一个“下载”按钮应该被触发。因为您只是将 onAction 事件处理程序添加到最后一个。

  • 这里不需要Paltform.runLater()

  • 如果只能循环一次,则不应构造相同的多个循环。

  • 尽量保持变量的范围尽可能窄。换句话说,不要定义全局变量并在任何地方使用作为占位符。这使得代码更容易出错且难以维护。

测试这个重构的代码:

private ArrayList<Button> btnar;
private VBox vb;
@FXML
private ScrollPane displayscroll;
private GridPane gridpane;

public HomeUI_2Controller() {

    gridpane = new GridPane();
    displayscroll.setContent(gridpane);

    btnar = new ArrayList<>();
    int imageCol = 0;
    int imageRow = 0;

    for (int i = 0; i < filelist2.size(); i++) {
        Button downloadbtn = new Button("Download");
        downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                System.out.println("You are requested to download the file " + filelist2.get(i).getName());
            }
        });
        btnar.add(downloadbtn);

        System.out.println(filelist2.get(i).getName());
        image = new Image(filelist2.get(i).toURI().toString());

        ImageView pic = new ImageView();
        pic.setFitWidth(130);
        pic.setFitHeight(130);

        pic.setImage(image);
        vb = new VBox();
        vb.getChildren().addAll(pic, downloadbtn);

        gridpane.add(vb, imageCol, imageRow);
        GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
        imageCol++;

        // To check if all the 3 images of a row are completed
        if (imageCol > 2) {
            // Reset Column
            imageCol = 0;
            // Next Row
            imageRow++;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多