2016-05-11 63 views
0

我正在制作一个应用程序,我们希望在登录后在一段特定时间段内使用多线程来显示闪烁的标签“加载”,然后继续进入下一页。这是我目前的进度:JavaFX多线程问题

public class LoadingController implements Initializable { 

@FXML 
private Label loadingLabel; 

boolean ready = false; 

public void setReady() { 
    System.out.println("now I'm ready"); 
    ready = true; 
} 

public void showLabel() { 
    this.loadingLabel.setVisible(true); 
} 

public void hideLabel() { 
    this.loadingLabel.setVisible(false); 
} 

public void goToPage2() { 
    try { 
     Parent root = FXMLLoader.load(getClass().getResource("Page2.fxml")); 
     Scene scene = new Scene(root); 
     Stage stage = Assignment.getStage(); 
     stage.setScene(scene); 
    } catch (IOException ex) { 
     Logger.getLogger(LoadingController.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

/** 
* Initializes the controller class. 
*/ 
@Override 
public void initialize(URL url, ResourceBundle rb) { 
    System.out.println("In loading page"); 
    // TODO launch thread 
    Thread2 thread = new Thread2(this); 
    thread.start(); 


} 

}

public class Thread2 extends Thread { 

private LoadingController con; 

public Thread2(LoadingController con) { 
    this.con = con; 
} 

public void run() { 
    System.out.println("Hello from a thread!"); 
    try { 
     for (int i = 0; i < 20; i++) { 
      con.hideLabel(); 
      Thread.sleep(100); 
      con.showLabel(); 
      Thread.sleep(100); 

     } 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 

    } 

    con.setReady(); 

} 

}

我目前得到这个错误涉及到线 一幕一幕=新场景(根);

线程“线程-6”中的异常java.lang.IllegalStateException:不在FX应用程序线程中。

任何人都可以在这个问题上提供一些指导吗? 谢谢

回答