簡體   English   中英

在javafx中,階段顯示為null

[英]Stage is showing to be null in javafx

在第一個場景中按下按鈕時調用此方法(abc)。 它所做的是將場景更改為WaitingScreen並調用另一個方法waitscr()

 public void abc(ActionEvent event)throws Exception{
 stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    //for changing the scene.
    Parent administrator = 
    FXMLLoader.load(getClass().getResource("waitingScreen.fxml"));
    stage.setScene(new Scene(administrator));
    stage.show();
    conn.close();
     waiting_screen_Controller c = new waiting_screen_Controller();
     c.waitscr(event);

waitscr的作用是啟動一個計時器5秒鍾,當計時器結束時它調用另一個方法setcr()(也許我只能在abc中啟動計時器)

  public void waitscr(ActionEvent event)throws IOException{
    timetask = new TimerTask(){

        @Override
        public void run() {
            if(!timing){
                try{
                    timetask.cancel();
                    setscr(event);
                }
                catch(Exception ex){
                    ex.printStackTrace();
               }
            }
            else
                timing = updateTime();
        }
    };
     timer.scheduleAtFixedRate(timetask,1000,1000);
}

它更新時間

 public boolean updateTime(){
    System.out.println(s);
       if(s==0){
           return false;
        }
        s--;
        return true;
        }

setcr的作用是將場景切換回第一個場景。

  public void setscr(ActionEvent event)throws IOException{ 
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("first.fxml"));
        Parent parent = loader.load();
        Scene s=new Scene(parent);
        stage = (Stage)((Node) event.getSource()).getScene().getWindow();
        System.out.print(event.getSource());
        stage.setScene(s);
        stage.show();

    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }

    }

但是問題在於它使npe處於階段狀態。

java.lang.NullPointerException
at sample.waiting_screen_Controller.setscr(waiting_screen_Controller.java:106)
at sample.waiting_screen_Controller$1.run(waiting_screen_Controller.java:45)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

我認為這是因為ActionEvent的緣故,因為npe處於階段,但是我打印了ActionEvent的來源,並且它不為null。

您要在調用waitscr之前替換場景。 這樣,當您調用Scene.getWindow ,場景不再與窗口關聯,結果為null

無論如何,您都不應該從非應用程序線程執行此操作。

通過僅檢索一次窗口並使用Platform.runLater您應該能夠解決此問題:

public void abc(ActionEvent event)throws Exception{
    stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    ...
    c.waitscr(stage);
public void waitscr(final Stage stage) throws IOException {
    timetask = new TimerTask(){

        @Override
        public void run() {
            if(!timing){
                try{
                    timetask.cancel();
                    setscr(stage);
                } catch(Exception ex){
                    ex.printStackTrace();
                }
            }
            else
                timing = updateTime();
        }
    };
    timer.scheduleAtFixedRate(timetask,1000,1000);
}
public void setscr(Stage stage)throws IOException{
        // there seems to be a try missing somewhere
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("first.fxml"));
        Parent parent = loader.load();
        Scene s=new Scene(parent);

        Platform.runLater(() -> {
            // scene update on javafx application thread
            stage.setScene(s);
            stage.show();
        });

    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM