【问题标题】:Possible Glitch found in JavaFX在 JavaFX 中发现可能的故障
【发布时间】:2016-06-20 05:39:14
【问题描述】:

对不起,标题含糊不清,但我认为我在 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 方法中初始化。

【问题讨论】:

  • root 在哪里初始化?
  • 抱歉,由于某种原因,本网站认为有些不是代码。
  • 我想这可能是我从匿名类中的匿名类中引用它。但如果这是问题所在,我认为它会崩溃。
  • 我修复了您的代码格式,并根据不同的缩进和不同的上下文(完整方法与 sn-p-y 代码)将其拆分为两个块。如果它本来就是一个块,请随时将其合并回一个块中。
  • 谢谢它是两个

标签: java java-8 javafx-8 borderpane


【解决方案1】:

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

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

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

Platform#runLater

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

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

【讨论】:

  • 哦,我想知道是否捕获了异常,这可能会修复我的代码,谢谢!
猜你喜欢
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 2015-01-09
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多