0

抱歉,标题含糊不清,但我认为我在 JavaFX BorderPane 类中发现了一个故障,但我并不肯定。所以我正在做的是在线程内的 javaFx 并发任务对象中运行此方法。这个方法一直有效,直到它到达打印语句。它打印出 1 然后不经过 root.setCenter 方法。如果我注释掉它继续运行的代码,否则它会卡在它上面,就像它处于无限循环中一样。需要注意的是,根(一个 boderpane 对象)本地存储在 JavaFX 主线程中。感谢您的任何建议。

  // will be used to store all the sites we still need to visit so we can do
  // a breadth first graph traversal of the hostsite
  Queue<URL> unvistedURLs = new LinkedList<>();
  LinkedList<Text> currentLevelText = new LinkedList<>();
  Queue<URL> levelCheckpoints = new LinkedList<>();
  int currentLevelHieght = 0;

  // the origional host
  String hostName = origin.getHost();

  // temporary objects
  HTMLLinks endHTMLLinks = null;
  try
  {
     endHTMLLinks = new HTMLLinks(origin);
  }
  catch (IOException e1)
  {
     // TODO Auto-generated catch block
     e1.printStackTrace();
  }

  HostSiteInfo hostSiteInfo = new HostSiteInfo();
  URL currentURL;
  Group displayArea = new Group();

  System.out.println(1);
  root.setCenter(displayArea);
  System.out.println(2);
  // imediatley input the host as a site we need to visit
  unvistedURLs.add(origin);
  levelCheckpoints.offer(origin);


@Override
public void start(Stage primaryStage)
{
  try
  {
     final BorderPane root = new BorderPane();
     Scene scene = new Scene(root, 1600, 1000);

     @SuppressWarnings("rawtypes")
     Thread renderThread = new Thread(new Task(){

        @Override
        protected Object call() throws Exception
        {
           try
           {

              WebSpider.traverseURLs(root,
                    new URL("http://www.georgefox.edu/"),
                    new PrintStream(System.out));
           }
           catch (MalformedURLException e)
           {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }

           return null;
        }});

     renderThread.setDaemon(true);
     renderThread.start();

JavaFx 应用程序的根在 this start 方法中初始化。

4

1 回答 1

0

假设root.setCenter在内部调用Task(这在您的代码中不清楚),您会遇到异常,因为您试图从 JavaFX UI 线程以外的线程修改场景图。由于这是在 a 中完成的Task,因此该异常会被任务捕获,而您最终看不到它。

要解决此问题,请替换root.setCenter(displayArea)

Platform.runLater(() -> root.cetCenter(displayArea)); 

请参阅Platform#runLater

为了能够捕获此类问题,最好为onFailed您设置一个处理程序Task

Task<Void> myTask = new Task<>() {
    ...
}
myTask.setOnFailed(workerStateEvent -> {
     System.out.println("Something wrong happened...");
     myTask.getException().printStackTrace();
     // Or handle the problem however suits your application. 
});
于 2016-06-20T09:33:26.480 回答