单击超链接时打开文件-Java FX

单击超链接时打开文件-Java FX,java,Java,我在JavaFX的textflow容器中创建了超链接。下面提供的代码仅在单击前面的链接时打开最后一个超链接文件。我认为问题在于迭代。请容忍我,因为我对Java还不熟悉` String[] splits = lessonResources.split("\\s+"); for(String s: splits){ link = new Hyperlink(s); lessonResourcesTextFlow.getChildre

我在JavaFX的textflow容器中创建了超链接。下面提供的代码仅在单击前面的链接时打开最后一个超链接文件。我认为问题在于迭代。请容忍我,因为我对Java还不熟悉`

String[] splits = lessonResources.split("\\s+");

       for(String s: splits){
             link = new Hyperlink(s);
             lessonResourcesTextFlow.getChildren().add(link);
             linked = new File(s);   
             link.setOnAction((ActionEvent e) -> {   
                  try {
                         if(Desktop.isDesktopSupported()){
                               try {
                                    Desktop.getDesktop().open(linked);
                                } catch (IOException ex) {
                                       Logger.getLogger(LessonPlanController.class.getName   ()).log(Level.SEVERE, null, ex);
                                }
                            }
                     } catch (Exception e) {
                      System.out.println(e);
                   }
               }); 
         }

在链接事件处理程序中包含文件构造函数解决了我的问题

String[] splits = lessonResources.split("\\s+");
    for(String s: splits){
            link = new Hyperlink(s);
            lessonResourcesTextFlow.getChildren().add(link);


            link.setOnAction((ActionEvent e) -> {  
                    linked = new File(s); 
                            try {
                                if(Desktop.isDesktopSupported()){
                                    try {
                                        Desktop.getDesktop().open(linked);
                                    } catch (IOException ex) {
                                        Logger.getLogger(LessonPlanController.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                }

                            } catch (Exception e) {
                                System.out.println(e);
                            }
            }); 

    } 

下一次,当你要求随机的陌生人拿出他们的空闲时间来免费解决你的问题时,至少花点时间正确使用空格键并格式化你的代码。