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

时间:2017-03-24 01:37:06

标签: java

我在Java FX的文本流容器中创建了超链接。下面提供的代码仅打开最后一个超链接文件,即使单击前面的链接也是如此。我认为问题出在迭代中。请耐心等待我,因为我仍然对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);
                   }
               }); 
         }

1 个答案:

答案 0 :(得分:0)

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

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

    }