setOnAction不会触发 [英] setOnAction is not triggered

查看:198
本文介绍了setOnAction不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在点击一个按钮button.Here数组列表显示一些文本一直加到VBOX时,VBOX以gridpane和setOnAction内被调用Platform.runLater()方法,如图所示。
code:

 私人的ArrayList<按钮和GT; btnar;
  私人垂直框VB;
  私人按钮downloadbtn;
@FXML
  私人的ScrollPane displayscroll;
   私人GridPane gridpane; 公共HomeUI_2Controller(){
    Platform.runLater(新的Runnable(){        @覆盖
        公共无效的run(){
         gridpane =新GridPane();
         displayscroll.setContent(gridpane);         btnar =新的ArrayList<>();
            的for(int i = 0; I< filelist2.size();我++){
                downloadbtn =新按钮(下载);
                btnar.add(downloadbtn);
            }
            INT imageCol = 0;
            INT imageRow = 0;            的for(int i = 0; I< filelist2.size();我++){
                的System.out.println(filelist2.get(ⅰ).getName());                图像=新的图像(filelist2.get(I).toURI()的toString());                PIC =新ImageView的();
                pic.setFitWidth(130);
                pic.setFitHeight(130);                pic.setImage(图片);
                VB =新的垂直框();
                。vb.getChildren()的addAll(PIC(按钮)btnar.get(一));                gridpane.add(VB,imageCol,imageRow);
                GridPane.setMargin(PIC,新的插图(2,2,2,2));
                imageCol ++;                //要检查所有的一排3图像完成
                如果(imageCol→​​2){
                    //重置列
                    imageCol = 0;
                    //下一行
                    imageRow ++;
                }            }
             downloadbtn.setOnAction(新的EventHandler<&ActionEvent的GT;(){
                @覆盖
                公共无效手柄(ActionEvent的为arg0){
                    的System.out.println(SSSSS);
                }
            });    }
    });}


解决方案

  • 只有你最后的下载按钮应该被触发。因为你要添加的的OnAction事件处理程序只有最后一个。


  • 您不需要 Paltform.runLater()在这里。


  • 您不应该构建相同的多个循环,如果你可以循环一次。


  • 尽量保持变量的范围尽可能窄。换句话说,不要定义全局变量和使用无处不在的占位符。这使得code更容易出错,难以维持。


测试这个重构code:

 私人的ArrayList<按钮和GT; btnar;
私人垂直框VB;
@FXML
私人的ScrollPane displayscroll;
私人GridPane gridpane;公共HomeUI_2Controller(){    gridpane =新GridPane();
    displayscroll.setContent(gridpane);    btnar =新的ArrayList<>();
    INT imageCol = 0;
    INT imageRow = 0;    的for(int i = 0; I< filelist2.size();我++){
        按钮downloadbtn =新按钮(下载);
        downloadbtn.setOnAction(新的EventHandler<&ActionEvent的GT;(){
            @覆盖
            公共无效手柄(ActionEvent的为arg0){
                的System.out.println(你被要求下载文件+ filelist2.get(I).getName());
            }
        });
        btnar.add(downloadbtn);        的System.out.println(filelist2.get(ⅰ).getName());
        图像=新的图像(filelist2.get(I).toURI()的toString());        ImageView的PIC =新ImageView的();
        pic.setFitWidth(130);
        pic.setFitHeight(130);        pic.setImage(图片);
        VB =新的垂直框();
        。vb.getChildren()的addAll(PIC,downloadbtn);        gridpane.add(VB,imageCol,imageRow);
        GridPane.setMargin(PIC,新的插图(2,2,2,2));
        imageCol ++;        //要检查所有的一排3图像完成
        如果(imageCol→​​2){
            //重置列
            imageCol = 0;
            //下一行
            imageRow ++;
        }
    }
}

I require to display some text upon clicking a button.Here Button arraylist has been to added to vbox, the vbox to gridpane and setOnAction was invoked within Platform.runLater() method as shown. code:

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

    }
    });

}

解决方案

  • Only your last "Download" button should be triggered. Because you are adding the onAction event handler to the last one only.

  • You don't need Paltform.runLater() here.

  • You shouldn't construct identical multiple loops, if you can loop only once.

  • Try to keep variable's scope as narrow as possible. In another words, don't define global variable and use everywhere as placeholder. This makes the code more error prone and difficult to maintain.

Test this refactored code:

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

这篇关于setOnAction不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆